We help IT Professionals succeed at work.

Excel formula to convert HSL color values to RGB color values

93 Views
Last Modified: 2020-04-15
Hello,

I'm trying to:
(1) improve the Hue/Sat/Lum formulas in the attached spreadsheet and
(2) determine what Excel formula(s) will convert numeric HSL color values into numeric values for other color formats.



Explanation of the attached Excel file:

The following screenshot is taken from the attached Color Conversion Chart* which I created some time ago.

2020-04-12b_Color-Conversion-Chart.PNG
As shown in the image, inserting values for a given color into the yellow cells of a row enables you to see that color (in the Display Color column) and obtain the conversion values for other numeric color formats.

For example, suppose you want to view the appearance and obtain conversion values of a color for which you have 3-digit 000-255 (MS Office) values. In this case, you would enter your values in the three yellow cells in conversion row [1].

Alternatively, if you have three hex values for a certain color, you could enter them in the yellow cells in conversion row [2] to display the color and obtain the conversion values in other formats.

Issues:

#1 Hue/Sat/Lum formulas need tweaking:
Although the Hue/Sat/Lum columns work fairly well in most cases, I sometimes get errors or even negative values — which obviously cannot be right. For example, entering MS Office values of [255][153][255] results in HSL values of [-43][255][204] and, as shown in the screenshot, entering values of [025][025][025] gives HSL results of [#DIV/0!][0][25].

#2 Need formulas to convert from HSL parameters to other formats:
The chart converts from other formats to HSL fairly well but I could use some help finding the formulas to convert from HSL to other formats.

By the way, The Color Conversion Chart is in the first tab of the attached file, but there are several other sheet tabs which I was initially going to delete before uploading the file. However, although they contain quite a bit of junk, much of the math and formula breakdowns are in those additional tabs so they may or may not be helpful/of interest.

Thanks

*There are many websites which will display a particular color and conversion values. However, the value of this color conversion chart is that you can copy & paste one of the conversion rows into multiple rows below and simultaneously see and compare as many different colors as you want.

Attached file: Color-Conversion-Chart-2020-04-12_u.xlsm
Comment
Watch Question

Retired
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
The HSL formulas was wrong.
S and L values are percentage, and can not be above 100.

I have inserted new formulas for HSL above the line, and the reverse calculation from HSL to RGB in row 17.

Formulas from this page
http://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
Color-Conversion-Chart-2020-04-13_u.xlsm

Author

Commented:
__________________________________________________

Thanks for the responses.

__________________________________________________

>> Paul Sauve

Thanks Paul. I will go back and review that.

__________________________________________________

>> Ejgil Hedegaard

The HSL formulas was wrong.
S and L values are percentage, and can not be above 100.

I believe your statement is incorrect. For example, the following image shows three views of the font color box for a given color. As is evident in the third box, the Sat value is 255 which is well above 100.



Also, the amended spreadsheet which you attached is giving incorrect values in the existing conversion rows. For example, entering parameters in conversion row [1] is producing incorrect values for other formats.

Finally, attempting to enter values in the HSL boxes in conversion row [5] results in a runtime error.
CERTIFIED EXPERT

Commented:
On the reverse calculator from Paul, HSL to RGB
https://www.rapidtables.com/convert/color/hsl-to-rgb.html

it is stated
Enter hue in degrees (°), saturation and lightness (0..100%)

Another site show
Hue is a degree on the color wheel (from 0 to 360) - 0 (or 360) is red, 120 is green, 240 is blue. Saturation is a percentage value; 0% means a shade of gray and 100% is the full color. Lightness is also a percentage; 0% is black, 100% is white.


If you enter RGB values 0, 128, 128 in [1] the HSL result is 180, 100, 25
Just like the RGB to HSL converter Paul mentioned.
I don't know why Excel convert that to 127, 255, 64


The runtime error is because the resulting RGB values becomes negative when some S or L are above 100%, not all, but when it calculate the RGB values are above 255.


I have not changed other formulas for [1], only HSL formulas, so don't know what the incorrect values are for other formulas.

Author

Commented:
My apologies Ejgil. You are correct and I was wrong. I was looking at the 0-255 values used in MS Office applications. Thank you for your patience and explanation.

Author

Commented:
Paul,

Using the link you gave me, I worked the math formulas into Excel formulas and finally got the HSL to RGB conversion row working. Many thanks.

Due to being very rusty with the math and the Excel formulas, the worksheet containing my calculations is very cumbersome but I've added it to my workbook (mostly for my own reference).

Now in the attached file, conversion row [5] enables you to enter values from the HSL numbers in MS Office apps and get them converted into other color formats.

The main reason I wanted this functionality is that I learned some time ago that the best/easiest way to have different colors look good together is to keep the Sat value the same in all of them. Now I can do that with a small chart I created at the bottom of the color conversion worksheet.

For example, the chart in the following screenshot shows 12 different Hues with the same Sat & Lum values resulting in 12 colors that look good with each other:

2020-04-14b_EE_color-conversion.PNG
Different entries can be placed in the three blue cells to see other combinations.

Thanks again for the help.

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.