Developer Tools

HEX vs RGB vs HSL: Color Formats Explained in Plain CSS

HEX, rgb() and hsl() write the same colors three very different ways. What each number means, how to convert by hand, and which one belongs in your CSS.

4 min read·Updated 2026-04-16

HEX, rgb() and hsl() are three ways of writing the same color, and each one is better at a different job. HEX is the compact format designers exchange; rgb() reads like hardware on a screen; hsl() is the only one you can reason about without a color picker. This guide explains exactly what each number means, converts between them by hand, and tells you which format to reach for in which situation.

HEX without the mystery

A hex color like #4C6EF5 is three pairs of base-16 digits, each pair encoding one channel from 0 to 255. 4C hex is 76 decimal (4 times 16 plus 12), 6E is 110, and F5 is 245. So #4C6EF5 is rgb(76, 110, 245). The shorthand #4CF removes the doubled digits to mean rgb(68, 204, 255); the 8-digit form #4C6EF580 appends alpha as a fourth byte. Because the range of each channel is exactly 256 values, a hex pair is the perfect two-character way to store one byte.

rgb(): the screen-literal format

rgb(76, 110, 245) states channel intensities outright, which is why it is the native language of displays and of the color math you will do in code. The legacy comma syntax (rgb(76, 110, 245)) still works everywhere, and the modern space-separated syntax adds a slash for alpha: rgb(76 110 245 / 0.5). Colors live in a 256-cubed space — about 16.7 million combinations — and the currentColor value behaves exactly like a color keyword in modern CSS.

hsl(): the format you can actually edit

HSL decomposes color into what your eye perceives: hue, saturation and lightness. Hue is an angle on a 360-degree color wheel where 0 and 360 are red, 120 is green and 240 is blue. Saturation runs 0% (pure gray) to 100% (fully vivid). Lightness runs 0% (black) to 100% (white), with 50% being the pure color itself. That structure makes adjustments arithmetic: dropping lightness to 30% instantly gives the dark variant of a brand color, and shifting saturation toward 0% produces the muted one — no math, no guessing.

The three formats side by side

FormatExampleReadable at a glance?Best for
HEX#4C6EF5No — opaque digitsCompact tokens, design handoff
rgb()rgb(76, 110, 245)Channel intensitiesCode that manipulates screens
hsl()hsl(224, 90%, 63%)Yes — every number means somethingDesign systems, dark mode, theming

Converting by hand: one worked example

Take #4C6EF5. The channels are 76, 110 and 245. Normalize each to a fraction: 0.298, 0.431, 0.961. Lightness is (max plus min) over 2, so (0.961 + 0.298) / 2 = 0.63, or 63%. Saturation is (max minus min) over (2 minus 2L) when L halves below 50%, which with max 0.961 and min 0.298 gives (0.961 - 0.298) / (2 - 2 times 0.63) = 0.663 / 0.74, about 0.90, or 90%. Hue is chosen by whichever channel is the max — blue, so hue = 4 plus (R minus G) over (max minus min), which resolves to 224 degrees. Result: hsl(224, 90%, 63%). Doing that in a tool is faster than the arithmetic, but knowing the shape of it means you can predict roughly how any hex will look in HSL.

Which to use in your CSS

  • HEX for the brand token file, where you copy values straight from a designer's handoff and never hand-edit them.
  • hsl() for anything thematic — support light and dark mode by adjusting lightness in one place, and generate on-hover shades with small lightness deltas.
  • rgb() when your code composes colors programmatically, mixes them with alpha, or reads pixel data back from canvas.
  • Relative colors and color-mix() in the newest browsers whenever you want a shade derived from a variable instead of a hardcoded twin.

Tools that make the round-trip instant

The Color Converter converts hex, rgb and hsl to one another in both directions, including alpha, so verifying a hand conversion takes one paste. The Color Generator produces coordinated palettes when you need a full mood to work from, and the Color Palette Extractor pulls the dominant colors out of an uploaded image — handy when a brand's real identity lives in a logo rather than a style guide.

Frequently asked questions

Is hsl genuinely better for accessibility work? Yes, in practice. Because lightness is a human-meaningful axis, you can enforce contrast by adjusting it — a button pair where hues differ but lightness is forced toward 50% tends to keep text readable. It is not a substitute for a contrast checker, but it makes contrast-friendly choices much more natural to author.

Do I still need HEX for browser support? No. rgb() has near-universal support and hsl() has been safe since 2011. The 8-digit and space-separated forms are the newer bits — keep the comma forms for the longest support tail.

When should I use a named color like red instead of a format? Only for one-off demo code or colors you never intend to vary. Named colors are opaque about their exact value, cannot express alpha, and collapse your ability to derive shades — every real token should live in one of the three numeric formats.

Tools used in this guide

Related guides