HSV Colorspace

Source-code only - please, don't post questions here.
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

HSV Colorspace

Postby Antoni » Jul 02, 2007 8:39

Sometimes it is useful to define the colors of things in the HSV colorspace, not in the usual RGB. Here is a conversion routine with a small demo.

Code: Select all

Sub RGB2HSV(ByRef hue As Integer,ByRef sat As Integer,ByRef value As Integer, ByVal myrgb As Integer)
           Dim As Integer max=0,min=255,r,g,b
           r=(myRGB Shr 16) And &hff
           g=(myRGB Shr 8) And &hff
           b=myRGB And &hff
           If r>max Then max=r
           If r<min Then min=r
           If g>max Then max=g
           If g<min Then min=g
           If b>max Then max=b
           If b<min Then min=b
           value=max
           '? max, min,Hex(myrgb),r,g,b
           If max=0 Or max=min Then sat=0:hue=0:Exit sub
           sat=255*(1.-(min/max))
           If max=r  Then
              hue=60.*(g-b)/(max-min)+IIf(g>=b,0,360)
           ElseIf max=g then
              hue=60.*(b-r)/(max-min)+120           
           Else
             hue=60.*(r-g)/(max-min)+240
           End if   
End sub
Function HSV2RGB(Byval hue As Integer, Byval sat As Integer,Byval value As Integer )As Integer
 'hue 0 TO 359   0=red,  120 green   240 blue
 'sat is saturation  0 to 255  0 is neutral grey 255 is full color
 'value 0 To 255 is the brightness  0 is black 255 is maximum brightness
  If sat = 0 Then Return RGB(value,value,value)
     
  hue  Mod= 360
  Dim As Single h1= hue/60         
  Dim As Integer i = Int(h1)
  Dim As Single f = frac(h1)                 
  Dim As Integer p = value * ( 255 - sat )/256
  Dim As Integer q = value * ( 255 -  f*sat)/256
  Dim As Integer t = value * ( 255 - ( 1. - f )*sat)/256

  Select Case As Const i
    Case 0: Return RGB(value,t,p)
    Case 1: Return RGB(q,value,p)
    Case 2: Return RGB(p,value,t)
    Case 3: Return RGB(p,q,value)
    Case 4: Return RGB(t,p,value)
    Case 5: Return RGB(value,p,q)
  End Select
End Function

Screenres 800,600,32
ScreenLock
'increasing value
For J As Integer =0 To 255
For I As Integer =0 To 359
 Pset (J,i),HSV2RGB(I,255,J)
Next
Next

'decreasing saturation
For J As Integer =0 To 255
For I As Integer =0 To 359
 Pset (J+256,i),HSV2RGB(I,255-J,255)
Next
Next
'one hue ,saturation and value gradient
For J As Integer =0 To 255
For I As Integer =0 To 255
 Pset (i,j+360),HSV2RGB(0,i,j)   
 Pset (i+256,j+360),HSV2RGB(120,i,j)
  Pset (i+512,j+360),HSV2RGB(240,i,j)
Next
Next

'value gradient for saturation 0
For J As Integer =0 To 255
For I As Integer =0 To 255
 Pset (I+512,J),HSV2RGB(0,0,j)
Next
Next

Dim As Integer h,s,v
RGB2hsv(h,s,v,HSV2rgb(27,224,25))
Print h,s,v


SCREENUNLOCK
Sleep

EDITED: Added Joshy's correction and the inverse function. Please notice the results use integer format so converting back and forth may prodeuce errors.. For accuracy both h,s,v and r,g,b should be floating point.
Last edited by Antoni on Jul 02, 2007 20:32, edited 2 times in total.
redcrab
Posts: 618
Joined: Feb 07, 2006 15:29
Location: France / Luxemburg
Contact:

Postby redcrab » Jul 02, 2007 8:41

really usefull thx !
D.J.Peters
Posts: 7191
Joined: May 28, 2005 3:28

Postby D.J.Peters » Jul 02, 2007 10:11

if the range are from 0 to 255 = 256 steps
so it must be /256 (i think so)

Code: Select all

const div256 as single =1.0/256.0
Function HSV2RGB(Byval hue As Integer, Byval sat As Integer,Byval value As Integer )As Integer
 'hue 0 TO 359   0=red,  120 green   240 blue
 'sat is saturation  0 to 255  0 is neutral grey 255 is full color 
 'value 0 To 255 is the brightness  0 is black 255 is maximum brightness
  If sat = 0 Then Return RGB(value,value,value)
     
  hue  Mod= 360
  Dim As Single h1= hue/60         
  Dim As Integer i = Int(h1)
  Dim As Single f = frac(h1)                 
  Dim As Integer p = value * ( 255 - sat )*div256
  Dim As Integer q = value * ( 255 -  f*sat)*div256
  Dim As Integer t = value * ( 255 - ( 1. - f )*sat)*div256

  Select Case As Const i
    Case 0: Return RGB(value,t,p)
    Case 1: Return RGB(q,value,p)
    Case 2: Return RGB(p,value,t)
    Case 3: Return RGB(p,q,value)
    Case 4: Return RGB(t,p,value)
    Case 5: Return RGB(value,p,q)
  End Select
End Function
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

Postby Antoni » Jul 02, 2007 16:06

OOps!
marinedalek
Posts: 124
Joined: Aug 24, 2005 1:55
Contact:

Postby marinedalek » Jul 02, 2007 16:38

Great! I tried porting some C code for this ages ago but didn't get very far at all! Could you possibly write an RGB2HSV function? Pretty please :D
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

Postby Antoni » Jul 02, 2007 20:33

Dons. I updated the first post with the new function and Joshy's correction.
Pritchard
Posts: 5425
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Sep 20, 2008 18:52

Been a year, but this is the best code I've seen for HSV->RGB conversions yet. Wow.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Sep 21, 2008 14:53

This is even better than the code I wrote because it's so simple. Very nice, I like it :)

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 0 guests