Search code examples
swingnimbus

Decode a nimbus private class `DerivedColor` into correct ARGB values


I am developing custom components which fit into the Nimbus look and feel. I'm running into a problem with the color defaults which, as with "nimbusBlueGrey" in Java 1.6 return instances of com.sun.java.swing.plaf.nimbus.DerivedColor.

Since this is a private class and it would anyway change in Java 7, I need to treat this like an instance of java.awt.Color. But when I try to mix the color, e.g. adjust hue, saturation, and brightness, these evil objects return wrong RGB colors. The toString output indicates this problem:

DerivedColor(color=50,50,50 parent=nimbusBase offsets=0.03245944,-0.525188,0.196078,0)

So I want to be able to read this as ARGB 0xffa9b0be which would be correct (cf. Nimbus Defaults) -- but what I get from getRGB is that useless 0x00323232.


Solution

  • The problem occurs when Nimbus is not the current look and feel. It seems that DerivedColor looks up the current look and feel and then fails to decode the parent color.

    So the solution is to use copies of the default colors for the case that Nimbus is not set.