Sub topic 14
Generating harmonies by rotating hue in code
This chapter assumes you know the HSL model (hue as an angle from 0 to 359, saturation and lightness as percentages) and the basic harmonies: complementary, analogous and triadic. You should also be comfortable reading short CSS and JavaScript. Here you will turn each harmony into a list of angle offsets and generate palettes from them.
The core idea: a harmony is a list of offsets
On the HSL colour wheel, hue is an angle. A harmony is a fixed set of angular distances from a base hue. Once you write those distances as numbers, generating a palette is arithmetic.
| Harmony | Offsets from the base hue (degrees) |
|---|---|
| Complementary | 0, 180 |
| Analogous | -30, 0, 30 |
| Triadic | 0, 120, 240 |
| Split-complementary | 0, 150, 210 |
| Tetradic (rectangle) | 0, 60, 180, 240 |
| Square | 0, 90, 180, 270 |
| Monochromatic | 0 (vary saturation or lightness instead) |
The offsets are the same for every base hue. Changing the base moves the whole pattern around the wheel, like turning a stencil.
Rotating hue in plain CSS
CSS can do the rotation without any JavaScript. Store the base hue in a custom property and add an offset inside calc().
:root {
--base-hue: 210;
}
.base {
background: hsl(var(--base-hue) 65% 45%);
}
.complement {
background: hsl(calc(var(--base-hue) + 180) 65% 45%);
}Changing --base-hue in one place recolours both rules. This suits themes, because a single variable drives the whole palette.
CSS wraps hue values for you. hsl(390 65% 45%) is the same colour as hsl(30 65% 45%), and negative angles also wrap. You can write calc(var(--base-hue) - 30) for an analogous neighbour and not worry about going below zero.
Here is a small analogous and triadic set built the same way:
:root {
--base-hue: 210;
--sat: 65%;
--light: 50%;
}
.analogous-left { background: hsl(calc(var(--base-hue) - 30) var(--sat) var(--light)); }
.analogous-right { background: hsl(calc(var(--base-hue) + 30) var(--sat) var(--light)); }
.triadic-second { background: hsl(calc(var(--base-hue) + 120) var(--sat) var(--light)); }
.triadic-third { background: hsl(calc(var(--base-hue) + 240) var(--sat) var(--light)); }Sharing --sat and --light keeps the colours consistent. Only the hue changes between swatches.
Rotating hue in JavaScript
When the palette must be computed, for example to feed a chart library or to run a contrast check, JavaScript needs to normalise the angle itself. Its % operator keeps the sign of the dividend, so -30 % 360 is -30, not 330.
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);
console.log(analogous);
console.log(triadic);Running this prints:
[ 'hsl(210 65% 50%)', 'hsl(30 65% 50%)' ]
[ 'hsl(180 65% 50%)', 'hsl(210 65% 50%)', 'hsl(240 65% 50%)' ]
[ 'hsl(210 65% 50%)', 'hsl(330 65% 50%)', 'hsl(90 65% 50%)' ]The expression (((baseHue + offset) % 360) + 360) % 360 works in three steps:
- Add the offset to the base hue. The sum can be negative or above 359.
- Take
% 360. The result is now between -359 and 359. - Add 360 and take
% 360again. Negative values move into range, and values already in range are unchanged.
The triadic result shows why this matters. 210 + 240 is 450, which wraps to 90.
Building the other schemes from the same function
Because a harmony is only a list of offsets, the variants cost no extra code. You can keep them in a lookup table:
const HARMONIES = {
complementary: [0, 180],
analogous: [-30, 0, 30],
triadic: [0, 120, 240],
splitComplementary: [0, 150, 210],
tetradic: [0, 60, 180, 240],
square: [0, 90, 180, 270],
};
function harmony(baseHue, offsets, saturation = 65, lightness = 50) {
return offsets.map((offset) => {
const hue = (((baseHue + offset) % 360) + 360) % 360;
return `hsl(${hue} ${saturation}% ${lightness}%)`;
});
}
function palette(baseHue, name) {
const offsets = HARMONIES[name];
if (!offsets) {
throw new Error(`Unknown harmony: ${name}`);
}
return harmony(baseHue, offsets);
}
console.log(palette(15, "splitComplementary"));This prints [ 'hsl(15 65% 50%)', 'hsl(165 65% 50%)', 'hsl(225 65% 50%)' ].
A monochromatic scheme keeps the hue and varies lightness instead, so it needs its own small function:
function monochromatic(hue, lightnessSteps = [25, 40, 55, 70, 85], saturation = 65) {
return lightnessSteps.map((l) => `hsl(${hue} ${saturation}% ${l}%)`);
}
console.log(monochromatic(210));Limits you will run into
Rotation gives you correct angles. It does not guarantee a good-looking or usable palette. Four problems come up often.
The HSL wheel is an RGB wheel
Adding 180 to a hue gives the complement on the RGB-based HSL wheel. That differs from the traditional RYB wheel taught in art classes. A base of hsl(30 …) is orange, and its HSL complement is hsl(210 …), a sky or azure blue. A painter using RYB would pair orange with a deeper blue, which is a different angle on the wheel they use. Code that rotates by 180 is correct for the wheel it uses, so tell your readers or teammates which wheel you mean.
Equal lightness is not equal brightness
At hsl(60 65% 50%) (yellow) and hsl(240 65% 50%) (blue), the lightness value is the same, but the yellow looks far brighter to the eye. A triadic or tetradic palette generated at a fixed lightness will have uneven visual weight. You can adjust lightness per colour, or use a perceptual model such as OKLCH. In OKLCH, equal lightness values look much more alike across hues, though you must still check that the resulting colours fit inside the screen's range.
Rotation ignores proportion and contrast
The function returns equal-weight colours. A real design should follow dominance, for example 60-30-10, and must pass contrast checks for text. Treat generated colours as raw material. Assign roles (background, surface, accent) afterwards, then test text pairs. For body text the WCAG minimum contrast ratio is 4.5:1.
Floating-point and fractional angles
If you generate offsets by division, such as 360 / n for n evenly spaced hues, you will get fractions. HSL accepts decimals, but round them before display or storage so that values like 119.99999999999999 do not leak into your output.
function evenlySpaced(baseHue, count) {
const step = 360 / count;
return Array.from({ length: count }, (_, i) => Math.round((baseHue + i * step) % 360));
}
console.log(evenlySpaced(210, 3)); // [ 210, 330, 90 ]
console.log(evenlySpaced(210, 5)); // [ 210, 282, 354, 66, 138 ]This handles any number of evenly spaced hues. Triadic is count = 3, square is count = 4, and the function also gives patterns the named schemes do not cover.
Choosing between CSS and JavaScript
| Situation | Better choice |
|---|---|
| Colours only used in stylesheets, a single base hue | CSS `calc()` on a custom property |
| Colours must feed a canvas, chart or contrast checker | JavaScript |
| You need to test or log the palette values | JavaScript |
| Users pick the base hue at runtime | Either; set the CSS variable from JavaScript with `element.style.setProperty("--base-hue", value)` |
Exercise
Using the harmony function from this chapter, what does harmony(30, [-60, 0, 60]) return as hue values, and why does the first value need the double-modulo step?
Show answer
The hues are 330, 30 and 90. For the first offset, 30 + (-60) is -30. A single % 360 leaves it at -30, which is not a valid hue angle. Adding 360 gives 330, and the second % 360 leaves it unchanged. The other two values, 30 and 90, were already in range.
Exercise
A teammate writes hsl(calc(var(--base-hue) + 180) 65% 45%) and says it gives the same complement a painter would choose with the RYB wheel. Is that correct? Give one example.
Show answer
No. The calculation gives the complement on the HSL (RGB-based) wheel. For a base hue of 30 (orange), it yields hue 210, an azure blue. On an RYB wheel the complement of orange is a deeper blue, which sits at a different position, so the two wheels disagree for most hues. Both are valid for their own wheel, so the team should agree which one it uses.