Sub topic 2
Hue: the family a color belongs to
This chapter assumes you have used a design or image-editing tool, know that screens mix red, green, and blue light, and can read short Python and CSS snippets. It covers hue only. Saturation and value get their own chapters.
What hue is
Hue is the property that places a color in a family: red, orange, yellow, green, blue, purple, and so on. It answers the question "which color is this?" before you ask how vivid or how light it is.
Light is made of wavelengths. Your eye reports them through three kinds of cone cells, and your brain turns that signal into a sense of color. Hue is the part of that sense that tracks which wavelengths dominate. A pale sky blue, a deep navy, and a vivid electric blue all share the same hue family. They differ in saturation and value.
To check whether two colors share a hue, ask whether one could be made from the other by only making it lighter, darker, grayer, or more vivid. If so, the hue is the same.
Hue is an angle, not a quantity
Hue has no natural "more" or "less". Red is not bigger than green. Hues are arranged around a circle, so software describes a hue as an angle in degrees from 0 to 360.
The angle scale used by HSL, HSV, and CSS starts with red at 0 and moves around the circle through the colors of the rainbow.
| Hue angle | Color name | RGB equivalent |
|---|---|---|
| 0 | Red | 255, 0, 0 |
| 30 | Orange | 255, 128, 0 |
| 60 | Yellow | 255, 255, 0 |
| 120 | Green | 0, 255, 0 |
| 180 | Cyan | 0, 255, 255 |
| 240 | Blue | 0, 0, 255 |
| 300 | Magenta | 255, 0, 255 |
The scale is circular, so 360 and 0 are the same hue. This has practical consequences, covered below.
Primary hues and where the angles come from
On screens, the three light primaries are red (0), green (120), and blue (240). They sit exactly 120 degrees apart. The hues between them come from mixing two primaries:
- Red plus green light gives yellow (60).
- Green plus blue light gives cyan (180).
- Blue plus red light gives magenta (300).
This is why the angle scale looks so regular. The hue angle is a way of describing which primary or pair of primaries dominates. In RGB terms, hue is determined by the ordering and relative spacing of the three channel values, not by their overall size.
Traditional art uses a different primary set (red, yellow, blue) for mixing paint. The same hue circle exists there, but the names and positions of the key colors differ. Digital design tools use the light-based circle, so the angles in this chapter assume it.
Computing hue from RGB
You can read the hue of any RGB color with the standard library. The colorsys module works with values from 0 to 1, so divide 8-bit channels by 255 first and multiply the returned hue by 360.
import colorsys
def hue_of(r, g, b):
"""Return (hue in degrees, saturation) for 8-bit RGB values."""
h, s, v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255)
return round(h * 360) % 360, round(s, 2)
samples = {
"red": (255, 0, 0),
"orange": (255, 128, 0),
"yellow": (255, 255, 0),
"green": (0, 255, 0),
"blue": (0, 0, 255),
"medium blue": (51, 102, 204),
"gray": (128, 128, 128),
}
for name, rgb in samples.items():
print(name, hue_of(*rgb))The output lists red at 0, orange at 30, yellow at 60, green at 120, blue at 240, and medium blue at 220. Gray comes back as hue 0 with saturation 0.
Gray deserves attention. It has no hue at all. When all three channels are equal, there is no dominant primary, so the hue is mathematically undefined. Libraries return 0 by convention. The 0 is a placeholder, and the color is not red.
Wrapping and measuring distance
Because the scale is a circle, subtraction behaves badly. A hue of 350 and a hue of 10 are both reds, only 20 degrees apart, yet 350 minus 10 gives 340. The distance between two hues is the shorter way around the circle.
def hue_distance(a, b):
"""Shortest angular distance between two hues, in degrees."""
d = abs(a - b) % 360
return min(d, 360 - d)
def rotate_hue(hue, degrees):
"""Move a hue around the circle, wrapping at 360."""
return (hue + degrees) % 360
print(hue_distance(350, 10)) # 20
print(hue_distance(0, 180)) # 180
print(rotate_hue(350, 30)) # 20
print(rotate_hue(10, -30)) # 340Python's % operator returns a non-negative result for a positive modulus, so rotate_hue(10, -30) correctly lands on 340. Not every language behaves this way. In C, JavaScript, and Java, the remainder of a negative number is negative, so you need ((x % 360) + 360) % 360.
The same wrapping applies to animation and gradients. If you interpolate from hue 350 to hue 10 as plain numbers, the color travels backward through the whole rainbow (350, 300, 240, and so on down to 10) instead of passing through the 20-degree gap. CSS Color 4 lets you choose how to interpolate, for example in hsl shorter hue or in hsl longer hue. Choose shorter for a small shift and longer only when you want the rainbow sweep.
Hue in CSS
CSS stores hue as an angle in hsl(). Putting the hue in a custom property lets you build a family of related colors from one number.
: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 --brand-hue to 140 recolors the whole set from blue to green while keeping the same relationships between lightness and saturation. hsl() accepts hues outside 0 to 360 and wraps them, so calc(var(--brand-hue) + 180) works even when the sum passes 360. A negative result is also valid.
Hue in hsl() is a unitless number of degrees, but deg, rad, grad, and turn units are all allowed. hsl(0.5turn 80% 45%) is the same as hsl(180 80% 45%).
Hue is not the whole story of how a color looks
Two colors with the same saturation and lightness numbers do not look equally bright if their hues differ. In HSL, pure yellow (hue 60) and pure blue (hue 240) both have saturation 100% and lightness 50%. Yet yellow looks far lighter than blue. The numbers do not account for how the eye responds to each hue. A later chapter returns to this gap between numeric lightness and perceived lightness. For now, the practical rule is that you cannot swap hues and expect the same contrast against a background.
Other edge cases to keep in mind:
- Hue names are fuzzy. Where "green" ends and "teal" begins is a judgment call. The angles give you precision, but any named boundary (for example, 165 versus 180) is a convention.
- Hue boundaries are uneven in perception. The 60-degree steps look evenly spaced in RGB math but not to your eyes. There is a wide stretch of colors that people call green and a narrow band they call yellow. Equal angles do not give equal perceived differences.
- Hue shifts across devices. The same RGB triple can render with a slightly different hue on a screen with a different color profile or gamut. Hue is stable inside one color space, not across all of them.
- HSL and HSV agree on hue. Both models compute hue the same way. Only saturation and the third axis differ. A hue you read in one model is valid in the other.
Common mistakes
- Treating hue 0 as "no hue". Hue 0 is red. A gray also reports 0, but only because the value is undefined.
- Subtracting hues without wrapping. Use the shorter way around the circle, as in
hue_distance. - Assuming equal angles look equal. A 30-degree shift in the blues is far less noticeable than a 30-degree shift between red and yellow.
- Mixing up paint and light primaries. Red-yellow-blue hue angles do not match the red-green-blue angles used by screens.
- Changing hue and expecting the same contrast. Moving from a blue to a yellow at the same lightness number changes how bright the color looks, so recheck legibility after any hue change.
Exercise
A brand color has hue 345. You want a color 40 degrees clockwise of it on the hue circle (increasing angle). What hue number do you use, and what is the shortest distance between the two hues?
Show answer
Rotate by adding 40 and wrapping: (345 + 40) % 360 = 25. The shortest distance between 345 and 25 is 40 degrees. A plain subtraction gives 320, which is the long way around the circle, so use min(320, 360 - 320) = 40.
Exercise
The function colorsys.rgbtohsv returns a hue of 0 for the RGB color (90, 90, 90). Is this color red? Explain.
Show answer
No. All three channels are equal, so the color is a neutral gray with saturation 0. Its hue is undefined, and 0 is only the default the library returns. You should check saturation before interpreting a hue value.
Exercise
In the CSS example, what happens to --brand-complement if you set --brand-hue to 300?
Show answer
calc(300 + 180) gives 480. The hsl() function wraps hue, so 480 is the same as 120, which is green. The complement of magenta (300) is green (120), as expected.