Sub topic 14
Using HSL and HSV in CSS and code
This chapter assumes you have read the earlier chapters on hue, saturation, value, and the HSV and HSL models. You should also be able to read short CSS and Python snippets. Here you will apply those ideas in two places: CSS, which supports HSL, and code, where HSV conversions are common.
HSL in CSS
CSS has an hsl() function but no hsv() function. It takes hue in degrees (0 to 360), then saturation and lightness as percentages. Because hue is a single number, you can store it in a custom property and derive a whole family of colors from it.
:root {
--brand-hue: 210;
--brand: hsl(var(--brand-hue) 80% 45%);
--brand-light: hsl(var(--brand-hue) 80% 85%);
--brand-dark: hsl(var(--brand-hue) 80% 20%);
--brand-muted: hsl(var(--brand-hue) 25% 45%);
--brand-complement: hsl(calc(var(--brand-hue) + 180) 80% 45%);
}Changing lightness gives the tints and shades. Lowering saturation gives the muted tone. Adding 180 to the hue gives the complement. Hues past 360 wrap around, so 390 is the same as 30. To re-theme the whole page, change --brand-hue and leave everything else alone.
HSV in code
Most design tools show HSV in their color pickers, and many image libraries use it. Python's standard-library colorsys module works with all values from 0 to 1.
import colorsys
# A medium blue as RGB values between 0 and 1
r, g, b = 0.2, 0.4, 0.8
h, s, v = colorsys.rgb_to_hsv(r, g, b)
print(round(h * 360), round(s, 2), round(v, 2)) # 220 0.75 0.8
# Halve the saturation, keep hue and value
r2, g2, b2 = colorsys.hsv_to_rgb(h, s * 0.5, v)Hue is a fraction of a full turn, so multiply by 360 to get degrees. A complement is a half turn, which is 0.5 in these units.
import colorsys
def complement(r, g, b):
h, s, v = colorsys.rgb_to_hsv(r, g, b)
return colorsys.hsv_to_rgb((h + 0.5) % 1.0, s, v)
print(complement(1.0, 0.5, 0.0)) # orange gives a blue-leaning colorThe % 1.0 keeps the hue inside the 0 to 1 range, the same wrap-around that CSS does for you.
Bridging HSV and HSL
The same color has different saturation numbers in the two models, because their third axes differ. Value is the brightness of the strongest channel. Lightness is the midpoint between the strongest and weakest channels. To hand an HSV color to CSS, convert it.
import colorsys
def hsv_to_css_hsl(h, s, v):
l = v * (1 - s / 2)
s_l = 0 if l in (0, 1) else (v - l) / min(l, 1 - l)
return f"hsl({round(h * 360)} {round(s_l * 100)}% {round(l * 100)}%)"
h, s, v = colorsys.rgb_to_hsv(0.2, 0.4, 0.8)
print(hsv_to_css_hsl(h, s, v)) # hsl(220 60% 50%)The hue stays at 220, but saturation goes from 75% in HSV to 60% in HSL.
Common errors
- Mixing units: CSS hue is in degrees, and
colorsyshue is 0 to 1. - Passing 0 to 255 RGB values to
colorsys, which expects 0 to 1. Divide by 255 first. - Reusing an HSV saturation number as an HSL saturation. The two are not equal, so convert as shown above.
- Assuming equal lightness values look equally light. Yellow at
50%looks brighter than blue at50%, as the chapter on perceived lightness explained. Check contrast with a ratio, not by the number.
Exercise
In Python, colorsys.rgb_to_hsv(1.0, 0.0, 0.0) returns a hue of 0.0. Write the expression that gives the hue of its complement in degrees, and say what color results.
Show answer
((0.0 + 0.5) % 1.0) * 360 gives 180 degrees. Red's complement is cyan, which is hsl(180 100% 50%) in CSS.
Exercise
In the CSS example, you change --brand-hue from 210 to 30. What happens to --brand-complement?
Show answer
It becomes hsl(calc(30 + 180) 80% 45%), which is hue 210. The complement is now the original blue, and all the other variants shift to orange.