dbTalk Databases Forums  

Convert RGB colours to HSL

comp.databases.ms-access comp.databases.ms-access


Discuss Convert RGB colours to HSL in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Phil
 
Posts: n/a

Default Convert RGB colours to HSL - 01-13-2012 , 07:19 PM






I have found all sorts of code to convert from RGB to HSL, but seem to get
different results. I need to get the same results as the colour picker, so
when I feed in Red (255 or HFF0000) I want to get out Hue = 0, Saturation =
240 and Luninescence = 120.

The example below gives Luminance = 112

This is a sample
Public Type HSL
Hue As Integer
Saturation As Integer
Luminance As Integer
End Type

Public Type RGB
Red As Integer
Green As Integer
Blue As Integer
End Type

Public Function RGBToHSL(Colin As String) As HSL
'?RGBtoHSL("FF0000").Luminance

Dim R As Double ' Range 0 — 1
Dim G As Double ' Range 0 — 1
Dim B As Double ' Range 0 — 1
Dim pRed As Single
Dim pGreen As Single
Dim pBlue As Single
Dim RetVal As HSL
Dim Pmax As Single
Dim pMin As Single
Dim pLum As Single
Dim pSat As Single
Dim pHue As Single

R = CDbl("&H" & Mid$(Colin, 2, 2))
G = CDbl("&H" & Mid$(Colin, 4, 2))
B = CDbl("&H" & Mid$(Colin, 6, 2))

pRed = R / 255
pGreen = G / 255
pBlue = B / 255

If pRed > pGreen Then
If pRed > pBlue Then
Pmax = pRed
Else
Pmax = pBlue
End If
ElseIf pGreen > pBlue Then
Pmax = pGreen
Else
Pmax = pBlue
End If

If pRed < pGreen Then
If pRed < pBlue Then
pMin = pRed
Else
pMin = pBlue
End If
ElseIf pGreen < pBlue Then
pMin = pGreen
Else
pMin = pBlue
End If

pLum = (Pmax + pMin) / 2

If Pmax = pMin Then
pSat = 0
pHue = 0
Else
If pLum < 0.5 Then
pSat = (Pmax - pMin) / (Pmax + pMin)
Else
pSat = (Pmax - pMin) / (2 - Pmax - pMin)
End If

Select Case Pmax!
Case pRed
pHue = (pGreen - pBlue) / (Pmax - pMin)
Case pGreen
pHue = 2 + (pBlue - pRed) / (Pmax - pMin)
Case pBlue
pHue = 4 + (pRed - pGreen) / (Pmax - pMin)
End Select
End If

RetVal.Hue = pHue * 239 \ 6
If RetVal.Hue < 0 Then RetVal.Hue = RetVal.Hue + 240

RetVal.Saturation = Int(pSat * 239)
RetVal.Luminance = Int(pLum * 239)

RGBToHSL = RetVal

End Function

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

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.