HEX, RGB and HSL Colour Codes Explained for Beginners
Every colour you see on a screen is a mix of red, green and blue light. Designers and developers describe those mixes with short codes, and the three you will meet most often are HEX, RGB and HSL. This guide shows what each means and how to use them.
RGB: red, green, blue
RGB lists the amount of red, green and blue as three numbers from 0 to 255. Written as rgb(37, 99, 235), it says: a little red (37), some green (99) and a lot of blue (235), which produces a strong blue. Black is rgb(0, 0, 0) because no light is on, and white is rgb(255, 255, 255) because everything is at full strength. Pure red is rgb(255, 0, 0).
HEX: the same thing in base 16
A HEX code is RGB written in hexadecimal, a counting system with 16 digits (0 to 9, then A to F). It starts with #, followed by two digits each for red, green and blue.
Take #2563EB:
- 25 in hex = 2 × 16 + 5 = 37 (red)
- 63 in hex = 6 × 16 + 3 = 99 (green)
- EB in hex = 14 × 16 + 11 = 235 (blue)
So #2563EB is exactly rgb(37, 99, 235). Short forms exist too: #FFF means #FFFFFF (white) and #09C means #0099CC. HEX is the most common format in CSS and design tools simply because it is short.
HSL: hue, saturation, lightness
RGB is how screens work, but it is not how people think about colour. HSL describes colour in a more natural way:
- 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 the intensity, from 0% (grey) to 100% (vivid).
- Lightness runs from 0% (black) through 50% (the pure colour) to 100% (white).
Our example blue is hsl(221, 83%, 53%). To make a darker version for a hover state, you can simply lower the lightness to 45% and keep the rest. Doing the same in HEX or RGB would need three separate changes. This is why many designers build colour schemes in HSL.
Converting between formats
| Format | Example | Good for |
|---|---|---|
| HEX | #2563EB | Compact CSS, design handoff |
| RGB | rgb(37, 99, 235) | Code, image editing, transparency (rgba) |
| HSL | hsl(221, 83%, 53%) | Making lighter, darker or muted variants |
To convert HEX to RGB by hand, turn each pair of digits into a decimal number as shown above. To go the other way, divide each RGB value by 16: the whole part is the first digit and the remainder is the second. Or let a tool do it. The Image Color Picker shows all three formats for any pixel you click on in a photo or screenshot.
Contrast and readability
A colour must also be readable. The Web Content Accessibility Guidelines (WCAG) define a contrast ratio between text and its background. For normal text, a ratio of at least 4.5 to 1 is the minimum for level AA, and large text needs 3 to 1. Level AAA asks for 7 to 1. White text on our example blue works out to about 5.2 to 1, so it passes for normal text. Light grey text on white, a common design choice, often fails. Check your colours, especially if your audience includes older people or people with colour-vision differences.
Tips for picking a palette
- Start with one main colour and add one or two accents. Too many colours look messy.
- Use neutral greys for text and backgrounds, and keep your bright colour for buttons and links.
- Do not rely on colour alone to convey meaning, for example red and green for error and success. Add an icon or a word.
- Test your palette in light and dark backgrounds.
- Pick colours from a photo that matches your brand, so the page feels coherent.