Very handy snippet nic - thanks
I have changed the code slightly to better demonstrate it, although the function remains intact.
Press H,S or V and move the mouse up or down to change the value.
h as float : s as float : v as float
Colour as Dword
h = rnd(360) : s = rnd(1000) * 0.001 : v = rnd(1000) * 0.001
sync on
sync rate 60
do
cls
if keystate(35)
dec h, mousemovey()
if h > 359 then h = 359
if h < 0 then h = 0
endif
if keystate(31)
dec s, mousemovey() * 0.01
if s > 1 then s = 1
if s < 0 then s = 0
endif
if keystate(47)
dec v, mousemovey() * 0.01
if v > 1 then v = 1
if v < 0 then v = 0
endif
Colour = hsv2dword(h, s , v)
text 0,0, "H = " + str$(h)
text 0,15, "S = " + str$(s)
text 0,30, "V = " + str$(v)
text 0,120,"R = " + str$(rgbr(colour))
text 0,135,"G = " + str$(rgbg(colour))
text 0,150,"B = " + str$(rgbb(colour))
box 0, 50, 50, 100, Colour, Colour, Colour, Colour
sync
loop
end
remstart
h: 0.0 => 360.0
s: 0.0 => 1.0
v: 0.0 => 1.0
returns: Color as DWORD
remend
function hsv2dword(H as float, S as float, V as float)
`Sanitize the input...
if H < 0.0 then H = 0.0 else if H > 360.0 then H = 360.0
if S < 0.0 then S = 0.0 else if S > 1.0 then S = 1.0
if V < 0.0 then V = 0.0 else if V > 1.0 then V = 1.0
`Variable for returned Colour
returnCol as DWORD
`Section of the Hue
Hi as integer : Hi = (H / 60.0) mod 6
`f is a fraction of the section
f as float : f = (H / 60.0) - Hi
`p, q and t are temp variables used to work out the rgb values before we know where to put them
p as float : p = V * (1.0 - S)
q as float : q = V * (1.0 - (f * S))
t as float : t = V * (1.0 - ((1.0 - f) * S))
`Depending on the section worked out above, we store the rgb value appropriately...
R as float : G as float : B as float
select Hi
case 0 : R = V : G = t : B = p : endcase
case 1 : R = q : G = V : B = p : endcase
case 2 : R = p : G = V : B = t : endcase
case 3 : R = p : G = q : B = V : endcase
case 4 : R = t : G = p : B = V : endcase
case 5 : R = V : G = p : B = q : endcase
endselect
`Sanitize the output - just incase of floating point errors...
if R < 0.0 then R = 0.0 else if R > 1.0 then R = 1.0
if G < 0.0 then G = 0.0 else if G > 1.0 then G = 1.0
if B < 0.0 then B = 0.0 else if B > 1.0 then B = 1.0
`Convert to Bytes and then convert to a DWORD using the rgb() function
rByte as byte : rByte = R * 255
gByte as byte : gByte = G * 255
bByte as byte : bByte = B * 255
returnCol = rgb(rByte, gByte, bByte)
endfunction returnCol