Well RGB LED's are fun, so I've been playing with one of those lately. I got one sitting on a breadboard at the moment, lighting up a crystal above it. It looks quite nice cycling colors in various modes, and will be a little desklamp thingy soon
Anyway, now I've tried OP's algorithm, which of course worked much better then my previous one (though mine is more of a rainbow spectrum, or at least it was supposed to be, and this one is a color wheel starting and ending in red).
But I also wanted to adjust the light/dark-ness of it. However I could simply not get the HSL -> RGB algorithm to work, from this page: http://www.easyrgb.com/index.php?X=MATH&H=19#text19
So I modifyed the HSV -> RGB function instead, making it into a sort of HSVL -> RGB function. Though V is not an argument, it is made dependent of the float variable "L". I just added a little snippet for lightness (the L float variable, from 0 to 1).
So when the argument L=0.5, the colors are fully saturated, like before. Less than 0.5 and they are darker, above and they are linearly more white until L=1 = white.
Of course, for the color to be as white as possible, one would have to balance the current limiting resistors to the R,G and B portion of the LED.
Anyway, if anyone is interested this is the (slightly) modified function.
void hsvl2rgb(float H, float L, int& R, int& G, int& B)
{
int var_i;
float S=1, V, lightness, var_1, var_2, var_3, var_h, var_r, var_g, var_b;
V = L * 2; // For the "darkness" of the LED
if ( V > 1 ) V = 1;
if ( S == 0 ) //HSV values = 0 ÷ 1
{
R = V * 255;
G = V * 255;
B = V * 255;
}
else
{
var_h = H * 6;
if ( var_h == 6 ) var_h = 0; //H must be < 1
var_i = int( var_h ) ; //Or ... var_i = floor( var_h )
var_1 = V * ( 1 - S );
var_2 = V * ( 1 - S * ( var_h - var_i ) );
var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );
if ( var_i == 0 ) {
var_r = V ;
var_g = var_3 ;
var_b = var_1 ;
}
else if ( var_i == 1 ) {
var_r = var_2 ;
var_g = V ;
var_b = var_1 ;
}
else if ( var_i == 2 ) {
var_r = var_1 ;
var_g = V ;
var_b = var_3 ;
}
else if ( var_i == 3 ) {
var_r = var_1 ;
var_g = var_2 ;
var_b = V ;
}
else if ( var_i == 4 ) {
var_r = var_3 ;
var_g = var_1 ;
var_b = V ;
}
else {
var_r = V ;
var_g = var_1 ;
var_b = var_2 ;
}
if ( L > 0.5 ) // Adjusting the Lightness (whiteness)
{
lightness = ( L - 0.5 ) / 0.5;
var_r += ( lightness * ( 1 - var_r ) );
var_g += ( lightness * ( 1 - var_g ) );
var_b += ( lightness * ( 1 - var_b ) );
}
R = (1-var_r) * 255; // RGB results = 0 ÷ 255. Reversed for common anode RGB LED's
G = (1-var_g) * 255;
B = (1-var_b) * 255;
}
}