6849805: Nimbus L&F: NimbusLookAndFeel.getDerivedColor() not always returns color2 for 1.0 midPoint
authorpeterz
Fri, 26 Jun 2009 21:43:21 +0400
changeset 3107 51a846c2c16f
parent 3106 bb4a4ea50e55
child 3108 06e84124688c
6849805: Nimbus L&F: NimbusLookAndFeel.getDerivedColor() not always returns color2 for 1.0 midPoint Summary: Different rounding mode used for float->int conversion Reviewed-by: malenkov
jdk/src/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java
jdk/src/share/classes/javax/swing/plaf/nimbus/NimbusLookAndFeel.java
jdk/test/javax/swing/plaf/nimbus/Test6849805.java
--- a/jdk/src/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java	Fri Jun 26 17:15:19 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java	Fri Jun 26 21:43:21 2009 +0400
@@ -31,7 +31,6 @@
 import javax.swing.plaf.UIResource;
 import javax.swing.Painter;
 import java.awt.print.PrinterGraphics;
-import static javax.swing.plaf.nimbus.NimbusLookAndFeel.deriveARGB;
 
 /**
  * Convenient base class for defining Painter instances for rendering a
@@ -347,7 +346,7 @@
      */
     protected final Color decodeColor(Color color1, Color color2,
                                       float midPoint) {
-        return new Color(deriveARGB(color1, color2, midPoint));
+        return new Color(NimbusLookAndFeel.deriveARGB(color1, color2, midPoint));
     }
 
     /**
--- a/jdk/src/share/classes/javax/swing/plaf/nimbus/NimbusLookAndFeel.java	Fri Jun 26 17:15:19 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/plaf/nimbus/NimbusLookAndFeel.java	Fri Jun 26 21:43:21 2009 +0400
@@ -436,15 +436,13 @@
      */
     static int deriveARGB(Color color1, Color color2, float midPoint) {
         int r = color1.getRed() +
-                (int) ((color2.getRed() - color1.getRed()) * midPoint + 0.5f);
+                Math.round((color2.getRed() - color1.getRed()) * midPoint);
         int g = color1.getGreen() +
-                (int) ((color2.getGreen() - color1.getGreen()) * midPoint +
-                        0.5f);
+                Math.round((color2.getGreen() - color1.getGreen()) * midPoint);
         int b = color1.getBlue() +
-                (int) ((color2.getBlue() - color1.getBlue()) * midPoint + 0.5f);
+                Math.round((color2.getBlue() - color1.getBlue()) * midPoint);
         int a = color1.getAlpha() +
-                (int) ((color2.getAlpha() - color1.getAlpha()) * midPoint +
-                        0.5f);
+                Math.round((color2.getAlpha() - color1.getAlpha()) * midPoint);
         return ((a & 0xFF) << 24) |
                 ((r & 0xFF) << 16) |
                 ((g & 0xFF) << 8) |
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/plaf/nimbus/Test6849805.java	Fri Jun 26 21:43:21 2009 +0400
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+   @bug 6849805
+   @summary Tests NimbusLookAndFeel.deriveColor()
+   @author Peter Zhelezniakov
+   @run main Test6849805
+*/
+
+import java.awt.Color;
+
+
+public class Test6849805 {
+
+    static boolean pass = true;
+
+    static class Minimbus extends javax.swing.plaf.nimbus.NimbusLookAndFeel {
+
+        public void test(Color c1, Color c2, float f) {
+            Color r = getDerivedColor(c1, c2, f);
+            Color test = (f > 0 ? c2 : c1);
+            System.out.printf("Got %s, need %s ", r, test);
+
+            if (r.getRGB() == test.getRGB() &&
+                r.getAlpha() == test.getAlpha()) {
+
+                System.out.println("Ok");
+            } else {
+                System.out.println("FAIL");
+                pass = false;
+            }
+        }
+    }
+
+    public static void main(String[] args) {
+        Minimbus laf = new Minimbus();
+        laf.test(Color.WHITE, Color.BLACK, 0f);
+        laf.test(Color.WHITE, Color.BLACK, 1f);
+        laf.test(Color.BLACK, Color.WHITE, 0f);
+        laf.test(Color.BLACK, Color.WHITE, 1f);
+        laf.test(Color.RED, Color.GREEN, 0f);
+        laf.test(Color.RED, Color.GREEN, 1f);
+        laf.test(new Color(127, 127, 127), new Color(51, 151, 212), 0f);
+        laf.test(new Color(127, 127, 127), new Color(51, 151, 212), 1f);
+        laf.test(new Color(221, 63, 189), new Color(112, 200, 89), 0f);
+        laf.test(new Color(221, 63, 189), new Color(112, 200, 89), 1f);
+
+        if (! pass) {
+            throw new RuntimeException("Some testcases failed, see above");
+        }
+    }
+}