Search code examples
javaawtrgbhsb

Creating Java Color objects using HSB values


I am trying to use create a Color object using HSB value but I am having some trouble.

for(int i = 0; i<255; i++)
{
   Color c = Color.getHSBColor(i,100,100);
   System.out.println(c);
}

I would expect this to rotate through all hues, but instead every color has the same RGB value of RGB(251,251,2)

The output is

java.awt.Color[r=251,g=251,b=2]
java.awt.Color[r=251,g=251,b=2]
java.awt.Color[r=251,g=251,b=2]
....

Any idea what I am doing wrong? Thanks


Solution

  • From the javadocs of Color#getHSBColor(float, float, float):

    The <code>s</code> and <code>b</code> components should be
    floating-point values between zero and one
    (numbers in the range 0.0-1.0).  The <code>h</code> component
    can be any floating-point number.  The floor of this number is
    subtracted from it to create a fraction between 0 and 1.
    

    In other words, the values don't range from 0 - 255, but from 0.0 - 1.0.