Cleaning up my web space I found an old code to convert HSL triplets (Hue Saturation Lightness) to RGB (Red Blue Green). I think the code comes from the Gimp source code and was ported to Perl, but it’s so simple I think you can port easily everywhere.
So, why do you need to convert HSL to RGB? HSL values are more human understandable, so there a good choice for a color picket or something similar. But most graphic application require RGB values which are mostly used by computers.
And here is the code:
hsl_to_rgb($h, $s, $l); # Values in 0-255 range sub hsl_to_rgb { my $h = $_[0]/255; my $s = $_[1]/255; my $l = $_[2]/255; my $r, $g, $b; if ($h<1/6) { $r = 1; $g = 6*$h; $b = 0; $g = $g*$s + 1 - $s; $b = $b*$s + 1 - $s; $r = $r*$l; $g = $g*$l; $b = $b*$l; } elsif ($h<1/3) { $r = 1-6*($h - 1/6); $g = 1; $b = 0; $r = $r*$s + 1 - $s; $b = $b*$s + 1 - $s; $r = $r*$l; $g = $g*$l; $b = $b*$l; } elsif ($h<1/2) { $r = 0; $g = 1; $b = 6*($h - 1/3); $r = $r*$s + 1 - $s; $b = $b*$s + 1 - $s; $r = $r*$l; $g = $g*$l; $b = $b*$l; } elsif ($h<2/3) { $r = 0; $g = 1-6*($h - 1/2); $b = 1; $r = $r*$s + 1 - $s; $g = $g*$s + 1 - $s; $r = $r*$l; $g = $g*$l; $b = $b*$l; } elsif ($h<5/6) { $r = 6*($h - 2/3); $g = 0; $b = 1; $r = $r*$s + 1 - $s; $g = $g*$s + 1 - $s; $r = $r*$l; $g = $g*$l; $b = $b*$l; } else { $r = 1; $g = 0; $b = 1-6*($h - 5/6); $g = $g*$s + 1 - $s; $b = $b*$s + 1 - $s; $r = $r*$l; $g = $g*$l; $b = $b*$l; } $r = int $r * 255; $g = int $g * 255; $b = int $b * 255; return {r => $r, g => $g, b => $b); }
Enjoy!