Sub topic 14
Generating harmonies by rotating hue in code
This chapter assumes you know what hue, saturation and lightness mean, and that you know the three harmonies: complementary, analogous and triadic. It also assumes you can read basic CSS and JavaScript.
The idea
Every harmony is a set of fixed angles on the colour wheel. In the HSL model, hue is an angle from 0 to 359 degrees. To build a palette, pick a base hue and add an offset for each colour. Keep saturation and lightness the same so the colours feel like one family.
| Harmony | Hue offsets from the base |
|---|---|
| Complementary | 0, 180 |
| Analogous | -30, 0, 30 |
| Triadic | 0, 120, 240 |
HSL uses the RGB-style wheel, so these angles give the RGB complement, not the RYB one. A red-based palette will not pair with green the way a painter's wheel would.
Rotating hue in CSS
CSS can do the arithmetic for you with custom properties and calc().
:root {
--base-hue: 210;
}
.base {
background: hsl(var(--base-hue) 65% 45%);
}
.complement {
background: hsl(calc(var(--base-hue) + 180) 65% 45%);
}With a base of 210, the complement hue is 390. CSS treats hue as a circular angle, so 390 is the same as 30. Changing --base-hue in one place recolours every element that uses it.
Rotating hue in JavaScript
Use JavaScript when you need the colours as data, for example to draw swatches or export a palette. Here the wrapping is your job.
function harmony(baseHue, offsets, saturation = 65, lightness = 50) {
return offsets.map((offset) => {
const hue = (((baseHue + offset) % 360) + 360) % 360;
return `hsl(${hue} ${saturation}% ${lightness}%)`;
});
}
const complementary = harmony(210, [0, 180]);
const analogous = harmony(210, [-30, 0, 30]);
const triadic = harmony(210, [0, 120, 240]);
console.log(complementary); // ["hsl(210 65% 50%)", "hsl(30 65% 50%)"]
console.log(analogous); // ["hsl(180 65% 50%)", "hsl(210 65% 50%)", "hsl(240 65% 50%)"]
console.log(triadic); // ["hsl(210 65% 50%)", "hsl(330 65% 50%)", "hsl(90 65% 50%)"]Why the double modulo
The expression (((baseHue + offset) % 360) + 360) % 360 handles two cases.
- A sum above 359, such as 210 + 240 = 450, wraps to 90.
- A negative sum, such as 10 - 30 = -20, is a problem. In JavaScript,
-20 % 360is-20, not340. Adding 360 makes it positive, and the second% 360stops values that were already positive from exceeding 359.
Making the palette usable
Rotating hue does not guarantee a readable palette. Because the function holds saturation and lightness constant, every colour gets the same lightness, so text on a swatch may fail contrast checks. Vary lightness per role instead. For example, use a dark shade for text and a light tint for backgrounds, then run a contrast check. You can also pass a different lightness to each call of harmony.
Exercise
What does harmony(20, [-30, 0, 30]) return for hue values, and why would the expression ((20 - 30) % 360) alone be a problem?
Show answer
The hues are 350, 20 and 50. Without the extra "+ 360", the first value would be -10, because JavaScript's % operator keeps the sign of the dividend. Adding 360 and applying % 360 again gives 350.