0

Apps like Photoshop have a single slider to select color: enter image description here.

I'd like to achieve the same using variable resistance knob as a slider to control color of RGB LED. Can anyone put me on the right track how to linearly map color values?

4
  • 1
    Use HSV like Majenko said. But you might also like to look at adding gamma correction to those RGB values. Our eyes don't perceive differences in brightness linearly. – Gerben May 5 '20 at 14:59
  • Any advise how to perform this? I was not going to adjust brightness or saturation and just go with max values. – wilkas May 6 '20 at 11:15
  • 1
    Adafruit has a great explanation and guide for this at learn.adafruit.com/led-tricks-gamma-correction/the-issue – Gerben May 7 '20 at 13:41
  • Thanks, gamma correction quick fix was so easy to implement and improved the look, especially after implementing pulsing fade – wilkas May 17 '20 at 3:41
2

What you are controlling there is one out of three values from the HSV (aka HSB) colour space.

Colour can be represented in a number of different ways, called "spaces". You computer monitor displays colour in the RGB colour space - red, green and blue. An RGB LED of course also displays in the RGB colour space. Your colour printer works in the CMYK colour space.

Different colour spaces represent the colour using different values and combinations of values, and which colour space you use may be imposed by technical limitations (such as in the printer it is dictated by the combination of the colours of the inks).

HSV is a convenient way of visually manipulating colour since it gives you easy to understand "Hue" (the colour), "Saturation" (how much colour) and "Value" (or "Brightness" - how light the colour is) values that are easy for a human to understand.

However physical devices don't generally work with that colour space, so you need to be able to translate between them.

You can think of HSV as the "Human" colour space, and RGB as the "Digital" colour space.

Fortunately it's relatively simple to convert from one to another and there are a number of code snippets online that give examples of how to do it.

I'm going to include one here that I use in my chipKIT TFT display library "Cariad". Given three 8 bit values (0-255) of "hue" "sat" and "val" it will calculate the red, green and blue values. "hue" would be the one you change with your pot - the others can be static values (or you could add more pots to control those too).

    unsigned int H_accent = hue/43;
    unsigned int bottom = ((255 - sat) * val)>>8;
    unsigned int top = val;
    unsigned char rising  = ((top-bottom)  *(hue%43   )  )  /  43  +  bottom;
    unsigned char falling = ((top-bottom)  *(43-hue%43)  )  /  43  +  bottom;

    int r = 0;
    int g = 0;
    int b = 0;

    switch(H_accent) {
        case 0:
                r = top;
                g = rising;
                b = bottom;
        break;

        case 1:
                r = falling;
                g = top;
                b = bottom;
        break;

        case 2:
                r = bottom;
                g = top;
                b = rising;
        break;

        case 3:
                r = bottom;
                g = falling;
                b = top;
        break;

        case 4:
                r = rising;
                g = bottom;
                b = top;
        break;

        case 5:
                r = top;
                g = bottom;
                b = falling;
        break;
    }
    if (r > 255) r = 255;
    if (g > 255) g = 255;
    if (b > 255) b = 255;
    if (r < 0) r = 0;
    if (g < 0) g = 0;
    if (b < 0) b = 0;
4
  • What are the ranges for "hue" "sat" and "val" values in this script? Is it 0-128? – wilkas May 5 '20 at 10:33
  • 1
    0-255 full 8 bit range – Majenko May 5 '20 at 10:36
  • Works like a charm. I wander why colors look different while Arduino board is powered via USB and via power supply connection – wilkas May 9 '20 at 9:56
  • 1
    Because of the varying forward voltages of the three LEDs and the differing incoming supply voltage. – Majenko May 9 '20 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.