HEX, RGB and HSL: a practical guide to colour codes

How each colour format works, when to use which in CSS and design tools, and how to check your colours are readable.

Cover image for HEX, RGB and HSL: a practical guide to colour codes

Every colour on a screen is a mix of red, green and blue light. HEX, RGB and HSL are three ways of writing down the same mix, and each is easiest for a different job.

HEX

color: #a8d08d;

A HEX code is six hexadecimal digits: two each for red, green and blue, from 00 (none) to ff (full). #ff0000 is pure red, #000000 is black and #ffffff is white. A three-digit shorthand doubles each digit, so #abc means #aabbcc.

HEX is compact, and the most common way to share colours between design tools, developers and brand guidelines. Its weakness is that it's hard to read: you can't easily tell from #a8d08d how to make it a little darker.

An eight-digit version, sometimes called HEXA, adds a fourth pair for opacity: #a8d08d80 is the same green at about 50%.

RGB

color: rgb(168, 208, 141);
color: rgb(168 208 141 / 50%);

RGB writes the same three channels as decimal numbers from 0 to 255. It's the format most code and image software works in, and the one to use when you need to calculate with colours. A fourth value adds opacity (RGBA), from 0 (transparent) to 1 (solid).

HSL

color: hsl(96, 42%, 68%);

HSL describes a colour the way people think about it:

  • Hue is the position on the colour wheel, from 0 to 360 degrees: 0 is red, 120 is green and 240 is blue.
  • Saturation is how intense it is, from 0% (grey) to 100% (full colour).
  • Lightness runs from 0% (black) through the pure colour at 50% to 100% (white).

That makes HSL the easiest format for building variations. Keep the hue and saturation, change the lightness, and you get a consistent set of tints and shades for buttons, hover states and backgrounds.

HSV, also called HSB

Design tools such as Photoshop and Figma often show HSV, or HSB. It has the same hue, but uses value (brightness) instead of lightness: 100% value is the pure colour, not white. CSS doesn't use it, so you'll convert it when moving a colour from a design tool into code.

Which to use

  • Sharing colours and brand guidelines: HEX, with RGB alongside.
  • Building a palette of shades: HSL.
  • Anything with transparency: RGBA, HSLA or HEXA.
  • Print: none of these. Printers use CMYK, and the conversion isn't exact, so ask your printer or designer for print values.

Building a small palette in HSL

A practical approach for a website:

  1. Start with the main brand colour, for example hsl(96, 42%, 68%).
  2. Make a darker shade for text and buttons by lowering the lightness, such as hsl(96, 42%, 30%).
  3. Make a very light tint for backgrounds by raising it, such as hsl(96, 42%, 94%).
  4. Choose greys with the same hue and very low saturation, such as hsl(96, 8%, 20%) for body text, so they sit comfortably beside the brand colour.

Keeping the palette in CSS custom properties makes it easy to adjust later:

:root {
  --brand: hsl(96, 42%, 68%);
  --brand-dark: hsl(96, 42%, 30%);
  --brand-tint: hsl(96, 42%, 94%);
  --text: hsl(96, 8%, 20%);
}

Check the contrast

A colour that looks good can still be hard to read. The Web Content Accessibility Guidelines (WCAG) set a minimum contrast ratio between text and its background: 4.5:1 for normal text and 3:1 for large text. Light brand colours often fail as text on white. Use them for backgrounds and accents, and keep a darker version for text.

Convert in one step

Paste any colour into the Color converter to see it as HEX, HEXA, RGB, RGBA, HSV, HSL and HSLA at once, or choose one by eye with the Color picker. There are direct converters for each pair as well, such as HEX to RGB and HEX to HSL.