xmlgraphics-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jerem...@apache.org
Subject svn commit: r1045045 - in /xmlgraphics/commons/branches/Temp_Color: src/java/org/apache/xmlgraphics/java2d/color/ src/java/org/apache/xmlgraphics/ps/ test/java/org/apache/xmlgraphics/java2d/color/
Date Mon, 13 Dec 2010 09:48:17 GMT
Author: jeremias
Date: Mon Dec 13 09:48:17 2010
New Revision: 1045045

URL: http://svn.apache.org/viewvc?rev=1045045&view=rev
Log:
Leave Color.equals() alone and implement same-ness check in ColorUtil.isSameColor(Color, Color).

Modified:
    xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java
    xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternatives.java
    xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/ps/PSState.java
    xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternativesTest.java

Modified: xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java?rev=1045045&r1=1045044&r2=1045045&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java
(original)
+++ xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorUtil.java
Mon Dec 13 09:48:17 2010
@@ -100,4 +100,82 @@ public final class ColorUtil {
         return new Color(srgb[0], srgb[1], srgb[2], alpha);
     }
 
+    /**
+     * Checks if two colors are the same color. This check is much more restrictive than
+     * {@link Color#equals(Object)} in that it doesn't only check if both colors result in
the
+     * same sRGB value. For example, if two colors not of the same exact class are compared,
+     * they are treated as not the same.
+     * <p>
+     * Note: At the moment, this method only supports {@link Color} and
+     * {@link ColorWithAlternatives} only. Other subclasses of {@link Color} are checked
only using
+     * the {@link Color#equals(Object)} method.
+     * @param col1 the first color
+     * @param col2 the second color
+     * @return true if both colors are the same color
+     */
+    public static boolean isSameColor(Color col1, Color col2) {
+        //Check fallback sRGB values first, then go into details
+        if (!col1.equals(col2)) {
+            return false;
+        }
+
+        //Consider same-ness only between colors of the same class (not subclasses)
+        //but consider a ColorWithAlternatives without alternatives to be the same as a Color.
+        boolean skipClassTest = false;
+        if (col1.getClass() == ColorWithAlternatives.class
+                && !((ColorWithAlternatives)col1).hasAlternativeColors()) {
+            skipClassTest = true;
+        }
+        if (col2.getClass() == ColorWithAlternatives.class
+                && !((ColorWithAlternatives)col2).hasAlternativeColors()) {
+            skipClassTest = true;
+        }
+        if (!skipClassTest && col1.getClass() != col2.getClass()) {
+            return false;
+        }
+
+        //Check color space
+        if (!col1.getColorSpace().equals(col2.getColorSpace())) {
+            return false;
+        }
+
+        //Check native components
+        float[] comps1 = col1.getComponents(null);
+        float[] comps2 = col2.getComponents(null);
+        if (comps1.length != comps2.length) {
+            return false;
+        }
+        for (int i = 0, c = comps1.length; i < c; i++) {
+            if (comps1[i] != comps2[i]) {
+                return false;
+            }
+        }
+
+        //Compare alternative colors, order is relevant
+        if (col1 instanceof ColorWithAlternatives && col2 instanceof ColorWithAlternatives)
{
+            ColorWithAlternatives ca1 = (ColorWithAlternatives)col1;
+            ColorWithAlternatives ca2 = (ColorWithAlternatives)col2;
+            if (ca1.hasAlternativeColors() && !ca2.hasAlternativeColors()) {
+                return false;
+            } else if (!ca1.hasAlternativeColors() && ca2.hasAlternativeColors())
{
+                return false;
+            } if (ca1.hasAlternativeColors()) {
+                Color[] alt1 = ca1.getAlternativeColors();
+                Color[] alt2 = ca2.getAlternativeColors();
+                if (alt1.length != alt2.length) {
+                    return false;
+                }
+                for (int i = 0, c = alt1.length; i < c; i++) {
+                    Color c1 = alt1[i];
+                    Color c2 = alt2[i];
+                    if (!isSameColor(c1, c2)) {
+                        return false;
+                    }
+                }
+            }
+        }
+
+        return true;
+    }
+
 }

Modified: xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternatives.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternatives.java?rev=1045045&r1=1045044&r2=1045045&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternatives.java
(original)
+++ xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternatives.java
Mon Dec 13 09:48:17 2010
@@ -177,64 +177,4 @@ public class ColorWithAlternatives exten
         return null;
     }
 
-    /** {@inheritDoc} */
-    public boolean equals(Object obj) {
-        if (!(obj instanceof Color)) {
-            return false;
-        }
-        Color otherCol = (Color)obj;
-        /* java.awt.Color from Sun does not consistenly convert floats to [0..255]
-        if (getRGB() != otherCol.getRGB()) {
-            return false;
-        }*/
-        if (!getColorSpace().equals(otherCol.getColorSpace())) {
-            return false;
-        }
-        float[] comps = getComponents(null);
-        float[] otherComps = otherCol.getComponents(null);
-        if (comps.length != otherComps.length) {
-            return false;
-        }
-        for (int i = 0, c = comps.length; i < c; i++) {
-            if (comps[i] != otherComps[i]) {
-                return false;
-            }
-        }
-        if (getClass() != obj.getClass() && this.alternativeColors != null) {
-            //We're quite strict here to preserve the additional functionality of this class.
-            //If we don't do this, a renderer may not detect the difference between this
class
-            //and a Color with the same sRGB value but with additional color alternatives
-            //taking precedence in some renderers.
-            return false;
-        }
-        if (getClass() == obj.getClass() ) {
-            ColorWithAlternatives other = (ColorWithAlternatives)obj;
-            if (this.alternativeColors == null && other.alternativeColors != null)
{
-                return false;
-            } else if (this.alternativeColors != null && other.alternativeColors
== null) {
-                return false;
-            }
-            if (this.alternativeColors != null
-                    && this.alternativeColors.length != other.alternativeColors.length)
{
-                return false;
-            }
-            if (this.alternativeColors != null) {
-                for (int i = 0, c = this.alternativeColors.length; i < c; i++) {
-                    Color col1 = this.alternativeColors[i];
-                    Color col2 = other.alternativeColors[i];
-                    if (!col1.equals(col2)) {
-                        return false;
-                    }
-                }
-            }
-        }
-        return true;
-    }
-
-    /** {@inheritDoc} */
-    public int hashCode() {
-        //No special treatment necessary for the alternative colors
-        return super.hashCode();
-    }
-
 }

Modified: xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/ps/PSState.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/ps/PSState.java?rev=1045045&r1=1045044&r2=1045045&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/ps/PSState.java
(original)
+++ xmlgraphics/commons/branches/Temp_Color/src/java/org/apache/xmlgraphics/ps/PSState.java
Mon Dec 13 09:48:17 2010
@@ -25,6 +25,8 @@ import java.io.IOException;
 import java.io.Serializable;
 import java.util.List;
 
+import org.apache.xmlgraphics.java2d.color.ColorUtil;
+
 /**
  * This class holds the current state of the PostScript interpreter.
  *
@@ -184,7 +186,7 @@ public class PSState implements Serializ
      * @return true if the color changed compared to the previous setting
      */
     public boolean useColor(Color value) {
-        if (!color.equals(value)) {
+        if (!ColorUtil.isSameColor(color, value)) {
             color = value;
             return true;
         } else {

Modified: xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternativesTest.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternativesTest.java?rev=1045045&r1=1045044&r2=1045045&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternativesTest.java
(original)
+++ xmlgraphics/commons/branches/Temp_Color/test/java/org/apache/xmlgraphics/java2d/color/ColorWithAlternativesTest.java
Mon Dec 13 09:48:17 2010
@@ -39,9 +39,30 @@ public class ColorWithAlternativesTest e
         Color postgelbLab = lab.toColor(83.25f, 16.45f, 96.89f, 1.0f);
         col1 = new ColorWithAlternatives(255, 204, 0, new Color[] {postgelbLab});
 
-        assertFalse(col1.equals(col2));
-        //Unfortunately, Color.equals() can't detect the color difference!
-        //col2.equals(col1) ===> true
+        //java.awt.Color tests on the sRGB value only
+        assertEquals(col1, col2);
+        assertEquals(col2, col1);
+    }
+
+    public void testSameColor() throws Exception {
+        Color col1 = new ColorWithAlternatives(255, 204, 0, null);
+        Color col2 = new Color(255, 204, 0);
+
+        //No alternatives. Only sRGB counts.
+        assertTrue(ColorUtil.isSameColor(col1, col2));
+
+        CIELabColorSpace lab = ColorSpaces.getCIELabColorSpaceD50();
+        Color postgelbLab = lab.toColor(83.25f, 16.45f, 96.89f, 1.0f);
+        col1 = new ColorWithAlternatives(255, 204, 0, new Color[] {postgelbLab});
+
+        //Same sRGB value but one color with alternatives:
+        assertFalse(ColorUtil.isSameColor(col1, col2));
+
+        //Once the spotcolor naked and once as part of a color with alternatives
+        assertFalse(ColorUtil.isSameColor(postgelbLab, col1));
+
+        //sRGB values is calculated from Lab color and doesn't exactly match the selected
fallback
+        assertFalse(postgelbLab.equals(col1));
     }
 
 }



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


Mime
View raw message