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

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:

- Display the Home tab of the ribbon.
- 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.
- Choose More Colors. Excel displays the Colors dialog box.
- Make sure the Custom tab is displayed.
- Using the Color Model drop-down list, choose HSL. (See Figure 1.)
- 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.
- 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.
- Click Cancel when done.

** Figure 1.** The Custom tab of the Colors dialog box.

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.

**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!

When you have a macro that processes a huge amount of data, it can seem like it takes forever to finish up. These ...

Discover MoreWhen you change from one worksheet to another, you may want to have Excel automatically run a macro for the worksheet you ...

Discover MoreWhen you need to get rid of characters in the middle of a cell value, the best way to do it is through the use of macros. ...

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

2023-04-14 14:29:14

Bruce Volkert

This was really helpful.

I struggled quite a bit to make my conversions based on documentation all over the internet that used the Mod function. Unfortunately, as you well know, the MOD function only returns integer or long values. This is problematic for the conversions because many of them are working with Saturation, Luminance, and Value in the 0-1 range. Using the Mod function instead of the approach you provide kills the conversion because the values always come back as 0 or 1 when the Saturation, Luminance, and Value are in the 0-1 range. It does not help much to work in the 0-100 range because the RGB system is working in the 0-255 range and you obviously lose fidelity if you only work with 0-100.

I understand that you don't need to know this; but, I sure did. Maybe this comment helps others.

Thanks for all you do.

2020-02-07 04:14:47

ashleedawg

You say, "You can convert an angle value by multiplying the angle by 255 and then dividing by 360."

I think the hue (angle°) be adjusted as:

hue/360*256

while your formula for "s" and "L" is correct as:

percentage/100*255

The reason for the difference is that with hue, 0° and 360° are the same, but with the other two values (when expressed as percentages) 0% and 100% are quite different. In reality, the difference is so small it's probably not visually noticeable one way or the other.

2020-02-07 04:03:17

ashleedawg

Shouldn't it be: angle * 256 / 360 ?

2018-06-03 06:56:02

Mike

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.

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

Copyright © 2023 Sharon Parq Associates, Inc.

## Comments