Sub topic 2
Hue: the family a color belongs to
This chapter assumes you know that screens mix red, green, and blue light, and that you can read short code snippets. It covers hue only. Saturation and value get their own chapters.
What hue is
Hue is the property that makes a color "red", "green", or "blue". If you ask someone to name a color and they answer with a plain color word, they are naming its hue. Two colors share a hue when they belong to the same family, even if one is pale and the other dark, or one is vivid and the other dull.
Hue is separate from the other two properties:
- A pale pink and a deep maroon have the same hue (red) but different value.
- A vivid red and a grayish red have the same hue but different saturation.
- A red and a green can have identical saturation and value and still differ in hue.
Hue is also the only one of the three properties that is circular. Going from red through orange, yellow, green, cyan, blue, and magenta brings you back to red. There is no "most hue" or "least hue", so software stores it as an angle.
Hue as an angle
Most tools measure hue in degrees from 0 to 360. The values below are the standard anchor points in HSL and HSV.
| Hue angle | Color family |
|---|---|
| 0 | Red |
| 60 | Yellow |
| 120 | Green |
| 180 | Cyan |
| 240 | Blue |
| 300 | Magenta |
| 360 | Red again (same as 0) |
Because the scale wraps around, 370 degrees is the same as 10 degrees, and -30 degrees is the same as 330 degrees. Code that shifts hue must wrap the result back into range.
Reading hue from a color
You rarely need to compute hue by hand, but a rough rule helps. In RGB, the hue depends on which channel is largest and which is smallest. When red is highest and blue is lowest, the hue lies between magenta and yellow, on the red-to-yellow side if green is above blue. The exact angle comes from how far the middle channel sits between the other two.
When all three channels are equal, the color is a gray. A gray has no meaningful hue, and tools usually report it as 0. This matters in code: do not treat the hue of a gray as a real measurement.
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)) # 220
# A gray: all channels equal, so hue is reported as 0
h, s, v = colorsys.rgb_to_hsv(0.5, 0.5, 0.5)
print(round(h * 360)) # 0The colorsys module returns hue as a fraction of a full turn, so multiplying by 360 gives degrees. A hue of 220 sits between cyan (180) and blue (240), which matches the blue-leaning color we started with.
Hue in CSS
CSS accepts hue directly in the hsl() function. Changing only the first number changes the family and nothing else, which makes hue a convenient single control.
.swatch-red { background: hsl(0 80% 50%); }
.swatch-green { background: hsl(120 80% 50%); }
.swatch-blue { background: hsl(240 80% 50%); }All three swatches share the same saturation and lightness numbers. They differ only in hue.
Common errors
- Assuming equal numbers look equally bright. Yellow at
hsl(60 80% 50%)looks much lighter than blue athsl(240 80% 50%), even though the lightness number is the same. A later chapter covers this. - Forgetting to wrap. Adding 180 to a hue of 210 gives 390. CSS wraps this for you, but many programming environments do not. Use a modulo, for example
(hue + 180) % 360. - Mixing units. CSS uses degrees, while
colorsysuses a 0 to 1 fraction. A hue of 0.5 incolorsysis 180 degrees. - Trusting the hue of a gray. When saturation is zero, the hue value is meaningless. Changing it changes nothing visible.
Exercise
Exercise
A color has hue 20 in HSL. You shift its hue by -50 degrees. What is the new hue after wrapping into the 0 to 360 range, and which color family is it closest to?
Show answer
20 - 50 = -30, which wraps to 330. A hue of 330 lies between magenta (300) and red (360), so it is a pinkish red.