HSL to RGB?

brncao

Board Regular
Joined
Apr 28, 2015
Messages
147
I'm surprised VBA doesn't have HSL. It only accepts RGB. I need to convert HSL to RGB.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Row\Col
A​
B​
C​
D​
E​
1​
RGB​
128​
128​
0​
B1:D1: Input
2​
HSL​
40​
60​
240​
B2:D2: {=RGBToHLS(B1,C1,D1)}
3​
RGB​
128​
128​
0​
B3:D3: {=HLsToRGB(B2,C2,D2)}

Code:
' Color conversions revised 2009-11, 2015-0105

Public Declare Function ColorRGBToHLS Lib "shlwapi.dll" _
                                      (ByVal clrRGB As Long, _
                                       pwHue As Long, _
                                       pwLuminance As Long, _
                                       pwSaturation As Long) As Long

Public Declare Function ColorHLSToRGB Lib "shlwapi.dll" _
                                      (ByVal wHue As Long, _
                                       ByVal wLuminance As Long, _
                                       ByVal wSaturation As Long) As Long

Function RGBToHLS(iRed As Long, iGrn As Long, iBlu As Long) As Variant
  ' shg 2014
  ' wrapper for ColorRGBToHLS
  Dim iHue As Long
  Dim iLum As Long
  Dim iSat As Long
  
  ColorRGBToHLS RGB(iRed, iGrn, iBlu), iHue, iLum, iSat
  RGBToHLS = Array(iHue, iLum, iSat)
End Function

Function HLSToRGB(iHue As Long, iLum As Long, iSat As Long) As Variant
  ' shg 2014
  ' wrapper for ColorHLSToRGB
  Dim iRGB As Long
  
  iRGB = ColorHLSToRGB(iHue, iLum, iSat)
  HLSToRGB = Array(iRGB And 255, (iRGB \ 256) And 255, (iRGB \ 65536) And 255)
End Function
 
Upvote 0
I'm getting an error: "Type mismatch: array or user-defined type expected"

Code:
Private Sub FormatCells()
    Dim rA As Range
    Dim rB As Range
    
    Set rA = Range("C1")
    Set rB = Range("D4")
    AddColoredBorders Union(rA, rB), HLSToRGB(0, 128, 150)
End Sub
Code:
Sub AddColoredBorders(myRange As Range, iColor() As Variant)
    With myRange.Borders
        .LineStyle = xlContinuous
        .Color = RGB(iColor(0), iColor(1), iColor(2))
        .TintAndShade = 0
        .Weight = xlThin
    End With
End Sub
 
Upvote 0
Code:
Sub AddColoredBorders(myRange As Range, iColor As Variant)
should do it.
 
Upvote 0
It worked! Also, rather than typing HLSToRGB(0, 128, 150), I would like to do it like this: HLSToRGB(arrayVariable).
Where arrayVariable = Array(0, 128, 150). How would I define it?
 
Last edited:
Upvote 0
Ignore above post. I'm not using it multiple times so no variables needed. I could've done arrayVariable = HLSToRGB(0, 128, 150) instead.

So my next step is to alternate hues, but I'm getting mismatches.


Code:
Dim i As Integer
For i = 0 to 100
HLSToRGB(AlternateHues(CLng(i)), 128, 150)
Next i
Code:
Function AlternateHues(iHue As Long)
    If iHue Mod 2 = 0 Then
        If iHue > 255 Then
            AlternateHues = iHue - 255
        Else
            AlternateHues = HLSToRGB(iHue)
        End If
    Else
        If iHue > 255 Then
            AlternateHues = iHue + 85 - 255
        Else
            AlternateHues = HLSToRGB(iHue + 85)
        End If
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,215,388
Messages
6,124,659
Members
449,178
Latest member
Emilou

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top