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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.ColorExt;
  24. import org.apache.xmlgraphics.java2d.color.ColorSpaces;
  25. import org.apache.xmlgraphics.java2d.color.NamedColorSpace;
  26. import org.apache.fop.apps.FOUserAgent;
  27. import org.apache.fop.apps.FopFactory;
  28. /**
  29. * Tests the ColorUtil class.
  30. */
  31. public class ColorUtilTestCase extends TestCase {
  32. /**
  33. * Test serialization to String.
  34. * @throws Exception if an error occurs
  35. */
  36. public void testSerialization() throws Exception {
  37. Color col = new Color(1.0f, 1.0f, 0.5f, 1.0f);
  38. String s = ColorUtil.colorToString(col);
  39. //This is what the old color spit out. Now it is 80 due to rounding
  40. //assertEquals("#ffff7f", s);
  41. assertEquals("#ffff80", s);
  42. col = new Color(1.0f, 0.0f, 0.0f, 0.8f);
  43. s = ColorUtil.colorToString(col);
  44. assertEquals("#ff0000cc", s);
  45. }
  46. /**
  47. * Test deserialization from String.
  48. * @throws Exception if an error occurs
  49. */
  50. public void testDeserialization() throws Exception {
  51. Color col = ColorUtil.parseColorString(null, "#ffff7f");
  52. assertEquals(255, col.getRed());
  53. assertEquals(255, col.getGreen());
  54. assertEquals(127, col.getBlue());
  55. assertEquals(255, col.getAlpha());
  56. col = ColorUtil.parseColorString(null, "#ff0000cc");
  57. assertEquals(255, col.getRed());
  58. assertEquals(0, col.getGreen());
  59. assertEquals(0, col.getBlue());
  60. assertEquals(204, col.getAlpha());
  61. }
  62. /**
  63. * Test equals().
  64. * @throws Exception if an error occurs
  65. */
  66. public void testEquals() throws Exception {
  67. Color col1 = ColorUtil.parseColorString(null, "#ff0000cc");
  68. Color col2 = ColorUtil.parseColorString(null, "#ff0000cc");
  69. assertEquals(col1, col2);
  70. col1 = ColorUtil.parseColorString(null, "fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)");
  71. col2 = ColorUtil.parseColorString(null, "cmyk(0.0,0.0,0.0,0.5)");
  72. assertEquals(col1, col2);
  73. col2 = ColorUtil.parseColorString(null, "fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.5,0.5,0.5,0.0)");
  74. assertFalse(col1.equals(col2));
  75. }
  76. /**
  77. * Tests the rgb() function.
  78. * @throws Exception if an error occurs
  79. */
  80. public void testRGB() throws Exception {
  81. FopFactory fopFactory = FopFactory.newInstance();
  82. FOUserAgent ua = fopFactory.newFOUserAgent();
  83. Color colActual;
  84. colActual = ColorUtil.parseColorString(ua, "rgb(255, 40, 0)");
  85. assertEquals(255, colActual.getRed());
  86. assertEquals(40, colActual.getGreen());
  87. assertEquals(0, colActual.getBlue());
  88. assertEquals(255, colActual.getAlpha());
  89. assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), colActual.getColorSpace());
  90. }
  91. /**
  92. * Tests the fop-rgb-icc() function.
  93. * @throws Exception if an error occurs
  94. */
  95. public void testRGBICC() throws Exception {
  96. FopFactory fopFactory = FopFactory.newInstance();
  97. URI sRGBLoc = new URI(
  98. "file:src/java/org/apache/fop/pdf/sRGB%20Color%20Space%20Profile.icm");
  99. ColorSpace cs = fopFactory.getColorSpace(null, sRGBLoc.toASCIIString());
  100. assertNotNull(cs);
  101. FOUserAgent ua = fopFactory.newFOUserAgent();
  102. ColorExt colActual;
  103. //fop-rgb-icc() is used instead of rgb-icc() inside FOP!
  104. String colSpec = "fop-rgb-icc(1.0,0.0,0.0,sRGBAlt,"
  105. + "\"" + sRGBLoc.toASCIIString() + "\",1.0,0.0,0.0)";
  106. colActual = (ColorExt)ColorUtil.parseColorString(ua, colSpec);
  107. assertEquals(255, colActual.getRed());
  108. assertEquals(0, colActual.getGreen());
  109. assertEquals(0, colActual.getBlue());
  110. assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), colActual.getColorSpace());
  111. float[] comps = colActual.getColorComponents(null);
  112. assertEquals(3, comps.length);
  113. assertEquals(1f, comps[0], 0);
  114. assertEquals(0f, comps[1], 0);
  115. assertEquals(0f, comps[2], 0);
  116. Color alt = colActual.getAlternateColors()[0];
  117. assertEquals(cs, alt.getColorSpace());
  118. comps = colActual.getColorComponents(null);
  119. assertEquals(3, comps.length);
  120. assertEquals(1f, comps[0], 0);
  121. assertEquals(0f, comps[1], 0);
  122. assertEquals(0f, comps[2], 0);
  123. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  124. colSpec = "fop-rgb-icc(1.0,0.5,0.0,blah,"
  125. + "\"invalid.icm\",1.0,0.5,0.0,0.15)";
  126. Color colFallback = ColorUtil.parseColorString(ua, colSpec);
  127. assertEquals(new Color(1.0f, 0.5f, 0.0f), colFallback);
  128. }
  129. /**
  130. * Tests the cmyk() function.
  131. * @throws Exception if an error occurs
  132. */
  133. public void testCMYK() throws Exception {
  134. ColorExt colActual;
  135. String colSpec;
  136. colSpec = "cmyk(0.0, 0.0, 1.0, 0.0)";
  137. colActual = (ColorExt)ColorUtil.parseColorString(null, colSpec);
  138. assertEquals(255, colActual.getRed());
  139. assertEquals(255, colActual.getGreen());
  140. assertEquals(0, colActual.getBlue());
  141. Color alt = colActual.getAlternateColors()[0];
  142. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  143. float[] comps = alt.getColorComponents(null);
  144. assertEquals(4, comps.length);
  145. assertEquals(0f, comps[0], 0);
  146. assertEquals(0f, comps[1], 0);
  147. assertEquals(1f, comps[2], 0);
  148. assertEquals(0f, comps[3], 0);
  149. assertEquals("fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)",
  150. ColorUtil.colorToString(colActual));
  151. colSpec = "cmyk(0.0274, 0.2196, 0.3216, 0.0)";
  152. colActual = (ColorExt)ColorUtil.parseColorString(null, colSpec);
  153. assertEquals(248, colActual.getRed(), 1);
  154. assertEquals(199, colActual.getGreen(), 1);
  155. assertEquals(172, colActual.getBlue(), 1);
  156. alt = colActual.getAlternateColors()[0];
  157. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  158. comps = alt.getColorComponents(null);
  159. assertEquals(0.0274f, comps[0], 0.001);
  160. assertEquals(0.2196f, comps[1], 0.001);
  161. assertEquals(0.3216f, comps[2], 0.001);
  162. assertEquals(0f, comps[3], 0);
  163. assertEquals("fop-rgb-icc(0.9726,0.7804,0.67840004,#CMYK,,0.0274,0.2196,0.3216,0.0)",
  164. ColorUtil.colorToString(colActual));
  165. colSpec = "fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)";
  166. colActual = (ColorExt)ColorUtil.parseColorString(null, colSpec);
  167. assertEquals(255, colActual.getRed());
  168. assertEquals(255, colActual.getGreen());
  169. assertEquals(0, colActual.getBlue());
  170. alt = colActual.getAlternateColors()[0];
  171. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  172. comps = alt.getColorComponents(null);
  173. assertEquals(4, comps.length);
  174. assertEquals(0f, comps[0], 0);
  175. assertEquals(0f, comps[1], 0);
  176. assertEquals(1f, comps[2], 0);
  177. assertEquals(0f, comps[3], 0);
  178. assertEquals("fop-rgb-icc(1.0,1.0,0.0,#CMYK,,0.0,0.0,1.0,0.0)",
  179. ColorUtil.colorToString(colActual));
  180. colSpec = "fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)";
  181. colActual = (ColorExt)ColorUtil.parseColorString(null, colSpec);
  182. assertEquals(127, colActual.getRed(), 1);
  183. assertEquals(127, colActual.getGreen(), 1);
  184. assertEquals(127, colActual.getBlue(), 1);
  185. alt = colActual.getAlternateColors()[0];
  186. assertEquals(ColorSpaces.getDeviceCMYKColorSpace(), alt.getColorSpace());
  187. comps = alt.getColorComponents(null);
  188. assertEquals(4, comps.length);
  189. assertEquals(0f, comps[0], 0);
  190. assertEquals(0f, comps[1], 0);
  191. assertEquals(0f, comps[2], 0);
  192. assertEquals(0.5f, comps[3], 0);
  193. assertEquals("fop-rgb-icc(0.5,0.5,0.5,#CMYK,,0.0,0.0,0.0,0.5)",
  194. ColorUtil.colorToString(colActual));
  195. }
  196. /**
  197. * Tests color for the #Separation pseudo-colorspace.
  198. * @throws Exception if an error occurs
  199. */
  200. public void testSeparationColor() throws Exception {
  201. ColorExt colActual;
  202. String colSpec;
  203. colSpec = "fop-rgb-icc(1.0,0.8,0.0,#Separation,,Postgelb)";
  204. colActual = (ColorExt)ColorUtil.parseColorString(null, colSpec);
  205. assertEquals(255, colActual.getRed());
  206. assertEquals(204, colActual.getGreen());
  207. assertEquals(0, colActual.getBlue());
  208. Color alt = colActual.getAlternateColors()[0];
  209. assertTrue(alt.getColorSpace() instanceof NamedColorSpace);
  210. NamedColorSpace ncs;
  211. ncs = (NamedColorSpace)alt.getColorSpace();
  212. assertEquals("Postgelb", ncs.getColorName());
  213. float[] comps = alt.getColorComponents(null);
  214. assertEquals(1, comps.length);
  215. assertEquals(1f, comps[0], 0);
  216. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  217. }
  218. /**
  219. * Tests the fop-rgb-named-color() function.
  220. * @throws Exception if an error occurs
  221. */
  222. public void testNamedColorProfile() throws Exception {
  223. FopFactory fopFactory = FopFactory.newInstance();
  224. URI ncpLoc = new URI("file:test/resources/color/ncp-example.icc");
  225. ColorSpace cs = fopFactory.getColorSpace(null, ncpLoc.toASCIIString());
  226. assertNotNull(cs);
  227. FOUserAgent ua = fopFactory.newFOUserAgent();
  228. ColorExt colActual;
  229. //fop-rgb-named-color() is used instead of rgb-named-color() inside FOP!
  230. String colSpec = "fop-rgb-named-color(1.0,0.8,0.0,NCP,"
  231. + "\"" + ncpLoc.toASCIIString() + "\",Postgelb)";
  232. colActual = (ColorExt)ColorUtil.parseColorString(ua, colSpec);
  233. assertEquals(255, colActual.getRed());
  234. assertEquals(204, colActual.getGreen());
  235. assertEquals(0, colActual.getBlue());
  236. assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), colActual.getColorSpace());
  237. float[] comps = colActual.getColorComponents(null);
  238. assertEquals(3, comps.length);
  239. assertEquals(1f, comps[0], 0);
  240. assertEquals(0.8f, comps[1], 0);
  241. assertEquals(0f, comps[2], 0);
  242. Color alt = colActual.getAlternateColors()[0];
  243. assertTrue(alt.getColorSpace() instanceof NamedColorSpace);
  244. NamedColorSpace ncs;
  245. ncs = (NamedColorSpace)alt.getColorSpace();
  246. assertEquals("Postgelb", ncs.getColorName());
  247. comps = alt.getColorComponents(null);
  248. assertEquals(1, comps.length);
  249. assertEquals(1f, comps[0], 0);
  250. assertEquals(colSpec, ColorUtil.colorToString(colActual));
  251. }
  252. }