0

I am using freeglut to draw some shapes on screen, and wish to adjust their colours based on the values associated with each of the objects. The values range from 0.0 to 1.0, and I want these to represent bright red to violet, with numbers linearly scaling through the visible spectrum.

Problem is, I need to represent these in RGB color codes (0.0 to 1.0 for each). Is there some simple way of achieving this?

quant
  • 21,507
  • 32
  • 115
  • 211
  • 3
    RGB is not a good colour space to do this in. I would look at working in [HSL or HSV](http://en.wikipedia.org/wiki/HSL_and_HSV), which have a "hue" axis, and then converting back to RGB when necessary. – Oliver Charlesworth Jan 05 '14 at 11:47
  • You could do that easily using HSL colors, iterating over the color chanel, and translating it to RGB – Manu343726 Jan 05 '14 at 11:47
  • The main problem you'll encounter is, that RGB doesn't cover a large portion of the visible color space. If you really want to represent the whole visible spectrum you'll have to work in a wide gamut color space, or better yet in a contact color space. But be warned that *no* display device in widely available is able to represent all visible colors; technically it could be done with existing technology using video projectors that use more than 3 colors in their color filter wheel. – datenwolf Jan 05 '14 at 12:30
  • 1
    here is mine better wavelength -> RGB conversion: http://stackoverflow.com/a/22681410/2521214 – Spektre Apr 28 '14 at 18:01

1 Answers1

1

Thanks for the comments, I didn't know about HSV. I found the following online:

void pSetHSV( float h, float s, float v ) {
    // H [0, 360] S and V [0.0, 1.0].
    int i = (int)floor(h/60.0f) % 6;
    float f = h/60.0f - floor(h/60.0f);
    float p = v * (float)(1 - s);
    float q = v * (float)(1 - s * f);
    float t = v * (float)(1 - (1 - f) * s);
    switch (i) {
        case 0: glColor3f(v, t, p);
        break;
        case 1: glColor3f(q, v, p);
        break;
        case 2: glColor3f(p, v, t);
        break;
        case 3: glColor3f(p, q, v);
        break;
        case 4: glColor3f(t, p, v);
        break;
        case 5: glColor3f(v, p, q);
    }
}

Source: http://forum.openframeworks.cc/t/hsv-color-setting/770

quant
  • 21,507
  • 32
  • 115
  • 211
  • Why wouldnt the regular RGB space work ? Something like http://www.astrouw.edu.pl/~jskowron/colors-x11/rgb.html ? – Prabindh Jan 05 '14 at 13:04