Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : KA Maps Engine8 * File : studio/web/src/color.ts9 * Module : studio10 * Repository : https://git.spboucher.ai/ka-maps-engine11 * Created : 2026-12-0112 * Last Modified : 2026-12-0113 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Couleurs kastyle côté interface : résolution des références de palette et des expressions43// (mélanges Oklab, comme le moteur), contraste WCAG. Sert à l'affichage ; le rendu reste celui du moteur.44export type RGBA = [number, number, number, number]; // 0–255, alpha 0–14546export function parseLiteral(s: string): RGBA | null {47 s = s.trim().toLowerCase();48 if (s === 'transparent') return [0, 0, 0, 0];49 if (s === 'black') return [0, 0, 0, 1];50 if (s === 'white') return [255, 255, 255, 1];51 let m = /^#([0-9a-f]{3,8})$/.exec(s);52 if (m) {53 const h = m[1];54 if (h.length === 3 || h.length === 4) {55 const v = [...h].map((c) => parseInt(c + c, 16));56 return [v[0], v[1], v[2], h.length === 4 ? v[3] / 255 : 1];57 }58 if (h.length === 6 || h.length === 8) return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16), h.length === 8 ? parseInt(h.slice(6, 8), 16) / 255 : 1];59 return null;60 }61 m = /^rgba?\(([^)]+)\)$/.exec(s);62 if (m) {63 const p = m[1].split(/[,\s/]+/).filter(Boolean).map(Number);64 if (p.length >= 3 && p.every(Number.isFinite)) return [p[0], p[1], p[2], p[3] ?? 1];65 }66 m = /^hsla?\(([^)]+)\)$/.exec(s);67 if (m) {68 const p = m[1].split(/[,\s/]+/).filter(Boolean).map((x) => parseFloat(x));69 if (p.length >= 3 && p.every(Number.isFinite)) {70 const [h, sat, l] = [((p[0] % 360) + 360) % 360, p[1] / 100, p[2] / 100];71 const c = (1 - Math.abs(2 * l - 1)) * sat;72 const x = c * (1 - Math.abs(((h / 60) % 2) - 1));73 const k = l - c / 2;74 const [r, g, b] = h < 60 ? [c, x, 0] : h < 120 ? [x, c, 0] : h < 180 ? [0, c, x] : h < 240 ? [0, x, c] : h < 300 ? [x, 0, c] : [c, 0, x];75 return [(r + k) * 255, (g + k) * 255, (b + k) * 255, p[3] ?? 1];76 }77 }78 return null;79}8081const toLin = (c: number) => ((c /= 255) <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);82const fromLin = (c: number) => Math.round(255 * Math.min(1, Math.max(0, c <= 0.0031308 ? c * 12.92 : 1.055 * c ** (1 / 2.4) - 0.055)));83function toOklab([r, g, b]: RGBA): [number, number, number] {84 const [lr, lg, lb] = [toLin(r), toLin(g), toLin(b)];85 const l = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb);86 const m = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb);87 const s = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb);88 return [0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s, 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s, 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s];89}90function fromOklab([L, a, b]: [number, number, number], alpha: number): RGBA {91 const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;92 const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;93 const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;94 return [fromLin(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s), fromLin(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s), fromLin(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s), alpha];95}9697/** Résout une couleur kastyle (littéral, @référence, expression) avec la palette. `null` si irrésoluble. */98export function resolveColor(v: unknown, palette: Record<string, unknown>, depth = 0): RGBA | null {99 if (depth > 16) return null;100 if (typeof v === 'string') {101 if (v.startsWith('@')) return v.slice(1) in palette ? resolveColor(palette[v.slice(1)], palette, depth + 1) : null;102 return parseLiteral(v);103 }104 if (!Array.isArray(v)) return null;105 const c = resolveColor(v[1], palette, depth + 1);106 if (!c) return null;107 switch (v[0]) {108 case 'alpha': return [c[0], c[1], c[2], Number(v[2])];109 case 'lighten': case 'darken': {110 const o = toOklab(c);111 o[0] += (v[0] === 'lighten' ? 1 : -1) * Number(v[2]);112 return fromOklab(o, c[3]);113 }114 case 'mix': {115 const d = resolveColor(v[2], palette, depth + 1);116 if (!d) return null;117 const t = Number(v[3]);118 const [x, y] = [toOklab(c), toOklab(d)];119 return fromOklab([x[0] + (y[0] - x[0]) * t, x[1] + (y[1] - x[1]) * t, x[2] + (y[2] - x[2]) * t], c[3] + (d[3] - c[3]) * t);120 }121 }122 return null;123}124125export const css = (c: RGBA | null) => (c ? `rgba(${Math.round(c[0])}, ${Math.round(c[1])}, ${Math.round(c[2])}, ${c[3]})` : 'transparent');126export const hex6 = (c: RGBA) => '#' + [c[0], c[1], c[2]].map((x) => Math.round(Math.min(255, Math.max(0, x))).toString(16).padStart(2, '0')).join('');127export const hex = (c: RGBA) => hex6(c) + (c[3] < 1 ? Math.round(c[3] * 255).toString(16).padStart(2, '0') : '');128129const lum = (c: RGBA) => 0.2126 * toLin(c[0]) + 0.7152 * toLin(c[1]) + 0.0722 * toLin(c[2]);130/** Aplatit une couleur translucide sur un fond opaque. */131export const over = (c: RGBA, bg: RGBA): RGBA => [c[0] * c[3] + bg[0] * (1 - c[3]), c[1] * c[3] + bg[1] * (1 - c[3]), c[2] * c[3] + bg[2] * (1 - c[3]), 1];132/** Rapport de contraste WCAG 2 (1 à 21) entre un avant-plan et un fond. */133export function contrast(fg: RGBA, bg: RGBA): number {134 const [a, b] = [lum(over(fg, bg)), lum(bg)];135 return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);136}137export const looksLikeColor = (v: unknown): boolean => (typeof v === 'string' && (v.startsWith('@') || parseLiteral(v) !== null)) || (Array.isArray(v) && ['mix', 'alpha', 'lighten', 'darken'].includes(v[0]));138