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 13KB

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