/advanced-color-math
When to Use
- Generating multi-stop palettes programmatically (brand → 10-step scale)
- Dark mode: finding perceptually equal light/dark pairs
- Enforcing WCAG AA/AAA contrast ratios automatically
- CSS
color-mix() and oklch() in production
- Extracting dominant color from user-uploaded images
Do NOT use for
- Static palette picking (use a color tool instead)
- Consumer branding decisions (perceptual math ≠ brand taste)
Color Space Decision Tree
Need gradient? → oklch() — uniform hue, no "muddy middle"
Need dark-mode pair? → LCH lightness mirror (L=30↔L=70)
Need contrast check? → WCAG relative luminance (sRGB → linear)
Need gamut mapping? → CSS color-gamut: p3 + @supports
Need tint/shade scale? → Ant Design algorithm (HSB rotation)
OKLAB/OKLCH Perceptual Gradient (culori + bottosson/oklab)
import { interpolate, formatHex } from 'culori'
// OKLCH gradient — hue stays vivid, no gray muddy middle
const gradient = interpolate(['#3b82f6', '#ec4899'], 'oklch')
const stops = Array.from({ length: 11 }, (_, i) => formatHex(gradient(i / 10)))
// → smooth blue-to-pink, no desaturated midpoint
// CSS native (Chrome 111+)
background: linear-gradient(to right,
oklch(62% 0.2 264),
oklch(64% 0.28 0)
);
Dark-Mode Pair Algorithm (Atlassian + Radix UI)
import { wcagContrast } from 'culori'
// Mirror at L=50 in OKLCH, then nudge for contrast
function darkPair(lightHex) {
const c = oklch(lightHex)
const darkL = c.l > 0.5 ? 1 - c.l + 0.05 : c.l
const candidate = { mode: 'oklch', l: darkL, c: c.c, h: c.h }
// Enforce 4.5:1 on dark bg (#1a1a1a = L≈0.12)
return wcagContrast(candidate, '#1a1a1a') >= 4.5
? formatHex(candidate)
: lighten(candidate, 0.1) // nudge until passing
}
Ant Design 10-Step Palette Algorithm
// Hue rotation: darks shift -4°, lights shift +10°
// Saturation: darks -9%, lights pinned at input S
// Brightness: 10 steps from 95% (lightest) → input B (darkest)
function generatePalette(hex) {
const [h, s, b] = toHSB(hex)
return Array.from({ length: 10 }, (_, i) => {
const t = i / 9
const hShift = t < 0.5 ? h + 10 * (1 - t * 2) : h - 4 * (t - 0.5) * 2
const sVal = t < 0.5 ? s * (t * 2) : s
const bVal = 95 - (95 - b) * t
return fromHSB(hShift, sVal, bVal)
})
}
WCAG Contrast Enforcement (tinycolor2 / culori)
import { wcagContrast } from 'culori'
function enforceContrast(fg, bg, level = 'AA') {
const min = level === 'AAA' ? 7 : 4.5
let candidate = fg
while (wcagContrast(candidate, bg) < min) {
// Darken fg by 5% OKLCH lightness
candidate = adjust(candidate, { l: -0.05 })
if (oklch(candidate).l < 0.05) break // give up at pure black
}
return formatHex(candidate)
}
// Radix UI approach: pre-compute all 12-step scales so any fg/bg combo passes
CSS color-mix() + Gamut Mapping
/* CSS native blend — no JS needed */
.badge-muted {
background: color-mix(in oklch, var(--color-primary) 20%, transparent);
}
/* P3 wide gamut with sRGB fallback */
.vibrant {
color: #ff6b6b; /* sRGB fallback */
color: color(display-p3 1 0.3 0.3); /* wider gamut */
}
/* Gamut-safe oklch */
@supports (color: oklch(0 0 0)) {
.vibrant { color: oklch(65% 0.28 27); }
}
Image Color Extraction (vibrant.js / color-thief pattern)
import Vibrant from 'node-vibrant'
const palette = await Vibrant.from(imageUrl).getPalette()
// Swatches: Vibrant, LightVibrant, DarkVibrant, Muted, LightMuted, DarkMuted
const primary = palette.Vibrant?.hex ?? '#3b82f6'
const '#fff') >= 4.5 ? '#fff' : '#000'
// nivo uses this pattern for chart auto-coloring from data
Anti-Fake-Pass Checklist
❌ Gradient interpolated in sRGB (desaturated gray midpoint)
❌ Dark-mode color picked visually without contrast check
❌ Palette steps with identical perceived lightness
❌ color-mix() used without sRGB fallback
❌ Image color extraction used without WCAG check on extracted color
❌ oklch() used in production without @supports guard
1---2name: advanced-color-math3description: "Advanced color science and dynamic palette generation from 20 production repos. LCH/OKLAB perceptual math, dark-mode pair algorithms, contrast enforcement, gamut mapping, CSS color-mix(), and color extraction from images. Sources: chroma.js, culori, color.js, tinycolor2, d3-color, radix-ui/colors, primer/primitives, open-color, Ant Design palette, Atlassian color, Shopify Polaris tokens, vanilla-colorful, colorette, color2k, react-colorful, polychrome, bottosson/oklab, values.css, khroma, nivo/colors."4origin: yana-ai — synthesized from gka/chroma.js, Evercoder/culori, nicowillis/colorjs, bgrins/tinycolor, d3/d3-color, radix-ui/colors, primer/primitives, yeun/open-color, ant-design/ant-design (palette algo), atlassian/atlassian-frontend-mirror, Shopify/polaris-tokens, web-padawan/vanilla-colorful, jorgebucaran/colorette, ricokahler/color2k, omgovich/react-colorful, Adechizy/polychrome, bottosson/oklab, values-css/values.css, google/khroma, plouc/nivo5license: Apache-2.06version: 1.0.07compatibility: yana-ai >= 1.3.378metadata:9 source: https://skillmd.com/skills/yanacuti1121/advanced-color-math10---1112# /advanced-color-math1314## When to Use1516- Generating multi-stop palettes programmatically (brand → 10-step scale)17- Dark mode: finding perceptually equal light/dark pairs18- Enforcing WCAG AA/AAA contrast ratios automatically19- CSS `color-mix()` and `oklch()` in production20- Extracting dominant color from user-uploaded images2122## Do NOT use for2324- Static palette picking (use a color tool instead)25- Consumer branding decisions (perceptual math ≠ brand taste)2627---2829## Color Space Decision Tree3031```32Need gradient? → oklch() — uniform hue, no "muddy middle"33Need dark-mode pair? → LCH lightness mirror (L=30↔L=70)34Need contrast check? → WCAG relative luminance (sRGB → linear)35Need gamut mapping? → CSS color-gamut: p3 + @supports36Need tint/shade scale? → Ant Design algorithm (HSB rotation)37```3839---4041## OKLAB/OKLCH Perceptual Gradient (culori + bottosson/oklab)4243```javascript44import { interpolate, formatHex } from 'culori'4546// OKLCH gradient — hue stays vivid, no gray muddy middle47const gradient = interpolate(['#3b82f6', '#ec4899'], 'oklch')48const stops = Array.from({ length: 11 }, (_, i) => formatHex(gradient(i / 10)))49// → smooth blue-to-pink, no desaturated midpoint5051// CSS native (Chrome 111+)52background: linear-gradient(to right,53 oklch(62% 0.2 264),54 oklch(64% 0.28 0)55);56```5758---5960## Dark-Mode Pair Algorithm (Atlassian + Radix UI)6162```javascript63import { wcagContrast } from 'culori'6465// Mirror at L=50 in OKLCH, then nudge for contrast66function darkPair(lightHex) {67 const c = oklch(lightHex)68 const darkL = c.l > 0.5 ? 1 - c.l + 0.05 : c.l69 const candidate = { mode: 'oklch', l: darkL, c: c.c, h: c.h }70 // Enforce 4.5:1 on dark bg (#1a1a1a = L≈0.12)71 return wcagContrast(candidate, '#1a1a1a') >= 4.572 ? formatHex(candidate)73 : lighten(candidate, 0.1) // nudge until passing74}75```7677---7879## Ant Design 10-Step Palette Algorithm8081```javascript82// Hue rotation: darks shift -4°, lights shift +10°83// Saturation: darks -9%, lights pinned at input S84// Brightness: 10 steps from 95% (lightest) → input B (darkest)85function generatePalette(hex) {86 const [h, s, b] = toHSB(hex)87 return Array.from({ length: 10 }, (_, i) => {88 const t = i / 989 const hShift = t < 0.5 ? h + 10 * (1 - t * 2) : h - 4 * (t - 0.5) * 290 const sVal = t < 0.5 ? s * (t * 2) : s91 const bVal = 95 - (95 - b) * t92 return fromHSB(hShift, sVal, bVal)93 })94}95```9697---9899## WCAG Contrast Enforcement (tinycolor2 / culori)100101```javascript102import { wcagContrast } from 'culori'103104function enforceContrast(fg, bg, level = 'AA') {105 const min = level === 'AAA' ? 7 : 4.5106 let candidate = fg107 while (wcagContrast(candidate, bg) < min) {108 // Darken fg by 5% OKLCH lightness109 candidate = adjust(candidate, { l: -0.05 })110 if (oklch(candidate).l < 0.05) break // give up at pure black111 }112 return formatHex(candidate)113}114115// Radix UI approach: pre-compute all 12-step scales so any fg/bg combo passes116```117118---119120## CSS color-mix() + Gamut Mapping121122```css123/* CSS native blend — no JS needed */124.badge-muted {125 background: color-mix(in oklch, var(--color-primary) 20%, transparent);126}127128/* P3 wide gamut with sRGB fallback */129.vibrant {130 color: #ff6b6b; /* sRGB fallback */131 color: color(display-p3 1 0.3 0.3); /* wider gamut */132}133134/* Gamut-safe oklch */135@supports (color: oklch(0 0 0)) {136 .vibrant { color: oklch(65% 0.28 27); }137}138```139140---141142## Image Color Extraction (vibrant.js / color-thief pattern)143144```javascript145import Vibrant from 'node-vibrant'146147const palette = await Vibrant.from(imageUrl).getPalette()148// Swatches: Vibrant, LightVibrant, DarkVibrant, Muted, LightMuted, DarkMuted149const primary = palette.Vibrant?.hex ?? '#3b82f6'150const onPrimary = wcagContrast(primary, '#fff') >= 4.5 ? '#fff' : '#000'151152// nivo uses this pattern for chart auto-coloring from data153```154155---156157## Anti-Fake-Pass Checklist158159```160❌ Gradient interpolated in sRGB (desaturated gray midpoint)161❌ Dark-mode color picked visually without contrast check162❌ Palette steps with identical perceived lightness163❌ color-mix() used without sRGB fallback164❌ Image color extraction used without WCAG check on extracted color165❌ oklch() used in production without @supports guard166```