freebasic.net Forum Index
FreeBASIC's Official Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in

HSV Colorspace

 
Post new topic   Reply to topic    freebasic.net Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
Antoni
Master
PostPosted: Jul 02, 2007 8:39    Post subject: HSV Colorspace Reply with quote

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:

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
 
Back to top
View user's profile Visit poster's website
redcrab
Sr. Member
PostPosted: Jul 02, 2007 8:41    Post subject: Reply with quote

really usefull thx !
 
Back to top
View user's profile Send e-mail Visit poster's website
D.J.Peters
Guru
PostPosted: Jul 02, 2007 10:11    Post subject: Reply with quote

if the range are from 0 to 255 = 256 steps
so it must be /256 (i think so)
Code:
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
 
Back to top
View user's profile Visit poster's website
Antoni
Master
PostPosted: Jul 02, 2007 16:06    Post subject: Reply with quote

OOps!
 
Back to top
View user's profile Visit poster's website
marinedalek

PostPosted: Jul 02, 2007 16:38    Post subject: Reply with quote

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
 
Back to top
View user's profile Visit poster's website
Antoni
Master
PostPosted: Jul 02, 2007 20:33    Post subject: Reply with quote

Dons. I updated the first post with the new function and Joshy's correction.
 
Back to top
View user's profile Visit poster's website
Pritchard
Guru
PostPosted: Sep 20, 2008 18:52    Post subject: Reply with quote

Been a year, but this is the best code I've seen for HSV->RGB conversions yet. Wow.
 
Back to top
View user's profile Send e-mail
notthecheatr
Master
PostPosted: Sep 21, 2008 14:53    Post subject: Reply with quote

This is even better than the code I wrote because it's so simple. Very nice, I like it :)
 
Back to top
View user's profile Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    freebasic.net Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



sf.net phatcode