Graphics Reference
In-Depth Information
UNDEFINED is a constant real number outside the interval [0,360].
procedure
RGBToHSL (
real
r, g, b;
ref real
h, s,
)
{
Input:
r, g, b Œ [0,1]
Output:
h Œ [0,360] , s,
Œ [0,1] unless s = 0 in which case h = UNDEFINED
begin
real
max, min, d;
max := Max (r,g,b);
min := Min (r,g,b);
:= (max + min)/2;
{ define the brightness }
if
max = min
then
begin
s := 0; h := UNDEFINED;
end
else
begin
d := max - min;
{ define the saturation }
if
£ 0.5
then
s := d/(max + min)
else
s := d/(2 - max - min);
{ define the hue }
if
r = max
then
h := (g - b)/d;
{ color is between yellow and magenta }
else if
g = max
then
h := 2 + (b - r)/d;
{ color is between cyan and yellow }
else if
b = max
then
h := 4 + (r - g)/d; { color is between magenta and cyan }
h := 60*h;
{ convert to degrees }
if
h < 0
then
h := h + 360;
{ prevent negative values }
end
end
;
Algorithm 8.6.3.
Converting RBG to HSL.