Converting HSV Color to RGBThe qbasic example programs for creating TGA images use the following subroutine to convert from real-valued HSV (hue, saturation, value) colors to byte-valued RGB (red, green, blue) colors. Hue is defined as an angle from 0 up to but not including 360 degrees. Saturation and value both lie between 0 and 1. |
SUB hsv2rgb (h AS SINGLE, s AS SINGLE, v AS SINGLE, c AS TGARGB)
DIM r AS SINGLE
DIM g AS SINGLE
DIM b AS SINGLE
DIM p AS SINGLE
DIM q AS SINGLE
DIM f AS SINGLE
DIM t AS SINGLE
DIM i AS INTEGER
IF s = 0 THEN
c.red = CHR$(INT(v * 255))
c.green = c.red
c.blue = c.red
EXIT SUB
END IF
h = h / 60
i = INT(h)
f = h - i
p = v * (1 - s)
q = v * (1 - (s * f))
t = v * (1 - (s * (1 - f)))
SELECT CASE i
CASE 0: r = v: g = t: b = p
CASE 1: r = q: g = v: b = p
CASE 2: r = p: g = v: b = t
CASE 3: r = p: g = q: b = v
CASE 4: r = t: g = p: b = v
CASE 5: r = v: g = p: b = q
END SELECT
c.red = CHR$(INT(r * 255))
c.green = CHR$(INT(g * 255))
c.blue = CHR$(INT(b * 255))
END SUB