/** * The Donate button's colour for a month's progress: red at 0%, through * orange, amber, yellow and lime, to green at 100%, as a smooth turn of the * hue so every percent has its own shade. The text is white or near-black, * whichever reads better on that shade (WCAG contrast), so it stays legible * through the light yellows. */ export interface DonateColors { background: string; text: string; border: string; } const DARK_TEXT = '#1b1f2a'; const LIGHT_TEXT = '#ffffff'; /** 0 to 100, whole; anything else (unknown) counts as 0. */ export function clampPercent(p: number | null | undefined): number { return typeof p === 'number' && Number.isFinite(p) ? Math.max(0, Math.min(100, Math.floor(p))) : 0; } export function donateColors(percent: number | null | undefined): DonateColors { const p = clampPercent(percent); // Hue 0 (red) to 130 (green). Yellows are lifted a little, and the ends kept deep. const hue = p * 1.3; let lightness = 44 + 8 * Math.sin((p / 100) * Math.PI); const saturation = 82; let rgb = hslToRgb(hue, saturation, lightness); const light = contrast(rgb, [255, 255, 255]) >= contrast(rgb, hexToRgb(DARK_TEXT)); const textRgb = light ? ([255, 255, 255] as Rgb) : hexToRgb(DARK_TEXT); // Where neither text colour reaches 4.5:1 (the reds turning orange), deepen // the shade under white text, or lighten it under dark text, until one does. for (let i = 0; i < 40 && contrast(rgb, textRgb) < 4.5; i++) { lightness += light ? -0.5 : 0.5; rgb = hslToRgb(hue, saturation, lightness); } const border = toHex(hslToRgb(hue, saturation, lightness - 12)); return { background: toHex(rgb), text: light ? LIGHT_TEXT : DARK_TEXT, border }; } type Rgb = [number, number, number]; function hslToRgb(h: number, s: number, l: number): Rgb { const S = s / 100; const L = l / 100; const k = (n: number) => (n + h / 30) % 12; const a = S * Math.min(L, 1 - L); const f = (n: number) => L - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1)); return [Math.round(f(0) * 255), Math.round(f(8) * 255), Math.round(f(4) * 255)]; } function hexToRgb(hex: string): Rgb { const v = parseInt(hex.slice(1), 16); return [(v >> 16) & 255, (v >> 8) & 255, v & 255]; } function toHex([r, g, b]: Rgb): string { return '#' + [r, g, b].map((x) => x.toString(16).padStart(2, '0')).join(''); } function luminance([r, g, b]: Rgb): number { const ch = (c: number) => { const v = c / 255; return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4; }; return 0.2126 * ch(r) + 0.7152 * ch(g) + 0.0722 * ch(b); } /** WCAG contrast ratio between two colours. */ export function contrast(a: Rgb, b: Rgb): number { const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x); return (hi + 0.05) / (lo + 0.05); } export { hexToRgb }; /** What the button shows for a month's progress. */ export interface DonateState { /** Coloured "Donate N%" goal meter; false is the plain outlined "Donate". */ meter: boolean; text: string; title: string; label: string; } /** * The button's state for the figure from /api/donations/progress: plain * "Donate" in the site's own colours until it arrives, if it cannot, and * while nothing has been given this month (a red "Donate 0%" reads as a * warning); the coloured "Donate N%" meter from the first percent. */ export function donateState(percent: number | null | undefined): DonateState { const p = typeof percent === 'number' && Number.isFinite(percent) ? clampPercent(percent) : 0; if (p === 0) { return { meter: false, text: 'Donate', title: 'Support this site on Ko-fi', label: 'Donate on Ko-fi (opens in a new tab)' }; } return { meter: true, text: `Donate ${p}%`, title: `${p}% of this month's goal`, label: `Donate on Ko-fi, ${p}% of this month's goal raised (opens in a new tab)`, }; }