You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by cb...@apache.org on 2010/01/08 14:44:02 UTC
svn commit: r897202 - in
/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d:
ColorConverter.java ColorExt.java ColorUtil.java DefaultColorConverter.java
GrayScaleColorConverter.java
Author: cbowditch
Date: Fri Jan 8 13:44:02 2010
New Revision: 897202
URL: http://svn.apache.org/viewvc?rev=897202&view=rev
Log:
Bugzilla #48405:
Color Converter Interface and Implementations
Submitted by: Peter Hancock <peter.hancock.at.gmail.com>
Added:
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorConverter.java
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorExt.java
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorUtil.java
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/DefaultColorConverter.java
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/GrayScaleColorConverter.java
Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorConverter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorConverter.java?rev=897202&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorConverter.java (added)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorConverter.java Fri Jan 8 13:44:02 2010
@@ -0,0 +1,36 @@
+/*
+ * 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 org.apache.xmlgraphics.java2d;
+
+import java.awt.Color;
+
+/**
+ * Utility for implementing a color conversion scheme.
+ */
+public interface ColorConverter {
+
+ /**
+ * @param color to convert
+ * @return converted color
+ */
+
+ Color convert(Color color);
+
+}
Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorExt.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorExt.java?rev=897202&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorExt.java (added)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorExt.java Fri Jan 8 13:44:02 2010
@@ -0,0 +1,272 @@
+/*
+ * 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: ColorExt.java 884117 2009-11-25 14:42:48Z jeremias $ */
+
+package org.apache.xmlgraphics.java2d;
+
+import java.awt.Color;
+import java.awt.color.ColorSpace;
+import java.util.Arrays;
+
+/**
+ * Color helper class.
+ * <p>
+ * This class extends java.awt.Color class keeping track of the original color
+ * property values specified by the fo user in a rgb-icc call.
+ */
+public final class ColorExt extends Color {
+ //
+ private static final long serialVersionUID = 1L;
+
+ // Values of fop-rgb-icc arguments
+ private float rgbReplacementRed;
+ private float rgbReplacementGreen;
+ private float rgbReplacementBlue;
+
+ private String iccProfileName;
+ private String iccProfileSrc;
+ private ColorSpace colorSpace;
+
+ private float[] colorValues;
+
+ /**
+ * Helper for createFromFoRgbIcc.
+ *
+ * @param colorSpc
+ * ColorSpace of color.
+ * @param colorVals
+ * Color components of color.
+ * @param opacity
+ * Opacity level of color.
+ */
+ private ColorExt(final ColorSpace colorSpc, final float[] colorVals,
+ final float opacity) {
+ super(colorSpc, colorVals, opacity);
+ }
+
+ /**
+ * Helper for createFromSvgIccColor.
+ *
+ * @param red
+ * Red component of color.
+ * @param green
+ * Green component of color.
+ * @param blue
+ * Blue component of color.
+ * @param opacity
+ * Opacity level of color.
+ *
+ */
+ private ColorExt(final float red, final float green, final float blue, final float opacity) {
+ super(red, green, blue, opacity);
+ }
+
+ /**
+ * Create ColorExt object backup up FO's rgb-icc color function.
+ *
+ * @param redReplacement
+ * Red part of RGB replacement color that will be used when ICC
+ * profile can not be loaded
+ * @param greenReplacement
+ * Green part of RGB replacement color that will be used when ICC
+ * profile can not be loaded
+ * @param blueReplacement
+ * Blue part of RGB replacement color that will be used when ICC
+ * profile can not be loaded
+ * @param profileName
+ * Name of ICC profile
+ * @param profileSrc
+ * Source of ICC profile
+ * @param colorSpace
+ * ICC ColorSpace for the ICC profile
+ * @param iccValues
+ * color values
+ * @return the requested color object
+ */
+ public static ColorExt createFromFoRgbIcc( float redReplacement,
+ final float greenReplacement, final float blueReplacement,
+ final String profileName, final String profileSrc,
+ final ColorSpace colorSpace, final float[] iccValues) {
+ ColorExt ce = new ColorExt(colorSpace, iccValues, 1.0f);
+ ce.rgbReplacementRed = redReplacement;
+ ce.rgbReplacementGreen = greenReplacement;
+ ce.rgbReplacementBlue = blueReplacement;
+ ce.iccProfileName = profileName;
+ ce.iccProfileSrc = profileSrc;
+ ce.colorSpace = colorSpace;
+ ce.colorValues = iccValues;
+ return ce;
+ }
+
+ /**
+ * Create ColorExt object backing up SVG's icc-color function.
+ *
+ * @param red
+ * Red value resulting from the conversion from the user provided
+ * (icc) color values to the batik (rgb) color space
+ * @param green
+ * Green value resulting from the conversion from the user
+ * provided (icc) color values to the batik (rgb) color space
+ * @param blue
+ * Blue value resulting from the conversion from the user
+ * provided (icc) color values to the batik (rgb) color space
+ * @param opacity
+ * Opacity
+ * @param profileName
+ * ICC profile name
+ * @param profileHref
+ * the URI to the color profile
+ * @param profileCS
+ * ICC ColorSpace profile
+ * @param colorValues
+ * ICC color values
+ * @return the requested color object
+ */
+ public static ColorExt createFromSvgIccColor(final float red,
+ final float green, final float blue, final float opacity,
+ final String profileName, final String profileHref,
+ final ColorSpace profileCS, final float[] colorValues) {
+ //TODO this method is not referenced by FOP, can it be deleted?
+ ColorExt ce = new ColorExt(red, green, blue, opacity);
+ ce.rgbReplacementRed = -1;
+ ce.rgbReplacementGreen = -1;
+ ce.rgbReplacementBlue = -1;
+ ce.iccProfileName = profileName;
+ ce.iccProfileSrc = profileHref;
+ ce.colorSpace = profileCS;
+ ce.colorValues = colorValues;
+ return ce;
+
+ }
+
+ /** {@inheritDoc} */
+ public int hashCode() {
+ //implementation from the superclass
+ //should be good enough for our purposes
+ return super.hashCode();
+ }
+
+ /** {@inheritDoc} */
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!super.equals(obj)) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ ColorExt other = (ColorExt) obj;
+ //TODO maybe use super.getColorComponents() instead
+ if (!Arrays.equals(colorValues, other.colorValues)) {
+ return false;
+ }
+ if (iccProfileName == null) {
+ if (other.iccProfileName != null) {
+ return false;
+ }
+ } else if (!iccProfileName.equals(other.iccProfileName)) {
+ return false;
+ }
+ if (iccProfileSrc == null) {
+ if (other.iccProfileSrc != null) {
+ return false;
+ }
+ } else if (!iccProfileSrc.equals(other.iccProfileSrc)) {
+ return false;
+ }
+ if (Float.floatToIntBits(rgbReplacementBlue)
+ != Float.floatToIntBits(other.rgbReplacementBlue)) {
+ return false;
+ }
+ if (Float.floatToIntBits(rgbReplacementGreen)
+ != Float.floatToIntBits(other.rgbReplacementGreen)) {
+ return false;
+ }
+ if (Float.floatToIntBits(rgbReplacementRed)
+ != Float.floatToIntBits(other.rgbReplacementRed)) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Get ICC profile name.
+ *
+ * @return ICC profile name
+ */
+ public String getIccProfileName() {
+ return this.iccProfileName;
+ }
+
+ /**
+ * Get ICC profile source.
+ *
+ * @return ICC profile source
+ */
+ public String getIccProfileSrc() {
+ return this.iccProfileSrc;
+ }
+
+ /**
+ * @return the original ColorSpace
+ */
+ public ColorSpace getOrigColorSpace() {
+ //TODO this method is probably unnecessary
+ // due to super.cs and getColorSpace()
+ return this.colorSpace;
+ }
+
+ /**
+ * Returns the original color values.
+ * @return the original color values
+ */
+ public float[] getOriginalColorComponents() {
+ //TODO this method is probably unnecessary
+ // due to super.fvalue and getColorComponents()
+ float[] copy = new float[this.colorValues.length];
+ System.arraycopy(this.colorValues, 0, copy, 0, copy.length);
+ return copy;
+ }
+
+ /**
+ * Create string representation of fop-rgb-icc function call to map this
+ * ColorExt settings.
+ * @return the string representing the internal fop-rgb-icc() function call
+ */
+ public String toFunctionCall() {
+ StringBuffer sb = new StringBuffer(40);
+ sb.append("fop-rgb-icc(");
+ sb.append(this.rgbReplacementRed + ",");
+ sb.append(this.rgbReplacementGreen + ",");
+ sb.append(this.rgbReplacementBlue + ",");
+ sb.append(this.iccProfileName + ",");
+ if (this.iccProfileSrc != null) {
+ sb.append("\"" + this.iccProfileSrc + "\"");
+ }
+ float[] colorComponents = this.getColorComponents(null);
+ for (int ix = 0; ix < colorComponents.length; ix++) {
+ sb.append(",");
+ sb.append(colorComponents[ix]);
+ }
+ sb.append(")");
+ return sb.toString();
+ }
+
+}
Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorUtil.java?rev=897202&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorUtil.java (added)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/ColorUtil.java Fri Jan 8 13:44:02 2010
@@ -0,0 +1,88 @@
+/*
+ * 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: ColorUtil.java 815938 2009-09-16 19:38:13Z jeremias $ */
+
+package org.apache.xmlgraphics.java2d;
+
+import java.awt.Color;
+
+/**
+ * Generic Color helper class.
+ * <p>
+ * This class supports parsing string values into color values and creating
+ * color values for strings. It provides a list of standard color names.
+ */
+public final class ColorUtil {
+
+ /** The name for the uncalibrated CMYK pseudo-profile */
+ public static final String CMYK_PSEUDO_PROFILE = "#CMYK";
+
+ /**
+ * Private constructor since this is an utility class.
+ */
+ private ColorUtil() {
+ }
+
+
+ /**
+ * Lightens up a color for groove, ridge, inset and outset border effects.
+ * @param col the color to lighten up
+ * @param factor factor by which to lighten up (negative values darken the color)
+ * @return the modified color
+ */
+ public static Color lightenColor(Color col, float factor) {
+ // TODO: This function converts the color into the sRGB namespace.
+ // This should be avoided if possible.
+ float[] cols = new float[4];
+ cols = col.getRGBComponents(cols);
+ if (factor > 0) {
+ cols[0] += (1.0 - cols[0]) * factor;
+ cols[1] += (1.0 - cols[1]) * factor;
+ cols[2] += (1.0 - cols[2]) * factor;
+ } else {
+ cols[0] -= cols[0] * -factor;
+ cols[1] -= cols[1] * -factor;
+ cols[2] -= cols[2] * -factor;
+ }
+ return new Color(cols[0], cols[1], cols[2], cols[3]);
+ }
+
+
+
+ /**
+ * Indicates whether the color is a gray value.
+ * @param col the color
+ * @return true if it is a gray value
+ */
+ public static boolean isGray(Color col) {
+ return (col.getRed() == col.getBlue() && col.getRed() == col.getGreen());
+ }
+
+ /**
+ * Creates an uncalibrary CMYK color with the given gray value.
+ * @param black the gray component (0 - 1)
+ * @return the CMYK color
+ */
+ public static Color toCMYKGrayColor(float black) {
+ float[] cmyk = new float[] {0f, 0f, 0f, 1.0f - black};
+ CMYKColorSpace cmykCs = CMYKColorSpace.getInstance();
+ float[] rgb = cmykCs.toRGB(cmyk);
+ return ColorExt.createFromFoRgbIcc(rgb[0], rgb[1], rgb[2],
+ CMYK_PSEUDO_PROFILE, null, cmykCs, cmyk);
+ }
+}
Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/DefaultColorConverter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/DefaultColorConverter.java?rev=897202&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/DefaultColorConverter.java (added)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/DefaultColorConverter.java Fri Jan 8 13:44:02 2010
@@ -0,0 +1,52 @@
+/*
+ * 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 org.apache.xmlgraphics.java2d;
+
+import java.awt.Color;
+
+import org.apache.xmlgraphics.java2d.ColorConverter;
+
+/**
+ * A default implementation that does not apply any conversion
+ */
+public final class DefaultColorConverter implements ColorConverter {
+
+ /**
+ * private constructor to support singleton pattern
+ */
+ private static final DefaultColorConverter SINGLETON = new DefaultColorConverter();
+
+ private DefaultColorConverter() {
+ }
+
+ /**
+ * static factory
+ *
+ * @return singleton instance of DefaultColorConverter
+ */
+ public static DefaultColorConverter getInstance() {
+ return SINGLETON;
+ }
+
+ /** {@inheritDoc} */
+ public Color convert(Color color) {
+ return color;
+ }
+}
Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/GrayScaleColorConverter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/GrayScaleColorConverter.java?rev=897202&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/GrayScaleColorConverter.java (added)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/java2d/GrayScaleColorConverter.java Fri Jan 8 13:44:02 2010
@@ -0,0 +1,59 @@
+/*
+ * 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 org.apache.xmlgraphics.java2d;
+
+import java.awt.Color;
+
+import org.apache.xmlgraphics.java2d.ColorConverter;
+
+/**
+ * Converts to grayscale using the standard RED=30%, GREEN=59% and BLUE=11%
+ * weights (see http://en.wikipedia.org/wiki/Grayscale)
+ */
+public final class GrayScaleColorConverter implements ColorConverter {
+
+ private static final int RED_WEIGHT = 77;
+ private static final int GREEN_WEIGTH = 150;
+ private static final int BLUE_WEIGHT = 28;
+
+ private static final GrayScaleColorConverter SINGLETON = new GrayScaleColorConverter();
+
+ private GrayScaleColorConverter() { }
+
+ /**
+ * static factory
+ *
+ * @return singleton instance of GrayScaleColorConverter
+ */
+ public static GrayScaleColorConverter getInstance() {
+ return SINGLETON;
+ }
+
+ /**
+ * The color is converted to CMYK with just the K component {@inheritDoc}
+ */
+ public Color convert(Color color) {
+
+ float kValue = (RED_WEIGHT * color.getRed() + GREEN_WEIGTH * color.getGreen() + BLUE_WEIGHT
+ * color.getBlue()) / 255.0f / 255.0f;
+
+ return ColorUtil.toCMYKGrayColor(kValue);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org