Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create rgb colors from a range of HSB values

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading