Hey,
This was a request for help fixing my code but I've been able to fix it myself.
So instead, below you will find the functions required for conversion between RGB and HSV (and back).
Enjoy
main:
SetWindowSize(640, 480, 0)
HSV as tHSV
RGB as tRGB
sourceRGB as tRGB
sourceRGB = CreateColor( 50, 100, 200, 255 )
RGBtoHSV( sourceRGB.r, sourceRGB.g, sourceRGB.b, HSV )
HSVtoRGB( HSV.h, HSV.s, HSV.v, RGB )
do
print("sR: " + str(sourceRGB.r))
print("sG: " + str(sourceRGB.g))
print("sB: " + str(sourceRGB.b))
drawbox( 12, 0, 50, 10, sourceRGB.c, sourceRGB.c, sourceRGB.c, sourceRGB.c, 1 )
print("")
print("H : " + str(HSV.h))
print("S : " + str(HSV.s))
print("V : " + str(HSV.v))
Print("")
print("rR: " + str(RGB.r))
print("rG: " + str(RGB.g))
print("rB: " + str(RGB.b))
drawbox( 12, 26, 50, 36, RGB.c, RGB.c, RGB.c, RGB.c, 1 )
sync()
loop
RGB->HSV:
////////////////////////
// RGB to HSV
function RGBtoHSV( r as float, g as float, b as float, HSV REF as tHSV )
min# = fMin3( r, g, b )
max# = fMax3( r, g, b )
HSV.v = max# / 255
delta# = max# - min#
if max# = 0
HSV.h = 0 : HSV.s = 0: HSV.v = 0: exitfunction
endif
if (max#=255) and (min#=255)
HSV.h = 0 : HSV.s = 0: HSV.v = 1: exitfunction
endif
HSV.s = delta#/max#
select max#
case r: HSV.h = (g-b)/delta#: endcase
case g: HSV.h = 2 + (b-r)/delta#: endcase
case b: HSV.h = 4 + (r-g)/delta#: endcase
endselect
HSV.h = fWrap(HSV.h*60, 0.0, 360.0)
endfunction
HSV->RGB:
////////////////////////
// HSV to RGB
function HSVtoRGB( h as float, s as float, v as float, RGB REF as tRGB )
h = fClamp(h, 0.0, 360.0) / 60.0
s = fClamp(s, 0.0, 1.0)
v = fClamp(v, 0.0, 1.0)
i = floor(h)
f# = h - i
p# = v * (1.0 - s)
q# = v * (1.0 - s * f#)
t# = v * (1.0 - s * (1.0 - f#) )
select i
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
RGB.r = fClamp(r#, 0.0, 1.0) * 255
RGB.g = fClamp(g#, 0.0, 1.0) * 255
RGB.b = fClamp(b#, 0.0, 1.0) * 255
RGB.c = MakeColor(RGB.r, RGB.g, RGB.b)
endfunction
UDTs:
type tHSV
h as float
s as float
v as float
endtype
type tRGB
r as integer
g as integer
b as integer
a as integer
c as integer
endtype
Helper functions:
////////////////////////
// Make Colour
function CreateColor( r as float, g as float, b as float, a as float )
ret as tRGB
ret.r = r
ret.g = g
ret.b = b
ret.a = a
ret.c = MakeColor( r,g,b )
endfunction ret
////////////////////////
// WRAP
function fWrap( value as float, first as float, last as float )
if value < first
newValue# = last - (first - value)
exitfunction newValue#
endif
if value > last
newValue# = first + (value - last)
exitfunction newValue#
endif
endfunction value
////////////////////////
// MAX
function fMax3( a#, b#, c# )
if (a#>b#) and (a#>c#) then exitfunction a#
if b#>c# then exitfunction b#
endfunction c#
////////////////////////
// MIN
function fMin3( a#, b#, c# )
if (a#<b#) and (a#<c#) then exitfunction a#
if b#<c# then exitfunction b#
endfunction c#
////////////////////////
// CLAMP
function fClamp( v as float, minv as float, maxv as float)
if v > maxv then exitfunction maxv
if v < minv then exitfunction minv
endfunction v
AGK V2 user - Tier 1 (mostly)