Skip to content

Guide

HEX, RGB, HSL and OKLCH explained

Four ways to write the same color, each good at something different. Here is how to read every part of a color code, and which format to reach for when.

By Mark Stoumann ·

Open any stylesheet and you will find colors written as #e879f9, rgb(232, 121, 249), hsl(292 91% 73%) or oklch(74.8% 0.207 322.2). They can all describe the very same pink. The formats differ in what their numbers mean, and that decides how easy it is to change a color on purpose: lighter, less saturated, or a different hue with the same weight.

HEX: three numbers in base 16

A hex code is three pairs of hexadecimal digits: red, green and blue. Each pair runs from 00 (0) to ff (255), so #e879f9 splits into e8, 79 and f9:

PairChannelCalculationValue
e8Red14 × 16 + 8232
79Green7 × 16 + 9121
f9Blue15 × 16 + 9249

The CSS specification allows hex codes of 3, 4, 6 or 8 digits. The short forms double each digit, so #f0c is #ff00cc, and #00f is #0000ff, pure blue. The fourth or the last two digits are alpha, the opacity: #e879f980 is the same pink at 80 in hex, 128 out of 255, which is about 50% opaque. Hex is case-insensitive, so #E879F9 is identical.

Hex is compact and universal, which is why design tools copy it by default. Its weakness is that you can’t adjust it by reading it: nobody can see at a glance what a 10% lighter version of #e879f9 would be.

RGB: the same numbers in decimal

rgb() holds exactly the same information as hex, written as numbers from 0 to 255 or as percentages. CSS now has two ways to write it:

  • Legacy syntax, with commas: rgb(232, 121, 249), and rgba(232, 121, 249, 0.5) for transparency.
  • Modern syntax, with spaces and the alpha after a slash: rgb(232 121 249 / 50%).

Both are valid. In CSS Color Level 4, rgba() is simply an alias of rgb(), so you never need the extra “a” any more. The newer functions such as oklch() only accept the modern syntax, so it is a good habit to use it everywhere. ColorPeekr copies RGB with commas because many tools outside CSS still expect that form.

RGB is the language of screens: every pixel is a mix of red, green and blue light. It is great for exchanging exact values and terrible for editing. Making a color “a bit warmer” means changing all three numbers at once.

HSL: friendlier numbers with a hidden catch

HSL rearranges RGB into three properties people understand: hue, an angle on the color wheel (0° red, 120° green, 240° blue), saturation from gray (0%) to full color (100%), and lightness from black (0%) through the pure color (50%) to white (100%). To make a button’s hover state darker, you lower one number.

The catch is that HSL lightness is a formula, not a perception. It is the average of the strongest and weakest of the three channels, so it ignores the fact that our eyes find green and yellow far brighter than blue. The CSS specification itself points this out. Compare three colors that HSL calls equally light:

HSLHexOKLCH lightnessContrast with white
hsl(60 100% 50%)#ffff0096.8%1.07:1
hsl(120 100% 50%)#00ff0086.6%1.37:1
hsl(240 100% 50%)#0000ff45.2%8.59:1

All three are “50% lightness”, yet yellow is almost invisible on white while blue passes even the strictest text contrast level. That is why rotating the hue in HSL to build a palette can mix glowing and heavy colors, and why a theme built by changing only HSL hue rarely passes a contrast check for every color.

HSL values are also usually rounded to whole numbers, which loses a little precision. #e879f9 becomes hsl(292, 91%, 73%), and that converts back to #e87bf9. You will never see the difference, but if a brand color must stay exact, keep the hex code as the master value.

OKLCH: lightness you can trust

OKLCH is the polar form of Oklab, a color space Björn Ottosson published in December 2020 to predict how light, how colorful and what hue a color looks to people. It has the same three ideas as HSL, but each number means what it says:

  • L, lightness, from 0% (black) to 100% (white). Two colors with the same L look about equally light, whatever their hue.
  • C, chroma, how colorful it is. 0 is gray; in CSS, 100% means 0.4. The most colorful sRGB color, pure blue, has a chroma of about 0.31.
  • H, hue, an angle like HSL’s, but the angles are different. Pure red sits near 29°, yellow near 110°, the brightest green near 142° and pure blue near 264°.

Because lightness is reliable, OKLCH is the best format for shade scales and design tokens. Keep L and C and turn H, and you get colors of equal visual weight; keep H and C and step L, and each shade looks evenly darker. ColorPeekr’s shade scales and the palette generator’s Balanced option work this way.

Colors beyond the sRGB screen

Hex, RGB and HSL can only describe sRGB, the standard color space of the web. OKLCH can also describe more saturated colors that wide-gamut displays, such as Display P3 screens, are able to show. When a screen can’t show a color, the browser gamut-maps it: it keeps the lightness and hue and lowers the chroma until the color fits. So oklch(70% 0.3 150) shows as a vivid green on a wide-gamut screen and as a slightly duller one on an ordinary monitor (ColorPeekr’s own fit gives #2dbc5f; browsers may land a shade away). If you need the color to be the same everywhere, use a chroma the sRGB range can hold; ColorPeekr’s values are always inside it.

All current major browsers have supported oklch() since May 2023, along with color-mix(), which blends two colors in a space you choose: color-mix(in oklch, #e879f9, black 30%) darkens the pink by mixing in 30% black, measured in OKLCH rather than in raw RGB channels.

Which format should you use?

JobBest formatWhy
Sharing a brand colorHEXShort, exact, understood by every tool
Transparency in CSS8-digit hex or modern rgb()#00000080 or rgb(0 0 0 / 50%)
Quick hand editsHSLChange one number to shift hue, saturation or lightness
Design tokens and shade scalesOKLCHEqual lightness looks equally light
Vivid colors on modern screensOKLCHReaches beyond sRGB, falls back gracefully
PrintCMYK from your printer’s profileScreen formats don’t know the paper or inks

HSB (also called HSV), the model in Photoshop and Figma’s pickers, is not a CSS format at all; convert it to one of the above before you write it in a stylesheet. You can paste any of these formats into the color picker to see the others. If you are about to choose text colors, read how the contrast ratio is calculated next: it builds on the same idea of how light a color really looks.

Frequently asked questions

Is HEX or RGB better for CSS?

Neither is better: #e879f9 and rgb(232 121 249) are the same color, and browsers treat them identically. Hex is shorter and is what design tools copy by default; rgb() is easier to read and to change by hand. Pick one per project and stay consistent.

What does an 8-digit hex code mean?

The last two digits are the alpha channel, the opacity, from 00 (fully transparent) to FF (fully opaque). #e879f980 is the same pink at 80 in hex, which is 128 out of 255, or about 50% opacity. The 4-digit form, such as #f0c8, works the same way with each digit doubled.

Why does my color change slightly when I convert it to HSL and back?

Because HSL values are usually rounded to whole degrees and percentages, and that loses a little precision. For example, #e879f9 becomes hsl(292, 91%, 73%), which converts back to #e87bf9. The difference is invisible, but if you need the exact color, keep the hex code as the source.

Do I still need rgba() and hsla()?

No. CSS Color Level 4 made rgba() and hsla() aliases of rgb() and hsl(), and all of them accept an alpha value. In the modern syntax you write it after a slash: rgb(232 121 249 / 50%).

Can I use OKLCH in all browsers?

Yes, in all current major browsers. According to MDN, oklch() has worked across browsers since May 2023. If you must support older browsers, put a hex or rgb() fallback on the line before the oklch() declaration.