StringMash.com

RGB to HSL converter

rgb() to hue, saturation and lightness, with the working.

Conversion
16 characters
Updates as you type
Hex#FF8800
RGBrgb(255, 136, 0)
HSLhsl(32, 100%, 50%)
HSV (HSB)hsv(32, 100%, 100%)
CMYKcmyk(0%, 47%, 100%, 0%)
Nearest CSS namedarkorange
Contrast with white2.39:1 (fails)
Contrast with black8.77:1 (AAA)
The swatch and working are below

Show the steps
Aa blackAa white
  1. Divide each channel by 255: red 1, green 0.533, blue 0. The largest is 1 and the smallest 0, so the chroma (their difference) is 1.
  2. Lightness is halfway between the largest and smallest: L = (1 + 0) ÷ 2 = 0.5, so 50%.
  3. Saturation is the chroma divided by (1 − |2L − 1|): 1 ÷ 1 = 1, so 100%.
  4. Hue: red is the largest, so H = 60 × (0.533 − 0) ÷ 1 = 32°. Rounded, 32°.
  5. Together: hsl(32, 100%, 50%).
  6. Contrast: 2.39:1 against white and 8.77:1 against black, so black text reads better on it.

Using the converter

Type an RGB colour as rgb(255, 136, 0) or three numbers from 0 to 255. The answer is an hsl() value you can paste into CSS.

Hex codes and colour names work too. Swap the boxes to turn hsl() back into rgb().

The RGB to HSL formula

Divide red, green and blue by 255. Call the largest M and the smallest m, and the chroma C = M − m.

Lightness is L = (M + m) ÷ 2. Saturation is C ÷ (1 − |2L − 1|), or 0 when C is 0. The hue is the same as in HSV. If red is the largest, H = 60 × ((G − B) ÷ C), plus 360 if negative. If green, H = 60 × ((B − R) ÷ C + 2). If blue, H = 60 × ((R − G) ÷ C + 4).

A worked example

Take rgb(255, 136, 0). Divided by 255 that's 1, 0.533 and 0, so M = 1, m = 0 and C = 1.

Lightness is (1 + 0) ÷ 2 = 0.5. Saturation is 1 ÷ (1 − |1 − 1|) = 1. Red is the largest, so the hue is 60 × 0.533 = 32°. The answer is hsl(32, 100%, 50%).

Common colours in HSL

Rounded to whole degrees and percentages.
ColourRGBHSL
Redrgb(255, 0, 0)hsl(0, 100%, 50%)
Orangergb(255, 165, 0)hsl(39, 100%, 50%)
Yellowrgb(255, 255, 0)hsl(60, 100%, 50%)
Limergb(0, 255, 0)hsl(120, 100%, 50%)
Greenrgb(0, 128, 0)hsl(120, 100%, 25%)
Cyanrgb(0, 255, 255)hsl(180, 100%, 50%)
Bluergb(0, 0, 255)hsl(240, 100%, 50%)
Navyrgb(0, 0, 128)hsl(240, 100%, 25%)
Purplergb(128, 0, 128)hsl(300, 100%, 25%)
Magentargb(255, 0, 255)hsl(300, 100%, 50%)
Pinkrgb(255, 192, 203)hsl(350, 100%, 88%)
Brownrgb(165, 42, 42)hsl(0, 59%, 41%)
Whitergb(255, 255, 255)hsl(0, 0%, 100%)
Silverrgb(192, 192, 192)hsl(0, 0%, 75%)
Greyrgb(128, 128, 128)hsl(0, 0%, 50%)
Blackrgb(0, 0, 0)hsl(0, 0%, 0%)

HSL compared with HSV

Both models share the same hue. They differ in the other two numbers. In HSL, lightness 50% is the pure colour, 0% is black and 100% is white, so a colour can be lightened or darkened from the middle.

In HSV (also called HSB), the pure colour sits at 100% value and white is 0% saturation at 100% value. hsl(220, 60%, 50%) and hsv(220, 75%, 80%) are the same blue. CSS reads hsl() but not hsv(), and RGB to HSV has the other model.

Where HSL came from

George Joblove and Donald Greenberg described HSL in the August 1978 issue of Computer Graphics, the same issue in which Alvy Ray Smith described HSV.

CSS has accepted hsl() since CSS Color Level 3, and Level 4 added the space-separated form, hsl(220 60% 50%), with an optional alpha after a slash.

Questions

Why is the hue 0 for greys?

When red, green and blue are equal there is no hue, so it is written as 0. Changing it makes no difference.

Should I use hsl() or rgb() in CSS?

Both give the same colours. hsl() is easier to adjust by hand, because you can change the lightness and keep the hue.

Sources

Added and reviewed . What's new