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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 org.apache.xmlgraphics.java2d.color.ColorSpaces;
  25. import org.apache.xmlgraphics.java2d.color.ColorWithAlternatives;
  26. import org.apache.xmlgraphics.java2d.color.NamedColorSpace;
  27. import org.apache.xmlgraphics.java2d.color.RenderingIntent;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.apps.FopFactory;
  30. import static org.junit.Assert.assertEquals;
  31. import static org.junit.Assert.assertFalse;
  32. import static org.junit.Assert.assertNotNull;
  33. import static org.junit.Assert.assertTrue;
  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. URI sRGBLoc = new URI("src/java/org/apache/fop/pdf/sRGB%20Color%20Space%20Profile.icm");
  112. ColorSpace cs = fopFactory.getColorSpaceCache().get(
  113. "sRGBAlt", sRGBLoc.toASCIIString(), RenderingIntent.AUTO);
  114. assertNotNull("Color profile not found", cs);
  115. FOUserAgent ua = fopFactory.newFOUserAgent();
  116. ColorWithFallback colActual;
  117. //fop-rgb-icc() is used instead of rgb-icc() inside FOP!
  118. String colSpec = "fop-rgb-icc(1.0,0.0,0.0,sRGBAlt,"
  119. + "\"" + sRGBLoc.toASCIIString() + "\",1.0,0.0,0.0)";
  120. colActual = (ColorWithFallback)ColorUtil.parseColorString(ua, colSpec);
  121. assertEquals(cs, colActual.getColorSpace());
  122. assertEquals(255, colActual.getRed(), 2f); //Java 5: 253, Java 6: 255
  123. assertEquals(0, colActual.getGreen(), 25f); //Java 5: 25, Java 6: 0
  124. assertEquals(0, colActual.getBlue());
  125. //I don't understand the difference. Maybe Java's sRGB and HP's sRGB are somehow not
  126. //equivalent. This is only going to be a problem if anyone actually makes use of the
  127. //RGB fallback in any renderer.
  128. //TODO Anyone know what's going on here?
  129. float[] comps = colActual.getColorComponents(null);
  130. assertEquals(3, comps.length);
  131. assertEquals(1f, comps[0], 0);
  132. assertEquals(0f, comps[1], 0);
  133. assertEquals(0f, comps[2], 0);
  134. assertEquals(0, colActual.getAlternativeColors().length);
  135. Color fallback = colActual.getFallbackColor();
  136. assertTrue(fallback.getColorSpace().isCS_sRGB());
  137. assertEquals(255, fallback.getRed());
  138. assertEquals(0, fallback.getGreen());
  139. assertEquals(0, fallback.getBlue());
  140. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  141. colSpec = "fop-rgb-icc(1.0,0.5,0.0,blah,"
  142. + "\"invalid.icm\",1.0,0.5,0.0,0.15)";
  143. Color colFallback = ColorUtil.parseColorString(ua, colSpec);
  144. assertEquals(new Color(1.0f, 0.5f, 0.0f), colFallback);
  145. }
  146. /**
  147. * Tests the cmyk() function.
  148. * @throws Exception if an error occurs
  149. */
  150. @Test
  151. public void testCMYK() throws Exception {
  152. ColorWithAlternatives colActual;
  153. String colSpec;
  154. colSpec = "cmyk(0.0, 0.0, 1.0, 0.0)";
  155. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  156. assertEquals(255, colActual.getRed());
  157. assertEquals(255, colActual.getGreen());
  158. assertEquals(0, colActual.getBlue());
  159. Color alt = colActual.getAlternativeColors()[0];
  160. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  161. float[] comps = alt.getColorComponents(null);
  162. assertEquals(4, comps.length);
  163. assertEquals(0f, comps[0], 0);
  164. assertEquals(0f, comps[1], 0);
  165. assertEquals(1f, comps[2], 0);
  166. assertEquals(0f, comps[3], 0);
  167. assertEquals("fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)",
  168. ColorUtil.colorToString(colActual));
  169. colSpec = "cmyk(0.0274, 0.2196, 0.3216, 0.0)";
  170. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  171. assertEquals(248, colActual.getRed(), 1);
  172. assertEquals(199, colActual.getGreen(), 1);
  173. assertEquals(172, colActual.getBlue(), 1);
  174. alt = colActual.getAlternativeColors()[0];
  175. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  176. comps = alt.getColorComponents(null);
  177. assertEquals(0.0274f, comps[0], 0.001);
  178. assertEquals(0.2196f, comps[1], 0.001);
  179. assertEquals(0.3216f, comps[2], 0.001);
  180. assertEquals(0f, comps[3], 0);
  181. assertEquals("fop-rgb-icc(0.9726,0.7804,0.67840004,#CMYK,,0.0274,0.2196,0.3216,0.0)",
  182. ColorUtil.colorToString(colActual));
  183. colSpec = "fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)";
  184. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  185. assertEquals(255, colActual.getRed());
  186. assertEquals(255, colActual.getGreen());
  187. assertEquals(0, colActual.getBlue());
  188. alt = colActual.getAlternativeColors()[0];
  189. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  190. comps = alt.getColorComponents(null);
  191. assertEquals(4, comps.length);
  192. assertEquals(0f, comps[0], 0);
  193. assertEquals(0f, comps[1], 0);
  194. assertEquals(1f, comps[2], 0);
  195. assertEquals(0f, comps[3], 0);
  196. assertEquals("fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)",
  197. ColorUtil.colorToString(colActual));
  198. colSpec = "fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)";
  199. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  200. assertEquals(127, colActual.getRed(), 1);
  201. assertEquals(127, colActual.getGreen(), 1);
  202. assertEquals(127, colActual.getBlue(), 1);
  203. alt = colActual.getAlternativeColors()[0];
  204. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  205. comps = alt.getColorComponents(null);
  206. assertEquals(4, comps.length);
  207. assertEquals(0f, comps[0], 0);
  208. assertEquals(0f, comps[1], 0);
  209. assertEquals(0f, comps[2], 0);
  210. assertEquals(0.5f, comps[3], 0);
  211. assertEquals("fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)",
  212. ColorUtil.colorToString(colActual));
  213. //Verify that the cmyk() and fop-rgb-icc(#CMYK) functions have the same results
  214. ColorWithAlternatives colCMYK = (ColorWithAlternatives)ColorUtil.parseColorString(
  215. null, "cmyk(0,0,0,0.5)");
  216. assertEquals(colCMYK.getAlternativeColors()[0], colActual.getAlternativeColors()[0]);
  217. //The following doesn't work:
  218. //assertEquals(colCMYK, colActual);
  219. //java.awt.Color does not consistenly calculate the int RGB values:
  220. //Color(ColorSpace cspace, float components[], float alpha): 0.5 --> 127
  221. //Color(float r, float g, float b): 0.5 --> 128
  222. if (!colCMYK.equals(colActual)) {
  223. System.out.println("Info: java.awt.Color does not consistently calculate"
  224. + " int RGB values from float RGB values.");
  225. }
  226. }
  227. /**
  228. * Tests color for the #Separation pseudo-colorspace.
  229. * @throws Exception if an error occurs
  230. */
  231. @Test
  232. public void testSeparationColor() throws Exception {
  233. ColorWithFallback colActual;
  234. String colSpec;
  235. colSpec = "fop-rgb-icc(1.0,0.8,0.0,#Separation,,Postgelb)";
  236. colActual = (ColorWithFallback)ColorUtil.parseColorString(null, colSpec);
  237. assertEquals(255, colActual.getRed(), 5);
  238. assertEquals(204, colActual.getGreen(), 3);
  239. assertEquals(0, colActual.getBlue(), 12);
  240. //sRGB results differ between JDKs
  241. Color fallback = colActual.getFallbackColor();
  242. assertEquals(255, fallback.getRed());
  243. assertEquals(204, fallback.getGreen());
  244. assertEquals(0, fallback.getBlue());
  245. assertFalse(colActual.hasAlternativeColors());
  246. assertTrue(colActual.getColorSpace() instanceof NamedColorSpace);
  247. NamedColorSpace ncs;
  248. ncs = (NamedColorSpace)colActual.getColorSpace();
  249. assertEquals("Postgelb", ncs.getColorName());
  250. float[] comps = colActual.getColorComponents(null);
  251. assertEquals(1, comps.length);
  252. assertEquals(1f, comps[0], 0);
  253. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  254. }
  255. /**
  256. * Tests the fop-rgb-named-color() function.
  257. * @throws Exception if an error occurs
  258. */
  259. @Test
  260. public void testNamedColorProfile() throws Exception {
  261. FopFactory fopFactory = FopFactory.newInstance(new File("./").toURI());
  262. URI ncpLoc = new URI("test/resources/color/ncp-example.icc");
  263. ColorSpace cs = fopFactory.getColorSpaceCache().get(
  264. "NCP", ncpLoc.toASCIIString(), RenderingIntent.AUTO);
  265. assertNotNull("Color profile not found", cs);
  266. FOUserAgent ua = fopFactory.newFOUserAgent();
  267. ColorWithFallback colActual;
  268. //fop-rgb-named-color() is used instead of rgb-named-color() inside FOP!
  269. String colSpec = "fop-rgb-named-color(1.0,0.8,0.0,NCP,"
  270. + "\"" + ncpLoc.toASCIIString() + "\",Postgelb)";
  271. colActual = (ColorWithFallback)ColorUtil.parseColorString(ua, colSpec);
  272. assertEquals(255, colActual.getRed(), 2);
  273. assertEquals(193, colActual.getGreen(), 2);
  274. assertEquals(0, colActual.getBlue());
  275. Color fallback = colActual.getFallbackColor();
  276. assertEquals(255, fallback.getRed());
  277. assertEquals(204, fallback.getGreen());
  278. assertEquals(0, fallback.getBlue());
  279. assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), fallback.getColorSpace());
  280. float[] comps = fallback.getColorComponents(null);
  281. assertEquals(3, comps.length);
  282. assertEquals(1f, comps[0], 0);
  283. assertEquals(0.8f, comps[1], 0);
  284. assertEquals(0f, comps[2], 0);
  285. assertTrue(colActual.getColorSpace() instanceof NamedColorSpace);
  286. NamedColorSpace ncs;
  287. ncs = (NamedColorSpace)colActual.getColorSpace();
  288. assertEquals("Postgelb", ncs.getColorName());
  289. comps = colActual.getColorComponents(null);
  290. assertEquals(1, comps.length);
  291. assertEquals(1f, comps[0], 0);
  292. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  293. }
  294. }