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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.getColorSpace("sRGBAlt", null, sRGBLoc.toASCIIString(),
  104. 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());
  114. assertEquals(0, colActual.getGreen());
  115. assertEquals(0, colActual.getBlue());
  116. float[] comps = colActual.getColorComponents(null);
  117. assertEquals(3, comps.length);
  118. assertEquals(1f, comps[0], 0);
  119. assertEquals(0f, comps[1], 0);
  120. assertEquals(0f, comps[2], 0);
  121. assertEquals(0, colActual.getAlternativeColors().length);
  122. Color fallback = colActual.getFallbackColor();
  123. assertTrue(fallback.getColorSpace().isCS_sRGB());
  124. assertEquals(255, colActual.getRed());
  125. assertEquals(0, colActual.getGreen());
  126. assertEquals(0, colActual.getBlue());
  127. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  128. colSpec = "fop-rgb-icc(1.0,0.5,0.0,blah,"
  129. + "\"invalid.icm\",1.0,0.5,0.0,0.15)";
  130. Color colFallback = ColorUtil.parseColorString(ua, colSpec);
  131. assertEquals(new Color(1.0f, 0.5f, 0.0f), colFallback);
  132. }
  133. /**
  134. * Tests the cmyk() function.
  135. * @throws Exception if an error occurs
  136. */
  137. public void testCMYK() throws Exception {
  138. ColorWithAlternatives colActual;
  139. String colSpec;
  140. colSpec = "cmyk(0.0, 0.0, 1.0, 0.0)";
  141. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  142. assertEquals(255, colActual.getRed());
  143. assertEquals(255, colActual.getGreen());
  144. assertEquals(0, colActual.getBlue());
  145. Color alt = colActual.getAlternativeColors()[0];
  146. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  147. float[] comps = alt.getColorComponents(null);
  148. assertEquals(4, comps.length);
  149. assertEquals(0f, comps[0], 0);
  150. assertEquals(0f, comps[1], 0);
  151. assertEquals(1f, comps[2], 0);
  152. assertEquals(0f, comps[3], 0);
  153. assertEquals("fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)",
  154. ColorUtil.colorToString(colActual));
  155. colSpec = "cmyk(0.0274, 0.2196, 0.3216, 0.0)";
  156. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  157. assertEquals(248, colActual.getRed(), 1);
  158. assertEquals(199, colActual.getGreen(), 1);
  159. assertEquals(172, colActual.getBlue(), 1);
  160. alt = colActual.getAlternativeColors()[0];
  161. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  162. comps = alt.getColorComponents(null);
  163. assertEquals(0.0274f, comps[0], 0.001);
  164. assertEquals(0.2196f, comps[1], 0.001);
  165. assertEquals(0.3216f, comps[2], 0.001);
  166. assertEquals(0f, comps[3], 0);
  167. assertEquals("fop-rgb-icc(0.972549,0.78039217,0.6745098,#CMYK,,0.0274,0.2196,0.3216,0.0)",
  168. ColorUtil.colorToString(colActual));
  169. colSpec = "fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)";
  170. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  171. assertEquals(255, colActual.getRed());
  172. assertEquals(255, colActual.getGreen());
  173. assertEquals(0, colActual.getBlue());
  174. alt = colActual.getAlternativeColors()[0];
  175. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  176. comps = alt.getColorComponents(null);
  177. assertEquals(4, comps.length);
  178. assertEquals(0f, comps[0], 0);
  179. assertEquals(0f, comps[1], 0);
  180. assertEquals(1f, comps[2], 0);
  181. assertEquals(0f, comps[3], 0);
  182. assertEquals("fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)",
  183. ColorUtil.colorToString(colActual));
  184. colSpec = "fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)";
  185. colActual = (ColorWithAlternatives)ColorUtil.parseColorString(null, colSpec);
  186. assertEquals(127, colActual.getRed(), 1);
  187. assertEquals(127, colActual.getGreen(), 1);
  188. assertEquals(127, colActual.getBlue(), 1);
  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(0f, comps[2], 0);
  196. assertEquals(0.5f, comps[3], 0);
  197. assertEquals("fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)",
  198. ColorUtil.colorToString(colActual));
  199. }
  200. /**
  201. * Tests color for the #Separation pseudo-colorspace.
  202. * @throws Exception if an error occurs
  203. */
  204. public void testSeparationColor() throws Exception {
  205. ColorWithFallback colActual;
  206. String colSpec;
  207. colSpec = "fop-rgb-icc(1.0,0.8,0.0,#Separation,,Postgelb)";
  208. colActual = (ColorWithFallback)ColorUtil.parseColorString(null, colSpec);
  209. assertEquals(255, colActual.getRed(), 1);
  210. assertEquals(204, colActual.getGreen(), 1);
  211. assertEquals(0, colActual.getBlue());
  212. Color fallback = colActual.getFallbackColor();
  213. assertEquals(255, fallback.getRed());
  214. assertEquals(204, fallback.getGreen());
  215. assertEquals(0, fallback.getBlue());
  216. assertFalse(colActual.hasAlternativeColors());
  217. assertTrue(colActual.getColorSpace() instanceof NamedColorSpace);
  218. NamedColorSpace ncs;
  219. ncs = (NamedColorSpace)colActual.getColorSpace();
  220. assertEquals("Postgelb", ncs.getColorName());
  221. float[] comps = colActual.getColorComponents(null);
  222. assertEquals(1, comps.length);
  223. assertEquals(1f, comps[0], 0);
  224. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  225. }
  226. /**
  227. * Tests the fop-rgb-named-color() function.
  228. * @throws Exception if an error occurs
  229. */
  230. public void testNamedColorProfile() throws Exception {
  231. FopFactory fopFactory = FopFactory.newInstance();
  232. URI ncpLoc = new URI("file:test/resources/color/ncp-example.icc");
  233. ColorSpace cs = fopFactory.getColorSpace("NCP", null, ncpLoc.toASCIIString(),
  234. RenderingIntent.AUTO);
  235. assertNotNull("Color profile not found", cs);
  236. FOUserAgent ua = fopFactory.newFOUserAgent();
  237. ColorWithFallback colActual;
  238. //fop-rgb-named-color() is used instead of rgb-named-color() inside FOP!
  239. String colSpec = "fop-rgb-named-color(1.0,0.8,0.0,NCP,"
  240. + "\"" + ncpLoc.toASCIIString() + "\",Postgelb)";
  241. colActual = (ColorWithFallback)ColorUtil.parseColorString(ua, colSpec);
  242. assertEquals(255, colActual.getRed());
  243. assertEquals(193, colActual.getGreen());
  244. assertEquals(0, colActual.getBlue());
  245. Color fallback = colActual.getFallbackColor();
  246. assertEquals(255, fallback.getRed());
  247. assertEquals(204, fallback.getGreen());
  248. assertEquals(0, fallback.getBlue());
  249. assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), fallback.getColorSpace());
  250. float[] comps = fallback.getColorComponents(null);
  251. assertEquals(3, comps.length);
  252. assertEquals(1f, comps[0], 0);
  253. assertEquals(0.8f, comps[1], 0);
  254. assertEquals(0f, comps[2], 0);
  255. assertTrue(colActual.getColorSpace() instanceof NamedColorSpace);
  256. NamedColorSpace ncs;
  257. ncs = (NamedColorSpace)colActual.getColorSpace();
  258. assertEquals("Postgelb", ncs.getColorName());
  259. comps = colActual.getColorComponents(null);
  260. assertEquals(1, comps.length);
  261. assertEquals(1f, comps[0], 0);
  262. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  263. }
  264. }