RGBToHSL201 |
Public Function RGBToHSL201(ByVal RGBValue As Long) As HSL
' by Donald, donald@xbeat.net, 20011126
Dim R As Long, G As Long, b As Long
Dim lMax As Long, lMin As Long, lDiff As Long, lSum As Long
R = RGBValue And &HFF&
G = (RGBValue And &HFF00&) \ &H100&
b = (RGBValue And &HFF0000) \ &H10000
If R > G Then lMax = R: lMin = G Else lMax = G: lMin = R
If b > lMax Then lMax = b Else If b < lMin Then lMin = b
lDiff = lMax - lMin
lSum = lMax + lMin
' Luminance
RGBToHSL201.Luminance = lSum / 5.1!
If lDiff Then
' Saturation
If RGBToHSL201.Luminance <= 50& Then
RGBToHSL201.Saturation = 100 * lDiff / lSum
Else
RGBToHSL201.Saturation = 100 * lDiff / (510 - lSum)
End If
' Hue
Dim q As Single: q = 60 / lDiff
Select Case lMax
Case R
If G < b Then
RGBToHSL201.Hue = 360& + q * (G - b)
Else
RGBToHSL201.Hue = q * (G - b)
End If
Case G
RGBToHSL201.Hue = 120& + q * (b - R)
Case b
RGBToHSL201.Hue = 240& + q * (R - G)
End Select
End If
End Function
|