Home > front end >  How to create rgb colors from a range of HSB values
How to create rgb colors from a range of HSB values

Time:10-15

I am trying to create a RGB color pallete from HSB values (one color for each degree) in a Java SWING app. My problem is that Color.HSBtoRGB() returns the same value for every degree. I do not understand it. I searched the Internet and tried a number of examples that do not seem to work on my system. Anyway, here is a simple working example of my problem:

import java.awt.Color;

public class Main {
    public static void main(String[] args) {

    int rgb, red, green, blue;
    for (int i = 0; i < 360; i  ) {
        rgb = Color.HSBtoRGB((float)i, 1.0f, 1.0f);
        red = (rgb >> 16) &0xFF;
        green = (rgb >> 8) &0xFF;
        blue = rgb &0xFF;
        System.out.format("%d %x %d %d %d \n", i, rgb, red, green, blue);
       }
    }
}

My output looks like this

0 ffff0000 255 0 0 
1 ffff0000 255 0 0 
2 ffff0000 255 0 0 
3 ffff0000 255 0 0 
4 ffff0000 255 0 0 

All 360 colors are RED. What am I doing wrong here?

CodePudding user response:

According to documentation, hue value is a floating point number between 0 and 1 so you have to divide i by 360.

rgb = Color.HSBtoRGB((float)i / 360, 1.0f, 1.0f);
  • Related