PowerBASIC Peer Support Forums
 

Go Back   PowerBASIC Peer Support Forums > User to user Discussions > PowerBASIC for Windows

Notices

PowerBASIC for Windows User to user discussions about the PB/Win (formerly PB/DLL) product line. Discussion topics include PowerBASIC Forms, PowerGEN and PowerTree for Windows.

Reply
 
Thread Tools Display Modes
  #1  
Old Mar 8th, 2006, 03:32 PM
Lothar Pink Lothar Pink is offline
Member
 
Join Date: Mar 2002
Posts: 1,610
RGB->HSV and vice versa

I'm an idiot on that area.

How can I calculate H,S,V out of given R,G,B color values and vice versa?

Lothar

------------------
Reply With Quote
  #2  
Old Mar 8th, 2006, 04:00 PM
Dominic Sexton Dominic Sexton is offline
Member
 
Join Date: Aug 2002
Posts: 49
I have not tried it but the code here is quite easy to understand
http://cs.haifa.ac.il/hagit/courses/...t_convert.html

------------------

Dominic
Manchester UK
__________________

Dominic
Manchester UK
Reply With Quote
  #3  
Old Mar 8th, 2006, 04:05 PM
Trevor Ridley Trevor Ridley is offline
Member
 
Join Date: Mar 2003
Posts: 87
I adapted the following code for converting HSL to RGB from something I found on the internet a long while back, I don't remember where. I cannot vouch for its accuracy but it worked good enough for me.

It is in VBA but it should not be difficult to adapt for PB:

Code:
Sub HSLtoRGB(ByVal Hue As Integer, _
             ByVal Saturation As Integer, _
             ByVal Luminance As Integer, _
             ByRef RGBRed As Integer, _
             ByRef RGBGreen As Integer, _
             ByRef RGBBlue As Integer)

    Dim pHue As Single
    Dim pSat As Single
    Dim pLum As Single
    Dim pRed As Single
    Dim pGreen As Single
    Dim pBlue As Single
    Dim temp2 As Single
    Dim temp3() As Single
    Dim temp1 As Single
    Dim n As Integer

   ReDim temp3(0 To 2)
   
   pHue = Hue / 255
   pSat = Saturation / 255
   pLum = Luminance / 255

   If pSat = 0 Then
      pRed = pLum
      pGreen = pLum
      pBlue = pLum
   Else
      If pLum < 0.5 Then
         temp2 = pLum * (1 + pSat)
      Else
         temp2 = pLum + pSat - pLum * pSat
      End If
      temp1 = 2 * pLum - temp2
   
      temp3(0) = pHue + 1 / 3
      temp3(1) = pHue
      temp3(2) = pHue - 1 / 3
      
      For n = 0 To 2
         If temp3(n) < 0 Then temp3(n) = temp3(n) + 1
         If temp3(n) > 1 Then temp3(n) = temp3(n) - 1
      
         If 6 * temp3(n) < 1 Then
            temp3(n) = temp1 + (temp2 - temp1) * 6 * temp3(n)
         Else
            If 2 * temp3(n) < 1 Then
               temp3(n) = temp2
            Else
               If 3 * temp3(n%) < 2 Then
                  temp3(n%) = temp1 + (temp2 - temp1) _
                        * ((2 / 3) - temp3(n%)) * 6
               Else
                  temp3(n%) = temp1
                End If
             End If
          End If
       Next n%

       pRed = temp3(0)
       pGreen = temp3(1)
       pBlue = temp3(2)
    End If

    RGBRed = Int(pRed * 255)
    RGBGreen = Int(pGreen * 255)
    RGBBlue = Int(pBlue * 255)
    
End Sub
------------------
Trevor
__________________
Trevor
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:49 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright © 1999-2010 PowerBASIC, Inc. All Rights Reserved.