Converting HSL to RGB

Written by Allen Wyatt (last updated December 30, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


2

Bassel needs a formula that can convert HSL color values (hue, saturation, and lightness values) to RGB color values (red, blue, and green values). He wonders if anyone can point him to either a worksheet formula that will do this or a way to do it in a macro.

Converting from HSL to RGB values is not a particularly trivial undertaking. For those interested in the math behind it, you can check out the Wikipedia article on the topic.

If you need to do the conversion infrequently, the easiest way is to follow these steps:

  1. Display the Home tab of the ribbon.
  2. Click the down-arrow at the right side of the Fill Color tool, in the Font group. Excel displays a small palette of colors and some other options.
  3. Choose More Colors. Excel displays the Colors dialog box.
  4. Make sure the Custom tab is displayed.
  5. Using the Color Model drop-down list, choose HSL. (See Figure 1.)
  6. Figure 1. The Custom tab of the Colors dialog box.

  7. At the bottom of the dialog box you can see the individual values for Hue, Sat, and Lum. Change these values to whatever you want.
  8. Using the Color Model drop-down list, chose RGB. The values shown at the bottom of the dialog box are the HSL values (entered in step 6) converted to RGB.
  9. Click Cancel when done.

This approach assumes that each of your HSL values can be express in the range of 0 to 255. If, however, your HSL values are either an angle (for hue) or a percentage (for saturation and luminance), then you'll need to convert them manually before entering them in step 6. You can convert an angle value by multiplying the angle by 255 and then dividing by 360. Percentages can be converted by multiplying them by 2.55.

If you have a need to get the values more often, then creating your own user-defined function to do the conversion will help. The following example accepts three parameters (Hue, Saturation, and Luminance) and returns a string containing the decimal RGB values separated by commas.

Function HSLtoRGB(Hue As Integer, Saturation As Integer, _
  Luminance As Integer) As String
    Dim r As Integer
    Dim g As Integer
    Dim b As Integer
    Dim C As Double
    Dim X As Double
    Dim m As Double
    Dim rfrac As Double
    Dim gfrac As Double
    Dim bfrac As Double
    Dim hangle As Double
    Dim hfrac As Double
    Dim sfrac As Double
    Dim lfrac As Double

    If (Saturation = 0) Then
        r = 255
        g = 255
        b = 255
    Else
        lfrac = Luminance / 255
        hangle = Hue / 255 * 360
        sfrac = Saturation / 255
        C = (1 - Abs(2 * lfrac - 1)) * sfrac
        hfrac = hangle / 60
        hfrac = hfrac - Int(hfrac / 2) * 2 'fmod calc
        X = (1 - Abs(hfrac - 1)) * C
        m = lfrac - C / 2
        Select Case hangle
            Case Is < 60
                rfrac = C
                gfrac = X
                bfrac = 0
            Case Is < 120
                rfrac = X
                gfrac = C
                bfrac = 0
            Case Is < 180
                rfrac = 0
                gfrac = C
                bfrac = X
            Case Is < 240
                rfrac = 0
                gfrac = X
                bfrac = C
            Case Is < 300
                rfrac = X
                gfrac = 0
                bfrac = C
            Case Else
                rfrac = C
                gfrac = 0
                bfrac = X
        End Select
        r = Round((rfrac + m) * 255)
        g = Round((gfrac + m) * 255)
        b = Round((bfrac + m) * 255)
    End If
    HSLtoRGB = Str(r) & "," & Str(g) & "," & Str(b)
End Function

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13535) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Saving Your Web Page

Want to save your document as a Web page? It's easy to do in Word; almost as easy as saving your document normally.

Discover More

Changing the Insertion Point Cursor

Want to change the characteristics of the insertion point used by Word? You may be out of luck, unless you make some ...

Discover More

Replacing Characters at the End of a Cell

The Find and Replace capabilities of Excel can come in handy, but they can't accomplish all your replacement needs. One ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Making Sure Cells are Filled In before Saving

When creating a workbook that will be used by others, you may wish to ensure that the user fills in some cells before ...

Discover More

Selecting Columns in VBA when Cells are Merged

If you have a macro that selects different columns in a worksheet while processing information, you may get some ...

Discover More

Displaying the Selected Cell's Address

Need to know the address of the cell that is currently selected? The function and macro highlighted in this tip will come ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is seven minus 7?

2024-08-13 21:33:39

Beta

Great code! One error for me: I had to change all Integer to Long. I used this in a file that has many macros and uses a lot of memory. No clue why Integer didn't work for me.


2023-12-31 17:06:11

George Reasoner

There is an error in the macro. If Saturation is 0, then the R, G and B values are all equal to the Luminance value.
Function HSLtoRGB(Hue As Integer, Saturation As Integer, _
Luminance As Integer) As String
Dim r As Integer
Dim g As Integer
Dim b As Integer
Dim C As Double
Dim X As Double
Dim m As Double
Dim rfrac As Double
Dim gfrac As Double
Dim bfrac As Double
Dim hangle As Double
Dim hfrac As Double
Dim sfrac As Double
Dim lfrac As Double

If (Saturation = 0) Then
r = Luminance
g = Luminance
b = Luminance
Else
lfrac = Luminance / 255
hangle = Hue / 255 * 360
sfrac = Saturation / 255
C = (1 - Abs(2 * lfrac - 1)) * sfrac
hfrac = hangle / 60
hfrac = hfrac - Int(hfrac / 2) * 2 'fmod calc
X = (1 - Abs(hfrac - 1)) * C
m = lfrac - C / 2
Select Case hangle
Case Is < 60
rfrac = C
gfrac = X
bfrac = 0
Case Is < 120
rfrac = X
gfrac = C
bfrac = 0
Case Is < 180
rfrac = 0
gfrac = C
bfrac = X
Case Is < 240
rfrac = 0
gfrac = X
bfrac = C
Case Is < 300
rfrac = X
gfrac = 0
bfrac = C
Case Else
rfrac = C
gfrac = 0
bfrac = X
End Select
r = Round((rfrac + m) * 255)
g = Round((gfrac + m) * 255)
b = Round((bfrac + m) * 255)
End If
HSLtoRGB = Str(r) & "," & Str(g) & "," & Str(b)
End Function


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.