Graphics Reference
In-Depth Information
8.6
Transforming Between Color Models
Finally, one needs transformations to convert between the various color models. Algo-
rithms 8.6.1 and 8.6.2 describe the conversions between RGB and HSV. Algorithms
8.6.3 and 8.6.4 convert between RGB and HSL. Algorithm 8.6.4, which converts HSL
to RGB, is the one described in [Fish90a]. It basically converts HSL to HSV and then
uses the steps in the HSV to RGB conversion algorithm. An undefined hue in these
algorithms means that the color is achromatic and corresponds to a shade of gray
UNDEFINED is a constant real number outside the interval [0,360].
procedure RGBToHSV ( real r, g, b; ref real h, s, v)
{
Input:
r, g, b Π[0,1]
Output:
h Π[0,360] , s, v Π[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);
v := max;
{ define the value }
if max = 0
{ define the saturation }
then s := 0
else s := (max - min)/max;
if s = 0
then h := UNDEFINED
else
{ the saturation is not zero (the chromatic case) }
begin
d := max - min;
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.1.
Converting RBG to HSV.
Search WWH ::




Custom Search