Reference

HEX vs RGB vs HSL: What the Color Codes Mean

Three ways to write the same color, with conversions worked by hand

Open any stylesheet and you will find colors written three different ways: #3A7BD5, rgb(58, 123, 213) and hsl(215, 65%, 53%). They are not three colors. They are the same blue in three notations, and each notation is better at some jobs. Knowing how they relate makes it far easier to tweak a color by hand instead of guessing, and it explains what you are looking at in the Color ID converter.

RGB: how screens make color

A screen pixel is made of a red, a green and a blue light source. RGB gives the brightness of each, from 0 (off) to 255 (full) in the usual 8-bit-per-channel format. Because light adds together, rgb(255, 0, 0) is pure red, rgb(255, 255, 0) is yellow (red plus green), rgb(255, 255, 255) is white and rgb(0, 0, 0) is black. With 256 levels in each of three channels there are 256 × 256 × 256 = 16,777,216 possible colors.

HEX: RGB in base 16

A HEX code is the same three numbers written in hexadecimal, two digits per channel, in the order red, green, blue, after a hash sign. Hexadecimal counts 0–9 then A–F, so one pair covers 00 (0) to FF (255).

To convert #3A7BD5 by hand, split it into 3A, 7B, D5. Each pair is the first digit × 16 plus the second digit:

So the color is rgb(58, 123, 213). HEX has two handy shortcuts. When every pair has doubled digits, CSS lets you write three digits: #FF8800 can be #F80. And a code with the same pair in all three channels, such as #808080, is always a gray.

HSL: how people think about color

HSL stands for hue, saturation and lightness, and it describes the same colors in terms that match intuition.

PartRangeMeaning
Hue0–360°Position on the color wheel. 0 is red, 120 is green, 240 is blue, and it wraps back to red at 360.
Saturation0–100%How intense the color is. 0% is a pure gray, 100% is the most vivid version of that hue.
Lightness0–100%0% is black, 100% is white, 50% is the pure color.

The practical benefit is that edits are predictable. Want a darker button on hover? Keep the hue and saturation and drop the lightness by ten points. Want a washed-out version for a disabled state? Reduce saturation. Doing the same with RGB means adjusting three numbers at once.

Converting RGB to HSL by hand

Take rgb(58, 123, 213). First divide each channel by 255 to get 0.227, 0.482 and 0.835. Then:

  1. Find the largest and smallest: max = 0.835 (blue), min = 0.227 (red). Their difference, the chroma, is 0.608.
  2. Lightness is the average of max and min: (0.835 + 0.227) / 2 = 0.531, or about 53%.
  3. Saturation, when lightness is above 0.5, is chroma divided by (2 minus max minus min): 0.608 / (2 − 1.062) = 0.648, or about 65%.
  4. Hue: because blue is the largest channel, hue = 60 × (4 + (red − green) / chroma) = 60 × (4 + (0.227 − 0.482) / 0.608) = about 215°.

Result: hsl(215, 65%, 53%). That is the same blue, expressed so that you can see at a glance it is a medium-bright, fairly saturated blue with a slight lean toward cyan (blue itself sits at 240).

Which format to use

SituationBest choice
Copying a brand color from a style guideHEX: compact and universal
Design tools with three numeric fieldsRGB
Building a light-to-dark scale or a hover stateHSL: change one number
Generating a harmonious paletteHSL: rotate the hue (see the palette guide)
Print workNone of them: print uses CMYK or spot inks, so convert with your print software

Caveats worth knowing

All three notations describe colors in the sRGB space that web browsers assume by default. Wider-gamut displays can show colors outside sRGB, and CSS has newer notations for those, but for everyday web and app work HEX, RGB and HSL are interchangeable. Conversion between them involves rounding, so converting back and forth a few times can move a value by one unit in a channel. That is invisible to the eye but can surprise you if you compare strings.

To try it yourself, open the Converter tab, type a HEX such as #3A7BD5, an RGB triple like 58, 123, 213 or an HSL triple like 215, 65%, 53%, and press Convert to see all three.

Keep reading

Open the color picker