You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColorUtilTestCase.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.util;
  19. import java.awt.Color;
  20. import java.awt.color.ColorSpace;
  21. import java.io.File;
  22. import java.net.URI;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertFalse;
  26. import static org.junit.Assert.assertNotNull;
  27. import static org.junit.Assert.assertTrue;
  28. import org.apache.xmlgraphics.java2d.color.ColorSpaces;
  29. import org.apache.xmlgraphics.java2d.color.ColorWithAlternatives;
  30. import org.apache.xmlgraphics.java2d.color.NamedColorSpace;
  31. import org.apache.xmlgraphics.java2d.color.RenderingIntent;
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.apps.FopFactory;
  34. /**
  35. * Tests the ColorUtil class.
  36. */
  37. public class ColorUtilTestCase {
  38. private FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  39. /**
  40. * Test serialization to String.
  41. * @throws Exception if an error occurs
  42. */
  43. @Test
  44. public void testSerialization() throws Exception {
  45. Color col = new Color(1.0f, 1.0f, 0.5f, 1.0f);
  46. String s = ColorUtil.colorToString(col);
  47. //This is what the old color spit out. Now it is 80 due to rounding
  48. //assertEquals("#ffff7f", s);
  49. assertEquals("#ffff80", s);
  50. col = new Color(1.0f, 0.0f, 0.0f, 0.8f);
  51. s = ColorUtil.colorToString(col);
  52. assertEquals("#ff0000cc", s);
  53. }
  54. /**
  55. * Test deserialization from String.
  56. * @throws Exception if an error occurs
  57. */
  58. @Test
  59. public void testDeserialization() throws Exception {
  60. Color col = ColorUtil.parseColorString(null, "#ffff7f");
  61. assertEquals(255, col.getRed());
  62. assertEquals(255, col.getGreen());
  63. assertEquals(127, col.getBlue());
  64. assertEquals(255, col.getAlpha());
  65. col = ColorUtil.parseColorString(null, "#ff0000cc");
  66. assertEquals(255, col.getRed());
  67. assertEquals(0, col.getGreen());
  68. assertEquals(0, col.getBlue());
  69. assertEquals(204, col.getAlpha());
  70. }
  71. /**
  72. * Test equals().
  73. * @throws Exception if an error occurs
  74. */
  75. @Test
  76. public void testEquals() throws Exception {
  77. Color col1 = ColorUtil.parseColorString(null, "#ff0000cc");
  78. Color col2 = ColorUtil.parseColorString(null, "#ff0000cc");
  79. assertEquals(col1, col2);
  80. col1 = ColorUtil.parseColorString(null, "fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)");
  81. /* The following doesn't work since java.awt.Color from Sun doesn't round consistently
  82. col2 = ColorUtil.parseColorString(null, "cmyk(0.0,0.0,0.0,0.5)");
  83. assertEquals(col1, col2);
  84. */
  85. col2 = ColorUtil.parseColorString(null, "fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.5,0.5,0.5,0.0)");
  86. assertTrue(col1.equals(col2));
  87. assertFalse(org.apache.xmlgraphics.java2d.color.ColorUtil.isSameColor(col1, col2));
  88. }
  89. /**
  90. * Tests the rgb() function.
  91. * @throws Exception if an error occurs
  92. */
  93. @Test
  94. public void testRGB() throws Exception {
  95. FOUserAgent ua = fopFactory.newFOUserAgent();
  96. Color colActual;
  97. colActual = ColorUtil.parseColorString(ua, "rgb(255, 40, 0)");
  98. assertEquals(255, colActual.getRed());
  99. assertEquals(40, colActual.getGreen());
  100. assertEquals(0, colActual.getBlue());
  101. assertEquals(255, colActual.getAlpha());
  102. assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), colActual.getColorSpace());
  103. }
  104. /**
  105. * Tests the fop-rgb-icc() function.
  106. * @throws Exception if an error occurs
  107. */
  108. @Test
  109. public void testRGBICC() throws Exception {
  110. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  111. // [TBD] FIXME - use getResource() to access profile [GA]
  112. URI sRGBLoc = new URI("../fop-core/src/main/resources/org/apache/fop/pdf/sRGB.icc");
  113. ColorSpace cs = fopFactory.getColorSpaceCache().get(
  114. "sRGBAlt", sRGBLoc.toASCIIString(), RenderingIntent.AUTO);
  115. assertNotNull("Color profile not found", cs);
  116. FOUserAgent ua = fopFactory.newFOUserAgent();
  117. ColorWithFallback colActual;
  118. //fop-rgb-icc() is used instead of rgb-icc() inside FOP!
  119. String colSpec = "fop-rgb-icc(1.0,0.0,0.0,sRGBAlt,"
  120. + "\"" + sRGBLoc.toASCIIString() + "\",1.0,0.0,0.0)";
  121. colActual = (ColorWithFallback)ColorUtil.parseColorString(ua, colSpec);
  122. assertEquals(cs, colActual.getColorSpace());
  123. assertEquals(255, colActual.getRed(), 2f); //Java 5: 253, Java 6: 255
  124. assertEquals(0, colActual.getGreen(), 25f); //Java 5: 25, Java 6: 0
  125. assertEquals(0, colActual.getBlue());
  126. //I don't understand the difference. Maybe Java's sRGB and HP's sRGB are somehow not
  127. //equivalent. This is only going to be a problem if anyone actually makes use of the
  128. //RGB fallback in any renderer.
  129. //TODO Anyone know what's going on here?
  130. float[] comps = colActual.getColorComponents(null);
  131. assertEquals(3, comps.length);
  132. assertEquals(1f, comps[0], 0);
  133. assertEquals(0f, comps[1], 0);
  134. assertEquals(0f, comps[2], 0);
  135. assertEquals(0, colActual.getAlternativeColors().length);
  136. Color fallback = colActual.getFallbackColor();
  137. assertTrue(fallback.getColorSpace().isCS_sRGB());
  138. assertEquals(255, fallback.getRed());
  139. assertEquals(0, fallback.getGreen());
  140. assertEquals(0, fallback.getBlue());
  141. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  142. colSpec = "fop-rgb-icc(1.0,0.5,0.0,blah,"
  143. + "\"invalid.icm\",1.0,0.5,0.0,0.15)";
  144. Color colFallback = ColorUtil.parseColorString(ua, colSpec);
  145. assertEquals(new Color(1.0f, 0.5f, 0.0f), colFallback);
  146. }
  147. /**
  148. * Tests the cmyk() function.
  149. * @throws Exception if an error occurs
  150. */
  151. @Test
  152. public void testCMYK() throws Exception {
  153. ColorWithAlternatives colActual;
  154. String colSpec;
  155. colSpec = "cmyk(0.0, 0.0, 1.0, 0.0)";
  156. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  157. assertEquals(255, colActual.getRed());
  158. assertEquals(255, colActual.getGreen());
  159. assertEquals(0, colActual.getBlue());
  160. Color alt = colActual.getAlternativeColors()[0];
  161. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  162. float[] comps = alt.getColorComponents(null);
  163. assertEquals(4, comps.length);
  164. assertEquals(0f, comps[0], 0);
  165. assertEquals(0f, comps[1], 0);
  166. assertEquals(1f, comps[2], 0);
  167. assertEquals(0f, comps[3], 0);
  168. assertEquals("fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)",
  169. ColorUtil.colorToString(colActual));
  170. colSpec = "cmyk(0.0274, 0.2196, 0.3216, 0.0)";
  171. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  172. assertEquals(248, colActual.getRed(), 1);
  173. assertEquals(199, colActual.getGreen(), 1);
  174. assertEquals(172, colActual.getBlue(), 1);
  175. alt = colActual.getAlternativeColors()[0];
  176. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  177. comps = alt.getColorComponents(null);
  178. assertEquals(0.0274f, comps[0], 0.001);
  179. assertEquals(0.2196f, comps[1], 0.001);
  180. assertEquals(0.3216f, comps[2], 0.001);
  181. assertEquals(0f, comps[3], 0);
  182. assertEquals("fop-rgb-icc(0.9726,0.7804,0.67840004,#CMYK,,0.0274,0.2196,0.3216,0.0)",
  183. ColorUtil.colorToString(colActual));
  184. colSpec = "fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)";
  185. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  186. assertEquals(255, colActual.getRed());
  187. assertEquals(255, colActual.getGreen());
  188. assertEquals(0, colActual.getBlue());
  189. alt = colActual.getAlternativeColors()[0];
  190. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  191. comps = alt.getColorComponents(null);
  192. assertEquals(4, comps.length);
  193. assertEquals(0f, comps[0], 0);
  194. assertEquals(0f, comps[1], 0);
  195. assertEquals(1f, comps[2], 0);
  196. assertEquals(0f, comps[3], 0);
  197. assertEquals("fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)",
  198. ColorUtil.colorToString(colActual));
  199. colSpec = "fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)";
  200. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  201. assertEquals(127, colActual.getRed(), 1);
  202. assertEquals(127, colActual.getGreen(), 1);
  203. assertEquals(127, colActual.getBlue(), 1);
  204. alt = colActual.getAlternativeColors()[0];
  205. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  206. comps = alt.getColorComponents(null);
  207. assertEquals(4, comps.length);
  208. assertEquals(0f, comps[0], 0);
  209. assertEquals(0f, comps[1], 0);
  210. assertEquals(0f, comps[2], 0);
  211. assertEquals(0.5f, comps[3], 0);
  212. assertEquals("fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)",
  213. ColorUtil.colorToString(colActual));
  214. //Verify that the cmyk() and fop-rgb-icc(#CMYK) functions have the same results
  215. ColorWithAlternatives colCMYK = (ColorWithAlternatives)ColorUtil.parseColorString(
  216. null, "cmyk(0,0,0,0.5)");
  217. assertEquals(colCMYK.getAlternativeColors()[0], colActual.getAlternativeColors()[0]);
  218. //The following doesn't work:
  219. //assertEquals(colCMYK, colActual);
  220. //java.awt.Color does not consistenly calculate the int RGB values:
  221. //Color(ColorSpace cspace, float components[], float alpha): 0.5 --> 127
  222. //Color(float r, float g, float b): 0.5 --> 128
  223. if (!colCMYK.equals(colActual)) {
  224. System.out.println("Info: java.awt.Color does not consistently calculate"
  225. + " int RGB values from float RGB values.");
  226. }
  227. }
  228. /**
  229. * Tests color for the #Separation pseudo-colorspace.
  230. * @throws Exception if an error occurs
  231. */
  232. @Test
  233. public void testSeparationColor() throws Exception {
  234. ColorWithFallback colActual;
  235. String colSpec;
  236. colSpec = "fop-rgb-icc(1.0,0.8,0.0,#Separation,,Postgelb)";
  237. colActual = (ColorWithFallback)ColorUtil.parseColorString(null, colSpec);
  238. assertEquals(255, colActual.getRed(), 5);
  239. assertEquals(204, colActual.getGreen(), 3);
  240. assertEquals(0, colActual.getBlue(), 12);
  241. //sRGB results differ between JDKs
  242. Color fallback = colActual.getFallbackColor();
  243. assertEquals(255, fallback.getRed());
  244. assertEquals(204, fallback.getGreen());
  245. assertEquals(0, fallback.getBlue());
  246. assertFalse(colActual.hasAlternativeColors());
  247. assertTrue(colActual.getColorSpace() instanceof NamedColorSpace);
  248. NamedColorSpace ncs;
  249. ncs = (NamedColorSpace)colActual.getColorSpace();
  250. assertEquals("Postgelb", ncs.getColorName());
  251. float[] comps = colActual.getColorComponents(null);
  252. assertEquals(1, comps.length);
  253. assertEquals(1f, comps[0], 0);
  254. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  255. }
  256. /**
  257. * Tests the fop-rgb-named-color() function.
  258. * @throws Exception if an error occurs
  259. */
  260. @Test
  261. public void testNamedColorProfile() throws Exception {
  262. FopFactory fopFactory = FopFactory.newInstance(new File("./").toURI());
  263. URI ncpLoc = new URI("test/resources/color/ncp-example.icc");
  264. ColorSpace cs = fopFactory.getColorSpaceCache().get(
  265. "NCP", ncpLoc.toASCIIString(), RenderingIntent.AUTO);
  266. assertNotNull("Color profile not found", cs);
  267. FOUserAgent ua = fopFactory.newFOUserAgent();
  268. ColorWithFallback colActual;
  269. //fop-rgb-named-color() is used instead of rgb-named-color() inside FOP!
  270. String colSpec = "fop-rgb-named-color(1.0,0.8,0.0,NCP,"
  271. + "\"" + ncpLoc.toASCIIString() + "\",Postgelb)";
  272. colActual = (ColorWithFallback)ColorUtil.parseColorString(ua, colSpec);
  273. assertEquals(255, colActual.getRed(), 2);
  274. assertEquals(193, colActual.getGreen(), 2);
  275. assertEquals(0, colActual.getBlue());
  276. Color fallback = colActual.getFallbackColor();
  277. assertEquals(255, fallback.getRed());
  278. assertEquals(204, fallback.getGreen());
  279. assertEquals(0, fallback.getBlue());
  280. assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), fallback.getColorSpace());
  281. float[] comps = fallback.getColorComponents(null);
  282. assertEquals(3, comps.length);
  283. assertEquals(1f, comps[0], 0);
  284. assertEquals(0.8f, comps[1], 0);
  285. assertEquals(0f, comps[2], 0);
  286. assertTrue(colActual.getColorSpace() instanceof NamedColorSpace);
  287. NamedColorSpace ncs;
  288. ncs = (NamedColorSpace)colActual.getColorSpace();
  289. assertEquals("Postgelb", ncs.getColorName());
  290. comps = colActual.getColorComponents(null);
  291. assertEquals(1, comps.length);
  292. assertEquals(1f, comps[0], 0);
  293. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  294. }
  295. }