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


Function HSLToRGB2
Converts from the HSL to the RGB color model, more specifically converts Hue, Saturation, and Luminance values to Red, Green, and Blue values. It's the inverse of RGBToHSL2.
More on HSL and RGB here.
Code
HSLToRGB201
Public Function HSLToRGB201( _
    ByVal Hue As Long, _
    ByVal Saturation As Long, _
    ByVal Luminance As Long, _
    Optional Validate As Boolean _
    ) As Long
' by Donald, donald@xbeat.net, 20011126
' after seeing Thomas Kabir's code (contact@vbfrood.de, http://www.vbfrood.de)
  Dim rR As Single, rG As Single, rB As Single
  Dim rH As Single, rL As Single, rS As Single
  Dim rMin As Single, rMax As Single, rDiff As Single
  
  If Validate Then ValidateHSL Hue, Saturation, Luminance
  
  If Saturation = 0 Then
    ' CLng(CSng(...)) else 127.5 -> 127
    HSLToRGB201 = CLng(CSng(2.55 * Luminance)) * &H10101
  Else
    rH = Hue / 60: rS = Saturation / 100: rL = Luminance / 100
    If rL <= 0.5 Then
      rMin = rL * (1 - rS)
    Else
      rMin = rL - rS * (1 - rL)
    End If
    rMax = 2 * rL - rMin
    rDiff = rMax - rMin
    
    Select Case Hue \ 60
    Case 0
      rR = rMax
      rB = rMin
      rG = rH * rDiff + rMin
    Case 1
      rG = rMax
      rB = rMin
      rR = rMin - (rH - 2) * rDiff
    Case 2
      rG = rMax
      rR = rMin
      rB = (rH - 2) * rDiff + rMin
    Case 3
      rB = rMax
      rR = rMin
      rG = rMin - (rH - 4) * rDiff
    Case 4
      rB = rMax
      rG = rMin
      rR = (rH - 4) * rDiff + rMin
    Case Else
      rR = rMax
      rG = rMin
      rB = rMin - (rH - 6) * rDiff
    End Select
    HSLToRGB201 = CLng(rB * 255) * &H10000 + CLng(rG * 255) * &H100 + CLng(rR * 255)
  End If
  
End Function
Calls
1Hue = 75: Saturation = 50: Luminance = 25
2Hue = 235: Saturation = 93: Luminance = 72
3Hue = 0: Saturation = 50: Luminance = 25
4Hue = 75: Saturation = 0: Luminance = 25
5Hue = 75: Saturation = 50: Luminance = 0
Charts
 VB5 Charts
CodeAuthorDopingNotes
HSLToRGB201 Donald  
Call 1
11.000.856Ás
Call 2
11.000.876Ás
Call 3
11.000.783Ás
Call 4
11.000.268Ás
Call 5
11.000.763Ás
 VB6 Charts
CodeAuthorDopingNotes
HSLToRGB201 Donald  
Call 1
11.000.887Ás
Call 2
11.000.896Ás
Call 3
11.000.809Ás
Call 4
11.000.259Ás
Call 5
11.000.821Ás
Notes & Conclusions
...
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau