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

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