rgb2hsv

BlitzMax Forums/BlitzMax Programming/rgb2hsv

CS_TBL(Posted 2006) [#1]
Trying to port this one: http://www.cs.rit.edu/~ncs/color/t_convert.html .. the other function will follow soon enough, first this one.

So uhm, maybe I'm blind orso, but I don't quite get the point of the result here below not working properly. Observe the H result of 'blue' in the debuglog..

Function RGBtoHSV(r:Float,g:Float,b:Float,h:Float Var,s:Float Var,v:Float Var)

	Local maxc:Float=Max(r,Max(g,b))
	Local minc:Float=Min(r,Min(g,b))
	
	v=maxc
	
	Local delta:Float=maxc-minc
	
	If maxc<>0
		s=delta/maxc
	Else
		s=0
		h=-1
		Return
	EndIf
	
	If r=maxc
		h=(g-b)/delta
	ElseIf g=maxc
		h=2+(b-r)/delta
	Else
		h=4=(r-g)/delta
	EndIf
	
	h:*60
	If h<0 h:+360
	
	DebugLog "--------------------------------"
	DebugLog h+" "+s+" "+v
		

End Function

Local h:Float
Local s:Float
Local v:Float

RGBtoHSV  1, 0, 0,h,s,v
RGBtoHSV  1, 1, 0,h,s,v
RGBtoHSV  0, 1, 0,h,s,v
RGBtoHSV  0, 1, 1,h,s,v
RGBtoHSV  0, 0, 1,h,s,v ' <- blue
RGBtoHSV  1, 0, 1,h,s,v

DebugLog Chr(10)

RGBtoHSV  1,.2,.1,h,s,v
RGBtoHSV .5,.5, 0,h,s,v
RGBtoHSV .1, 1,.2,h,s,v
RGBtoHSV  0,.5,.5,h,s,v
RGBtoHSV .2,.1, 1,h,s,v ' <- blue'ish
RGBtoHSV .5, 0,.5,h,s,v

End



splinux(Posted 2006) [#2]
Try this:
Function RGBtoHSV(r:Float,g:Float,b:Float,h:Float Var,s:Float Var,v:Float Var)
Local mmin:Float=Min(r, Min(g, b))
Local mmax:Float=Max(r, Max(g, b))

v=mmax

Local delta:Float=mmax-mmin

If mmax<>0
s=delta/mmax
Else
s=0
h=-1
Return
EndIf

If r=mmax
h=(g-b)/delta
ElseIf g=mmax
h=2+(b-r)/delta
Else
h=4+(r-g)/delta
EndIf

h=h*60
If h<0 Then h=h+360

DebugLog "--------------------------------"
DebugLog h+" "+s+" "+v


End Function

Local h:Float
Local s:Float
Local v:Float

RGBtoHSV 1, 0, 0,h,s,v
RGBtoHSV 1, 1, 0,h,s,v
RGBtoHSV 0, 1, 0,h,s,v
RGBtoHSV 0, 1, 1,h,s,v
RGBtoHSV 0, 0, 1,h,s,v ' <- blue
RGBtoHSV 1, 0, 1,h,s,v

DebugLog Chr(10)

RGBtoHSV 1,.2,.1,h,s,v
RGBtoHSV .5,.5, 0,h,s,v
RGBtoHSV .1, 1,.2,h,s,v
RGBtoHSV 0,.5,.5,h,s,v
RGBtoHSV .2,.1, 1,h,s,v ' <- blue'ish
RGBtoHSV .5, 0,.5,h,s,v

End


fredborg(Posted 2006) [#3]
Or this: http://www.blitzbasic.com/codearcs/codearcs.php?code=1749


CS_TBL(Posted 2006) [#4]
splinux, that's just spooky, I simply fail to see what the difference is between yours and mine, except some cosmetics.


fredborg(Posted 2006) [#5]
h=4=(r-g)/delta


splinux(Posted 2006) [#6]
Yes.
Mine worked.
I've retranslated it.


CS_TBL(Posted 2006) [#7]
aaarrgh@#&$%^#$%#

*runs to collide with a wall ._.