]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Round-trip for fop-rgb-named-color() function.
authorJeremias Maerki <jeremias@apache.org>
Tue, 29 Jun 2010 16:52:06 +0000 (16:52 +0000)
committerJeremias Maerki <jeremias@apache.org>
Tue, 29 Jun 2010 16:52:06 +0000 (16:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_Color@959028 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/util/ColorUtil.java
test/java/org/apache/fop/util/ColorUtilTestCase.java
test/resources/color/ncp-example.icc [new file with mode: 0644]

index 983b3c42124bcb9fe3ecba98fdbd09cc3d743b75..71c97f84e2c05178647bcd29d03b2654337f106d 100644 (file)
@@ -660,31 +660,50 @@ public final class ColorUtil {
      */
     public static String toFunctionCall(ColorExt color) {
         Color[] alt = color.getAlternateColors();
-        if (alt.length == 0) {
+        ICCColor icc = null;
+        for (int i = 0, c = alt.length; i < c; i++) {
+            if (alt[i] instanceof ICCColor) {
+                //Find first ICCColor in alternatives
+                icc = (ICCColor)alt[i];
+                break;
+            }
+        }
+        if (icc == null) {
             return toRGBFunctionCall(color);
-        } else if (alt.length != 1) {
-            throw new IllegalStateException(
-                    "Cannot convert to function call: the number of alternate colors is not one.");
         }
         StringBuffer sb = new StringBuffer(40);
-        sb.append("fop-rgb-icc(");
+
+        String functionName;
         float[] rgb = color.getColorComponents(null);
         assert rgb.length == 3;
+        sb.append("(");
         sb.append(rgb[0]).append(",");
         sb.append(rgb[1]).append(",");
         sb.append(rgb[2]).append(",");
-        ICCColor icc = (ICCColor)alt[0];
-        sb.append(icc.getColorProfileName()).append(",");
+        String profileName = icc.getColorProfileName();
+        sb.append(profileName).append(",");
         if (icc.getColorProfileSource() != null) {
             sb.append("\"").append(icc.getColorProfileSource()).append("\"");
         }
-        float[] colorComponents = icc.getColorComponents(null);
-        for (int ix = 0; ix < colorComponents.length; ix++) {
-            sb.append(",");
-            sb.append(colorComponents[ix]);
+
+        if (icc.getColorSpace() instanceof NamedColorSpace) {
+            NamedColorSpace ncs = (NamedColorSpace)icc.getColorSpace();
+            if (SEPARATION_PSEUDO_PROFILE.equalsIgnoreCase(profileName)) {
+                functionName = "fop-rgb-icc";
+            } else {
+                functionName = "fop-rgb-named-color";
+            }
+            sb.append(",").append(ncs.getColorName());
+        } else {
+            functionName = "fop-rgb-icc";
+            float[] colorComponents = icc.getColorComponents(null);
+            for (int ix = 0; ix < colorComponents.length; ix++) {
+                sb.append(",");
+                sb.append(colorComponents[ix]);
+            }
         }
         sb.append(")");
-        return sb.toString();
+        return functionName + sb.toString();
     }
 
     private static Color createColor(int r, int g, int b) {
index 82fe841fc5026497b88d75a8502e090fff10e44e..26876e5813e45ec067a25fd9fbcbba71b2e601a5 100644 (file)
@@ -27,6 +27,7 @@ import junit.framework.TestCase;
 
 import org.apache.xmlgraphics.java2d.color.ColorExt;
 import org.apache.xmlgraphics.java2d.color.ColorSpaces;
+import org.apache.xmlgraphics.java2d.color.NamedColorSpace;
 
 import org.apache.fop.apps.FOUserAgent;
 import org.apache.fop.apps.FopFactory;
@@ -124,12 +125,8 @@ public class ColorUtilTestCase extends TestCase {
         String colSpec = "fop-rgb-icc(1.0,0.0,0.0,sRGBAlt,"
             + "\"" + sRGBLoc.toASCIIString() + "\",1.0,0.0,0.0)";
         colActual = (ColorExt)ColorUtil.parseColorString(ua, colSpec);
-        //assertEquals(255, colActual.getRed()); //253 is returned
-        //assertEquals(24, colActual.getGreen()); //24 is returned
-        //I don't understand the difference. Maybe Java's sRGB and HP's sRGB are somehow not
-        //equivalent. This is only going to be a problem if anyone actually makes use of the
-        //RGB fallback in any renderer.
-        //TODO Anyone know what's going on here?
+        assertEquals(255, colActual.getRed());
+        assertEquals(0, colActual.getGreen());
         assertEquals(0, colActual.getBlue());
         assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), colActual.getColorSpace());
         float[] comps = colActual.getColorComponents(null);
@@ -226,4 +223,67 @@ public class ColorUtilTestCase extends TestCase {
                 ColorUtil.colorToString(colActual));
     }
 
+    /**
+     * Tests color for the #Separation pseudo-colorspace.
+     * @throws Exception if an error occurs
+     */
+    public void testSeparationColor() throws Exception {
+        ColorExt colActual;
+        String colSpec;
+
+        colSpec = "fop-rgb-icc(1.0,0.8,0.0,#Separation,,Postgelb)";
+        colActual = (ColorExt)ColorUtil.parseColorString(null, colSpec);
+        assertEquals(255, colActual.getRed());
+        assertEquals(204, colActual.getGreen());
+        assertEquals(0, colActual.getBlue());
+
+        Color alt = colActual.getAlternateColors()[0];
+        assertTrue(alt.getColorSpace() instanceof NamedColorSpace);
+        NamedColorSpace ncs;
+        ncs = (NamedColorSpace)alt.getColorSpace();
+        assertEquals("Postgelb", ncs.getColorName());
+        float[] comps = alt.getColorComponents(null);
+        assertEquals(1, comps.length);
+        assertEquals(1f, comps[0], 0);
+        assertEquals(colSpec, ColorUtil.colorToString(colActual));
+    }
+
+    /**
+     * Tests the fop-rgb-named-color() function.
+     * @throws Exception if an error occurs
+     */
+    public void testNamedColorProfile() throws Exception {
+        FopFactory fopFactory = FopFactory.newInstance();
+        URI ncpLoc = new URI("file:test/resources/color/ncp-example.icc");
+        ColorSpace cs = fopFactory.getColorSpace(null, ncpLoc.toASCIIString());
+        assertNotNull(cs);
+
+        FOUserAgent ua = fopFactory.newFOUserAgent();
+        ColorExt colActual;
+
+        //fop-rgb-named-color() is used instead of rgb-named-color() inside FOP!
+        String colSpec = "fop-rgb-named-color(1.0,0.8,0.0,NCP,"
+            + "\"" + ncpLoc.toASCIIString() + "\",Postgelb)";
+        colActual = (ColorExt)ColorUtil.parseColorString(ua, colSpec);
+        assertEquals(255, colActual.getRed());
+        assertEquals(204, colActual.getGreen());
+        assertEquals(0, colActual.getBlue());
+        assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), colActual.getColorSpace());
+        float[] comps = colActual.getColorComponents(null);
+        assertEquals(3, comps.length);
+        assertEquals(1f, comps[0], 0);
+        assertEquals(0.8f, comps[1], 0);
+        assertEquals(0f, comps[2], 0);
+
+        Color alt = colActual.getAlternateColors()[0];
+        assertTrue(alt.getColorSpace() instanceof NamedColorSpace);
+        NamedColorSpace ncs;
+        ncs = (NamedColorSpace)alt.getColorSpace();
+        assertEquals("Postgelb", ncs.getColorName());
+        comps = alt.getColorComponents(null);
+        assertEquals(1, comps.length);
+        assertEquals(1f, comps[0], 0);
+
+        assertEquals(colSpec, ColorUtil.colorToString(colActual));
+    }
 }
diff --git a/test/resources/color/ncp-example.icc b/test/resources/color/ncp-example.icc
new file mode 100644 (file)
index 0000000..7afb2d8
Binary files /dev/null and b/test/resources/color/ncp-example.icc differ