JDK-6849805 : Nimbus L&F: NimbusLookAndFeel.getDerivedColor() not always returns color2 for 1.0 midPoint
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2009-06-10
  • Updated: 2012-03-22
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 Other
7 b64Fixed OpenJDK6Fixed
Related Reports
Duplicate :  
Description
The spec for javax.swing.plaf.nimbus.NimbusLookAndFeel.getDerivedColor(Color color1, Color color2, float midPoint) says:

     * @param midPoint The offset between color 1 and color 2, a value of 0.0 is
     *                 color 1 and 1.0 is color 2;

Exact second color is not always returned for 1.0 offset value.

Please see the following code sample:

------------------------------------------------------------------------------------
import java.awt.*;

public class Test {
    public static void main(String[] args) {
        class MyNibusLAF extends javax.swing.plaf.nimbus.NimbusLookAndFeel {
            public Color callGetDerivedColor(Color c1, Color c2, float midPoint) {
                return getDerivedColor(c1, c2, midPoint);
            }
        }
        Color secondColor = Color.BLACK;
        Color returnedColor = new MyNibusLAF().callGetDerivedColor(Color.WHITE, secondColor, 1.f);
        System.out.println("returnedColor = " + returnedColor);
        System.out.println("secondColor = " + secondColor);
    }
}
------------------------------------------------------------------------------------

Output will look:

returnedColor = javax.swing.plaf.ColorUIResource[r=1,g=1,b=1]
secondColor = java.awt.Color[r=0,g=0,b=0]


Color RGB components are not as expected.

Comments
EVALUATION I believe this is caused by wrong rounding mode. Conversion to (int) rounds towards zero, whereas we should be rounding towards negative infinity. Math.round() does exactly what we need here.
26-06-2009