# HSL to RGB?

#### brncao

##### Board Regular
I'm surprised VBA doesn't have HSL. It only accepts RGB. I need to convert HSL to RGB.

### Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
 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``````

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))
.Weight = xlThin
End With
End Sub``````

Code:
``Sub AddColoredBorders(myRange As Range, iColor As Variant)``
should do it.

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:
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``````

Replies
4
Views
203
Replies
6
Views
94
Replies
6
Views
76
Replies
3
Views
66
Replies
1
Views
104

1,203,639
Messages
6,056,487
Members
444,869
Latest member
tulo spont

### 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.

### Which adblocker are you using?

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

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