RGB -> HSV Help.
BlitzMax Forums/BlitzMax Programming/RGB -> HSV Help.
| ||
SuperStrict Global HSV_DEBUG:Byte = 1 Local Colour:TColour = New TColour colour.r = 0 colour.g = 0 colour.b = 255 Print rgb2hsv(colour).h+","+rgb2hsv(colour).s+","+rgb2hsv(colour).v Type TColour Field a:Int Field r:Int,g:Int,b:Int Field h:Double,s:Double,v:Double End Type Function rgb2hsv:TColour(in_colour:TColour) Local h:Double,s:Double,v:Double Local r:Double = Double(in_colour.r)/255 Local g:Double = Double(in_colour.g)/255 Local b:Double = Double(in_colour.b)/255 Local minimum:Double = Min(r,Min(g,b)) Local maximum:Double = Max(r,Max(g,b)) Local delta_maximum:Double = maximum-minimum v = maximum If delta_maximum = 0 h = 0 s = 0 Else s = delta_maximum / maximum Local delta_r:Double = (((maximum-r)/6)+(delta_maximum/2))/delta_maximum Local delta_g:Double = (((maximum-g)/6)+(delta_maximum/2))/delta_maximum Local delta_b:Double = (((maximum-b)/6)+(delta_maximum/2))/delta_maximum If r = maximum h = delta_b-delta_g Else If g = maximum h = (1/3)+delta_r-delta_b Else If b = maximum h = (2/3)+delta_g-delta_r End If If (h<0) Then h :+ 1 If (h>1) Then h :- 1 End If Local out_colour:TColour = New TColour out_colour.a = in_colour.a out_colour.h = h out_colour.s = s out_colour.v = v Return out_colour End Function Well I had to translate some code I found into BMax for this conversion as cower.utility is unavailble whilst Noel is banned. There is an inverse function, and also a colourising function, these are not included because they appear to work fine. The problem is that the test code in there returns 0,1,1 (well with .00000s) which is red, not the 0.66666...9 exepected for a blue. Could somebody check through the code and see where I may have gone wrong please? The code is translated from http://www.easyrgb.com/math.php?MATH=M20#text20 EDIT: I forget to mention, my friend ran through them both on paper, and they should work. He's not blitz saavy though so won't know exactly how a command will act given certain things etc. |
| ||
This works: http://www.blitzbasic.com/codearcs/codearcs.php?code=1749 |
| ||
Well we solved it before reading your reply:h = (1/3)+delta_r-delta_b h = (2/3)+delta_g-delta_r These two lines were the problem as the division was being rounded, (double(1)/3), (double(2)/3) fixed teh problem. |