Sub topic 1
Why hex, rgb() and hsl() fall short
Prerequisites
This chapter assumes you can do the following:
- Write basic CSS rules and selectors.
- Declare and use custom properties, such as
--brandandvar(--brand). - Read colors written as hex (
#3b82f6),rgb()andhsl().
No new syntax appears here. The goal is to understand the problems that oklch() and color-mix() solve, so the later chapters make sense.
What these notations have in common
Hex, rgb() and hsl() look different, but they describe the same thing. Each one picks a color inside the sRGB color space, which is the standard color range for screens since the 1990s.
- Hex and
rgb()give the amount of red, green and blue light as three numbers.#3b82f6andrgb(59 130 246)are the same color. hsl()is a rearrangement of those same RGB values into hue, saturation and lightness. It is easier to read, but it covers the same range of colors.
The diagram shows the two problems that matter in practice.
Problem 1: the numbers do not match what you see
The L in HSL stands for lightness. You would expect that colors with the same L look equally light. They do not. Compare these two colors:
.yellow {
background: hsl(60 100% 50%);
}
.blue {
background: hsl(240 100% 50%);
}Both have 100% saturation and 50% lightness. Yet the yellow looks very bright and the blue looks dark. HSL lightness is a math formula on RGB values. It does not model how the human eye responds to different hues. The eye is much more sensitive to green and yellow light than to blue light.
This has a direct effect on accessibility. Contrast ratios in WCAG (the Web Content Accessibility Guidelines) are based on relative luminance, which is closer to what you perceive.
| Color | HSL lightness | Contrast with white text |
|---|---|---|
| `hsl(60 100% 50%)` (yellow) | 50% | about 1.07:1 |
| `hsl(240 100% 50%)` (blue) | 50% | about 8.6:1 |
White text on the yellow is unreadable. White text on the blue passes easily. The two colors have the same L value.
Problem 2: edits behave differently for each hue
Many designers build a hover state by lowering HSL lightness:
.button {
background: hsl(217 91% 60%);
}
.button:hover {
background: hsl(217 91% 50%);
}This works acceptably for one hue. But a 10-point drop does not look like the same change on a green button, a yellow button and a purple button. If you generate a palette by looping over hues and using the same lightness steps, the results look uneven. Some shades look washed out and others look heavy.
The same issue affects gradients and mixes. A gradient between two colors in sRGB can pass through a muddy gray midpoint. Hue shifts in HSL can also change the apparent brightness along the way.
Problem 3: a limited range of colors
Modern screens, such as most phones, laptops and recent monitors, can show more colors than sRGB covers. The Display P3 color space is about 25% larger. Hex has no way to ask for those colors. In rgb(), the values stay inside the sRGB range of 0 to 255, so the most vivid greens, reds and blues are out of reach.
Problem 4: hex hides the meaning
Hex values are compact, but they carry no readable meaning. Looking at #3b82f6, you cannot tell how light it is or how much to change it for a hover state. You have to open a color picker or convert it by hand. In a design system, this makes review and maintenance harder.
What a better model needs
From these problems, you can list what a better color notation should do:
- Give a lightness value that matches perceived brightness, so equal values look equally light.
- Separate lightness, colorfulness and hue, so you can change one without disturbing the others.
- Reach colors beyond sRGB on screens that support them.
- Mix and blend colors without gray or dirty midpoints.
The next chapter introduces Oklab and OKLCH, which were designed to meet these needs.
Exercise
Two colors both have hsl() lightness of 50%. One is yellow (hue 60) and one is blue (hue 240). Why can white text be unreadable on one and fine on the other, and which problem from this chapter does this show?
Show answer
HSL lightness is a formula applied to RGB values, not a measure of perceived brightness. The eye responds much more strongly to yellow than to blue light, so the yellow looks far brighter and gives low contrast with white text (about 1.07:1), while the blue gives about 8.6:1. This shows that HSL lightness does not match what you see, so it cannot guarantee readable text.
Exercise
Name two reasons why hex is a poor fit for computed colors such as hover states and palettes.
Show answer
Any two of these: the value carries no readable meaning, so you cannot tell how light or colorful the color is; you cannot adjust lightness alone without converting to another notation; and hex cannot express colors outside the sRGB range that modern displays can show.