Sub topic 1
Why hex, rgb() and hsl() fall short
Hex, rgb() and hsl() describe the same small set of colors, all in the sRGB color space. This chapter explains what that means and where those notations break down when you build palettes, hover states and gradients. The later chapters introduce oklch() and color-mix() as the fix, so this one only sets up the problem.
Prerequisites
This chapter assumes you already know:
- Basic CSS syntax and selectors.
- CSS custom properties (variables), including
var(). - How to read hex (
#3b82f6),rgb()andhsl()colors.
You don't need to know any color science. The terms are defined as they appear.
What these notations actually are
A color space is a rule that maps numbers to colors people can see. Hex, rgb() and hsl() look different, but they share one color space.
- Hex (
#3b82f6) isrgb()written in base 16.#3b82f6isrgb(59 130 246). rgb()gives the strength of the red, green and blue light of a screen pixel. Each channel runs from 0 to 255.hsl()is a rearrangement of the same RGB values into hue (an angle on the color wheel), saturation and lightness. It is a different way to name the same sRGB colors, not a different set of colors.
sRGB is the standard color space that web browsers have used since the 1990s. It was designed around the cathode-ray tube monitors of that era. It was not designed so that numbers match what people perceive. The shortcomings below follow from that.
Problem 1: hsl() lightness is not perceived lightness
The L in hsl() is a formula over the RGB channels. It is not a measurement of how bright a color looks. As a result, two colors with the same L can look very different in brightness.
Here is a demo. Every swatch has hsl(<hue> 100% 50%), so saturation and lightness are identical. Only the hue changes.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Same hsl lightness, different brightness</title>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 0.5rem; }
.swatch {
width: 7rem;
padding: 1.5rem 0.5rem;
text-align: center;
color: white;
font-weight: 700;
}
.red { background: hsl(0 100% 50%); }
.yellow { background: hsl(60 100% 50%); }
.green { background: hsl(120 100% 50%); }
.cyan { background: hsl(180 100% 50%); }
.blue { background: hsl(240 100% 50%); }
.magenta { background: hsl(300 100% 50%); }
</style>
</head>
<body>
<div class="swatch red">Red</div>
<div class="swatch yellow">Yellow</div>
<div class="swatch green">Green</div>
<div class="swatch cyan">Cyan</div>
<div class="swatch blue">Blue</div>
<div class="swatch magenta">Magenta</div>
</body>
</html>Open it and look at the white text. On yellow, green and cyan it is almost unreadable. On blue it is easy to read. Yet all six backgrounds say "50% lightness".
WCAG (the Web Content Accessibility Guidelines) defines a quantity called relative luminance. It is a weighted measure of how much light a color emits, and it correlates much better with perceived brightness than HSL lightness does. The contrast ratio of white text on each swatch follows from it:
| Hue | Color | Relative luminance | Contrast with white text |
|---|---|---|---|
| 0 | `hsl(0 100% 50%)` | 0.213 | 4.0:1 |
| 60 | `hsl(60 100% 50%)` | 0.928 | 1.07:1 |
| 120 | `hsl(120 100% 50%)` | 0.715 | 1.37:1 |
| 180 | `hsl(180 100% 50%)` | 0.787 | 1.25:1 |
| 240 | `hsl(240 100% 50%)` | 0.072 | 8.6:1 |
| 300 | `hsl(300 100% 50%)` | 0.285 | 3.1:1 |
WCAG AA asks for at least 4.5:1 for normal body text. With the same hsl() lightness, white text on yellow fails by a wide margin and white text on blue passes easily.
Problem 2: palettes built by rotating hue are uneven
A common shortcut for a set of category colors, chart series or status colors is to keep saturation and lightness fixed and step the hue.
:root {
--series-1: hsl(0 70% 50%);
--series-2: hsl(60 70% 50%);
--series-3: hsl(120 70% 50%);
--series-4: hsl(240 70% 50%);
}You would expect four colors of equal weight. In practice, yellow and green look much lighter than red and blue. In a chart, some series stand out more than others for no reason related to the data. Put text on these colors and you need a different text color for each, which defeats the point of generating them from a formula.
The same effect appears in design systems with a "500 shade" for every hue. If the 500 shades come from hsl() with the same L, they have different contrast against the page background, so they can't be swapped for each other safely.
Problem 3: arithmetic on channels gives surprising results
Generated colors depend on doing arithmetic on color values, such as darkening for hover or averaging two colors. Arithmetic on sRGB and HSL values has three common failure modes.
Lightness steps are not consistent across hues
A typical hover rule lowers L by a fixed amount:
.button-blue {
background: hsl(220 90% 56%);
}
.button-blue:hover {
background: hsl(220 90% 46%);
}
.button-yellow {
background: hsl(50 90% 56%);
}
.button-yellow:hover {
background: hsl(50 90% 46%);
}Both hover rules subtract 10 points of L. The darkening you see is not the same. The size of the visible change depends on the hue, and some hues also appear to shift in tint as they darken. Blues are known to drift toward purple. A "subtract 10" rule is therefore not a reliable design token. You end up tuning each hue by hand.
Gamma makes channel averages look wrong
sRGB channel values are gamma-encoded. This means the number stored is not proportional to the light emitted. It is a curved encoding that spends more of its range on dark values, which matches how eyes work. For example, rgb(128 128 128) is not half as bright as rgb(255 255 255). In linear light it emits about 22% as much.
When you blend two colors by averaging encoded channel values, you are averaging numbers on a curve. The result is mathematically simple and visually uneven. Mixed colors often look darker or flatter than you expect.
Mixing complementary colors passes through gray
Blue is rgb(0 0 255) and yellow is rgb(255 255 0). The midpoint of the two in sRGB is rgb(127.5 127.5 127.5), which is a plain gray. A gradient between them goes through that gray. You can see it in the browser:
.gradient {
height: 4rem;
background: linear-gradient(in srgb to right, blue, yellow);
}The in srgb part names the color space used to calculate the in-between colors, and it's the default for gradients. Expect a muddy middle section. A later chapter on interpolation spaces explains how to choose a different space to fix this.
Problem 4: the sRGB gamut ceiling
A gamut is the range of colors a color space can represent. Hex and the classic comma or space syntax of rgb() cannot go beyond sRGB. Many current phones, laptops and monitors show a wider gamut, such as Display P3, which is about 25% larger than sRGB. Its most saturated reds and greens have no hex code.
CSS can ask for wider colors with the color() function:
.promo {
background: #ff0000;
background: color(display-p3 1 0 0);
}The first line is the most saturated red sRGB can show. The second is the most saturated red in Display P3. It only differs on screens that can show it, and browsers without color() support ignore the second line and use the hex fallback.
This matters because brand colors, illustrations and photos often fall outside sRGB. Writing them in hex means they are silently squeezed into the narrower range.
Problem 5: the notations are hard to read and adjust
Beyond color science, there is a plain usability problem.
- Hex is opaque. Looking at
#3b82f6you can't tell whether it is dark, light, vivid or dull. You can't change it by editing a digit and predict the outcome. - RGB channels don't match intent. To make a color "slightly lighter" you have to change all three channels in proportion.
- HSL is closer to intent, but its axes are unreliable. Hue and saturation shift how bright a color looks, as Problem 1 showed. You can't safely adjust one axis and expect the others to stay put.
For a design system you want one-axis edits to behave predictably. If a token says "lightness 60%", every hue at that lightness should look equally light.
Summary of the limits
| Limit | Where it shows up | Consequence |
|---|---|---|
| HSL lightness is not perceptual | Same `L`, different brightness | Text contrast varies by hue |
| Hue rotation palettes | Chart series, status colors | Uneven visual weight |
| Fixed lightness steps | Hover and active states | Inconsistent darkening and hue drift |
| Gamma-encoded channels | Blends and gradients | Dull or muddy mixes |
| sRGB-only gamut | Brand colors, vivid accents | Colors are clipped on wide-gamut screens |
| Opaque numbers | Maintenance | Tweaks need trial and error |
What the next chapters do about it
The fix has two parts. First, use a color space designed so that equal numeric steps look like equal visual steps. That is the idea behind Oklab and its polar form OKLCH, which the next chapter explains. Second, use CSS functions that let you choose the space for blending (color-mix()) and derive new colors from existing ones (relative color syntax). Hex and rgb() stay valid and useful for static values. They are not a good basis for computed color.
Exercise
A teammate builds status colors with hsl(0 80% 50%), hsl(60 80% 50%) and hsl(120 80% 50%) and puts white text on each. Why will the text be hard to read on two of them, and what does it show about the L in hsl()?
Show answer
The three colors have the same HSL lightness, but their relative luminance differs a lot. Yellow (hue 60) and green (hue 120) are much brighter than red (hue 0), so white text on them has very low contrast, well under the WCAG AA minimum of 4.5:1. This shows that L in hsl() is a formula over the RGB channels and does not match how bright a color looks.
Exercise
What color does the middle of linear-gradient(in srgb to right, blue, yellow) approach, and why?
Show answer
It approaches mid-gray, rgb(127.5 127.5 127.5). Blue is rgb(0 0 255) and yellow is rgb(255 255 0). Averaging each channel gives 127.5 for red, green and blue, so the midpoint has no hue.
Exercise
Name two reasons a brand color written as a hex code may look less vivid on a modern phone than the designer intended.
Show answer
Hex can only represent colors inside the sRGB gamut, so any more saturated color in the design (for example from Display P3) is clipped to the nearest sRGB color. Also, if the designer's source was wider than sRGB, no hex value can reproduce it. Writing the color with color(display-p3 ...) and a hex fallback avoids the loss on screens that support it.