From: Jeremias Maerki Date: Tue, 29 Jun 2010 16:52:06 +0000 (+0000) Subject: Round-trip for fop-rgb-named-color() function. X-Git-Tag: fop-1_1rc1old~290^2~26 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fdb20deabe93eade9c6903b98904e19de2ae49c2;p=xmlgraphics-fop.git Round-trip for fop-rgb-named-color() function. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_Color@959028 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/util/ColorUtil.java b/src/java/org/apache/fop/util/ColorUtil.java index 983b3c421..71c97f84e 100644 --- a/src/java/org/apache/fop/util/ColorUtil.java +++ b/src/java/org/apache/fop/util/ColorUtil.java @@ -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) { diff --git a/test/java/org/apache/fop/util/ColorUtilTestCase.java b/test/java/org/apache/fop/util/ColorUtilTestCase.java index 82fe841fc..26876e581 100644 --- a/test/java/org/apache/fop/util/ColorUtilTestCase.java +++ b/test/java/org/apache/fop/util/ColorUtilTestCase.java @@ -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 index 000000000..7afb2d8fc Binary files /dev/null and b/test/resources/color/ncp-example.icc differ