views:

62

answers:

3

I have a 8-bit color palette, therefore I am having 256 colors.

The palette is basically an array of [256 * 3] {r, g, b} values, Now I need to draw a color spectrum out of it. So basically I have to select 256 out of total 256*256*256 values possible which would enable me to draw the rainbow as closely as possible.

Similar questions here on SO point to HSV based approach, but I am looking for an RGB implementation coz I have APIs defined that way.

Any help is much appreciated.

[EDIT] : I need sth close to image below.

alt text

+1  A: 

It really is easiest to use HSV, because that's what you'll end up implementing anyway. Keep S and V fixed (both at 1) and let H vary from 0° to 360°.

The recipe for converting HSV to RGB is described on Wikipedia.

Thomas
vary from 0 to 360? I just have 256 colors?
Neeraj
Hue is often measured in degrees, on the colour wheel. You can use 0..255, or 0..1, or 0%..100%, as long as you normalize the equations accordingly.
Thomas
+2  A: 

Create a simple linear gradient of the Hue channel in HSV, and convert the values to RGB using this code

Here are a bunch of formulas for your interest.

If you're using Qt by any chance than QColor already implements this in toRgb()

Notice that the Hue color space begin and end in the same color (red) so if you want a true rainbow that doesn't repeat colors you'll probably need to take a subset of the full range of Hue values.

shoosh
+3  A: 

The HSV solution is still correct, because that pretty much captures your problem. A "rainbow" is by definition a series of colors with constant S and V, but varying H.

MSalters
Yup use the right color model which does what you want, and then convert.
Brian R. Bondy
can you elaborate how I can vary the hue from 0-360 to generate 256 colors
Neeraj
"Hue" is not necessarily an integer. Store it in a `double`, and increment it in steps of (360.0/256.0). You'll of course end up with `double` values of R,G and B. Round them when you put the color in the palette, not earlier.
MSalters