/** * Hex colours to hue, saturation and value and back, for the colour field of * the tag palette: the field picks saturation and value, the slider the hue. */ export interface Hsv { /** 0 to 360. */ h: number; /** 0 to 1. */ s: number; /** 0 to 1. */ v: number; } const HEX = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i; /** A typed colour as lowercase #rrggbb, or null when it is not one. Accepts the short #rgb form and a missing #. */ export function normalizeHex(raw: string): string | null { const m = HEX.exec(raw.trim()); if (!m) return null; const digits = m[1].length === 3 ? [...m[1]].map((c) => c + c).join('') : m[1]; return `#${digits.toLowerCase()}`; } export function hexToHsv(hex: string): Hsv { const norm = normalizeHex(hex) ?? '#000000'; const [r, g, b] = [1, 3, 5].map((i) => parseInt(norm.slice(i, i + 2), 16) / 255); const max = Math.max(r, g, b); const d = max - Math.min(r, g, b); let h = 0; if (d) { if (max === r) h = ((g - b) / d) % 6; else if (max === g) h = (b - r) / d + 2; else h = (r - g) / d + 4; } return { h: (h * 60 + 360) % 360, s: max ? d / max : 0, v: max }; } export function hsvToHex({ h, s, v }: Hsv): string { const f = (n: number) => { const k = (n + h / 60) % 6; const c = v - v * s * Math.max(0, Math.min(k, 4 - k, 1)); return Math.round(c * 255) .toString(16) .padStart(2, '0'); }; return `#${f(5)}${f(3)}${f(1)}`; } /** The pure hue at full saturation and value, the colour the field fades from. */ export function hueHex(h: number): string { return hsvToHex({ h, s: 1, v: 1 }); }