![]() |
|
|||||||
| 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. |
![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 ------------------ |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|