Convert RGB to HSV
James Hurley
jhurley0305 at sbcglobal.net
Sun Jul 5 00:51:38 EDT 2009
What I have been using are the following two functions. I believe they
originated with Monte Goulding. (With a slight addition to allow for a
single parameter input containing the rgb values.)
They have been VERY useful. There are times when HSV is critical.
(Of course neither is useful in getting the RGB--or HSV--values from
the color names.)
Jim Hurley
function RGBtoHSV r, g, b
if the paramcount is 1 then
put item 3 of r into b
put item 2 of r into g
put item 1 of r into r
end if
local maxv, minv, diff, s, rc, gc, bc, h
set the numberFormat to "0.###############"
put r / 255 into r
put g / 255 into g
put b / 255 into b
put max(r,g,b) into maxv
put min(r,g,b) into minv
put maxv - minv into diff
if maxv <> 0 and diff <> 0 then
put diff / maxv into s
put (maxv - r) / diff into rc
put (maxv - g) / diff into gc
put (maxv - b) / diff into bc
if r = maxv then put bc - gc into h
else if g = maxv then put 2 + rc - bc into h
else if b = maxv then put 4 + gc - rc into h
multiply h by 60
if h < 0 then
add 360 to h
end if
else
put 0 into s
put 0 into h
end if
return round(h),round(s * 100),round(maxv * 100)
end RGBtoHSV
function HSVtoRGB h, s, v
if the paramcount is 1 then
put item 3 of h into v
put item 2 of h into s
put item 1 of h into h
end if
local rgb, i, f, p, q, t
set the numberFormat to "0.###############"
divide s by 100
divide v by 100
if s is 0 then put v,v,v into rgb
else
divide h by 60
put trunc(h) into i
put h - i into f
put v * (1 - s) into p
put v * (1 - s * f) into q
put v * (1 - s * (1- f)) into t
if i is 0 then put v,t,p into rgb
if i is 1 then put q,v,p into rgb
if i is 2 then put p,v,t into rgb
if i is 3 then put p,q,v into rgb
if i is 4 then put t,p,v into rgb
if i is 5 then put v,p,q into rgb
end if
return round(item 1 of rgb * 255), round(item 2 of rgb * 255),
round(item 3 of rgb * 255)
end HSVtoRGB
More information about the use-livecode
mailing list