ColorBabel: Hue to RGB, do you copy?

When talking in the color space one should speak lightly.  Illustrated here is a translation from 10 bit hue to discrete 8 bit PWM levels for red, green and blue LED drive on the Arduino platform…
The following code uses a case/switch to determine the operating sextant and set the corresponding color ramp values.  The upper map bounding values may be changed to limit the duty cycle as per the forward voltage characteristic of the LED and the supply voltage in application:

// prevent overflow with modulo 1024
ColorBabel = HueValue % 1024 

// determine the operating sextant using case switch
// and set output accordingly using map command

switch (ColorBabel) {

	case 0 ... 170 :
		Green = map (ColorBabel, 0, 170, 0, 255);
		Red   = 255;
		Blue  = 000;
	break;

	case 171 ... 341 :
		Green = 255;
		Red   = map (ColorBabel, 171, 341, 255, 0);
		Blue  = 000;
	break;

	case 342 ... 511 :
		Green = 255;
		Red   = 000;
		Blue  = map (ColorBabel, 342, 511, 0, 255);
	break;

	case 512 ... 682 :
		Green = map (ColorBabel, 512, 682, 255, 0);
		Red   = 000;
		Blue  = 255;
	break;

	case 683 ... 852 :
		Green = 000;
		Red   = map (ColorBabel, 683, 852, 0, 255);
		Blue  = 255;
	break;	

	case 853 ... 1023 :
		Green = 000;
		Red   = 255;
		Blue  = map (ColorBabel, 853, 1023, 255, 0);
	break;

        default: // Should never arrive here
        break;

}

// set PWM output for each color
analogWrite (GreenLED, Green);
analogWrite (RedLED  , Red  );
analogWrite (BlueLED , Blue );

Testing was performed on a one-off RGB Arduino shield. Utilizing a few resistors and some IRLML2402 logic level N-channel FETs as shown left.  Note the LED is actually common anode, the lower side is being switched and the polarity markings on the star are incorrect.  An external power supply is required as the USB port is limited to 500mA with enumeration…

Leave a Reply

Your email address will not be published. Required fields are marked *