VBspeed / Colors / RGBToHSL2
VBspeed © 2000-10, updated: 27-Nov-2001
RGBToHSL2 RGB to HSL, Formula 2
See also RGBToHSL


Function RGBToHSL2
Converts from the RGB to the HSL color model, more specifically converts Red, Green, and Blue values to Hue, Saturation, and Luminance values. It's the inverse of HSLToRGB2.
More on HSL and RGB here.
Code
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
Calls
1RGBValue = &HE61C94
2RGBValue = &H1CE694
3RGBValue = &H1C1C1C
4RGBValue = &H0&
Charts
 VB5 Charts
CodeAuthorDopingNotes
RGBtoHSL201 Donald  
Call 1
11.000.997Ás
Call 2
11.000.997Ás
Call 3
11.000.501Ás
Call 4
11.000.418Ás
 VB6 Charts
CodeAuthorDopingNotes
RGBtoHSL201 Donald  
Call 1
11.001.020Ás
Call 2
11.001.048Ás
Call 3
11.000.510Ás
Call 4
11.000.426Ás
Notes & Conclusions
...
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau