You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2010/12/13 12:07:16 UTC

svn commit: r1045063 - in /xmlgraphics/commons/branches/Temp_Color: examples/java/java2d/ps/EPSColorsExample.java src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java

Author: jeremias
Date: Mon Dec 13 11:07:15 2010
New Revision: 1045063

URL: http://svn.apache.org/viewvc?rev=1045063&view=rev
Log:
Added a conversion function from sRGB to CMYK (arbitrary conversion like the one from CMYK to sRGB) to DeviceCMYKColorSpace.
Added an example class that demonstrates color handling of the PSGenerator.

Added:
    xmlgraphics/commons/branches/Temp_Color/examples/java/java2d/ps/EPSColorsExample.java   (with props)
Modified:
    xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java

Added: xmlgraphics/commons/branches/Temp_Color/examples/java/java2d/ps/EPSColorsExample.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_Color/examples/java/java2d/ps/EPSColorsExample.java?rev=1045063&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_Color/examples/java/java2d/ps/EPSColorsExample.java (added)
+++ xmlgraphics/commons/branches/Temp_Color/examples/java/java2d/ps/EPSColorsExample.java Mon Dec 13 11:07:15 2010
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package java2d.ps;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.io.IOUtils;
+
+import org.apache.xmlgraphics.java2d.color.CIELabColorSpace;
+import org.apache.xmlgraphics.java2d.color.ColorSpaces;
+import org.apache.xmlgraphics.java2d.color.DeviceCMYKColorSpace;
+import org.apache.xmlgraphics.java2d.color.NamedColorSpace;
+import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D;
+
+/**
+ * This example demonstrates how colors are handled when generating PostScript/EPS.
+ */
+public class EPSColorsExample {
+
+    /**
+     * Creates an EPS file. The contents are painted using a Graphics2D implementation that
+     * generates an EPS file.
+     * @param outputFile the target file
+     * @throws IOException In case of an I/O error
+     */
+    public static void generateEPSusingJava2D(File outputFile) throws IOException {
+        OutputStream out = new java.io.FileOutputStream(outputFile);
+        out = new java.io.BufferedOutputStream(out);
+        try {
+            //Instantiate the EPSDocumentGraphics2D instance
+            EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
+            g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
+
+            //Set up the document size
+            g2d.setupDocument(out, 400, 200); //400pt x 200pt
+
+            //Paint a bounding box
+            g2d.drawRect(0, 0, 400, 200);
+
+            g2d.setFont(new Font("sans-serif", Font.BOLD, 14));
+            g2d.drawString("Color usage example:", 10, 20);
+            g2d.setFont(new Font("sans-serif", Font.PLAIN, 12));
+            g2d.drawString("RGB", 10, 84);
+            g2d.drawString("CMYK", 60, 84);
+            g2d.drawString("(Lab)", 110, 84);
+            g2d.drawString("(Named)", 160, 84);
+
+            //We're creating a few boxes all filled with some variant of the
+            //"Postgelb" (postal yellow) color as used by Swiss Post.
+
+            Color colRGB = new Color(255, 204, 0);
+            g2d.setColor(colRGB);
+            g2d.fillRect(10, 30, 40, 40);
+
+            //Just convert RGB to CMYK and use that
+            float[] compsRGB = colRGB.getColorComponents(null);
+            DeviceCMYKColorSpace cmykCS = ColorSpaces.getDeviceCMYKColorSpace();
+            float[] compsCMYK = cmykCS.fromRGB(compsRGB);
+            Color colCMYK = DeviceCMYKColorSpace.createCMYKColor(compsCMYK);
+            g2d.setColor(colCMYK);
+            g2d.fillRect(60, 30, 40, 40);
+
+            //Try CIELab (not implemented, yet)
+            CIELabColorSpace d50 = ColorSpaces.getCIELabColorSpaceD50();
+            Color colLab = d50.toColor(83.25f, 16.45f, 96.89f, 1.0f);
+            g2d.setColor(colLab);
+            g2d.fillRect(110, 30, 40, 40);
+
+            //Try named color (Separation, not implemented, yet)
+            float[] c1xyz = d50.toCIEXYZNative(83.25f, 16.45f, 96.89f);
+            NamedColorSpace postgelb = new NamedColorSpace("Postgelb", c1xyz);
+            Color colNamed = new Color(postgelb, new float[] {1.0f}, 1.0f);
+            g2d.setColor(colNamed);
+            g2d.fillRect(160, 30, 40, 40);
+
+            //Cleanup
+            g2d.finish();
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+    }
+
+    /**
+     * Command-line interface
+     * @param args command-line arguments
+     */
+    public static void main(String[] args) {
+        try {
+            File targetDir;
+            if (args.length >= 1) {
+                targetDir = new File(args[0]);
+            } else {
+                targetDir = new File(".");
+            }
+            if (!targetDir.exists()) {
+                System.err.println("Target Directory does not exist: " + targetDir);
+            }
+            generateEPSusingJava2D(new File(targetDir, "eps-example-colors.eps"));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+}

Propchange: xmlgraphics/commons/branches/Temp_Color/examples/java/java2d/ps/EPSColorsExample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/commons/branches/Temp_Color/examples/java/java2d/ps/EPSColorsExample.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java?rev=1045063&r1=1045062&r2=1045063&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java (original)
+++ xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/DeviceCMYKColorSpace.java Mon Dec 13 11:07:15 2010
@@ -43,6 +43,7 @@ public class DeviceCMYKColorSpace extend
     }
 
     /** {@inheritDoc} */
+    @Override
     public float[] toRGB(float[] colorvalue) {
         return new float [] {
             (1 - colorvalue[0]) * (1 - colorvalue[3]),
@@ -51,16 +52,32 @@ public class DeviceCMYKColorSpace extend
     }
 
     /** {@inheritDoc} */
+    @Override
     public float[] fromRGB(float[] rgbvalue) {
-        throw new UnsupportedOperationException("NYI");
+        assert rgbvalue.length == 3;
+        //Note: this is an arbitrary conversion, not a color-managed one!
+        float r = rgbvalue[0];
+        float g = rgbvalue[1];
+        float b = rgbvalue[2];
+        if (r == g && r == b) {
+            return new float[] {0, 0, 0, 1 - r};
+        } else {
+            float c = 1 - r;
+            float m = 1 - g;
+            float y = 1 - b;
+            float k = Math.min(c, Math.min(m, y));
+            return new float[] {c, m, y, k};
+        }
     }
 
     /** {@inheritDoc} */
+    @Override
     public float[] toCIEXYZ(float[] colorvalue) {
         throw new UnsupportedOperationException("NYI");
     }
 
     /** {@inheritDoc} */
+    @Override
     public float[] fromCIEXYZ(float[] colorvalue) {
         throw new UnsupportedOperationException("NYI");
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org