xonecas.io
8058
Ir

irvc / files

irvc / web / src / lib / scheme.ts

4.8 KB
1import type { Token } from "./theme";
2
3// A terminal color scheme as emitted by scripts/schemes.mjs: background,
4// foreground and the 16 ANSI colors (black..white, then the bright row).
5export interface Scheme {
6  n: string;
7  bg: string;
8  fg: string;
9  c: string[];
10}
11
12type RGB = [number, number, number];
13
14const rgb = (hex: string): RGB => [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)) as RGB;
15const hex = (c: RGB) =>
16  "#" +
17  c
18    .map((n) =>
19      Math.round(Math.max(0, Math.min(255, n)))
20        .toString(16)
21        .padStart(2, "0"),
22    )
23    .join("");
24
25// mix blends a toward b; t=0 is a, t=1 is b.
26const mix = (a: string, b: string, t: number) => {
27  const [x, y] = [rgb(a), rgb(b)];
28  return hex(x.map((v, i) => v + (y[i] - v) * t) as RGB);
29};
30
31function luminance(h: string) {
32  const [r, g, b] = rgb(h).map((v) => {
33    v /= 255;
34    return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
35  });
36  return 0.2126 * r + 0.7152 * g + 0.0722 * b;
37}
38
39function contrast(a: string, b: string) {
40  const [x, y] = [luminance(a), luminance(b)].sort((p, q) => q - p);
41  return (x + 0.05) / (y + 0.05);
42}
43
44// chroma is how colorful a color is (0 = gray); used to prefer vivid colors.
45const chroma = (h: string) => {
46  const c = rgb(h);
47  return (Math.max(...c) - Math.min(...c)) / 255;
48};
49
50export const isDark = (s: Scheme) => luminance(s.bg) < 0.3;
51
52// The chromatic ANSI slots: red..cyan (and magenta), normal and bright.
53// Black and white are skipped; they are the background/foreground's job.
54const COLORED = [1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14];
55
56// toPalette maps a scheme onto our palette tokens. Surfaces are tints of the
57// background toward the foreground, so any scheme gets a consistent stack.
58export function toPalette(s: Scheme): Record<Token, string> {
59  const { bg, fg, c } = s;
60  const ok = (h: string) => contrast(h, bg) >= 3;
61  const accent =
62    COLORED.map((i) => c[i])
63      .filter(ok)
64      .sort((a, b) => chroma(b) - chroma(a))[0] ?? fg;
65  const reds = [c[1], c[9]].sort((a, b) => contrast(b, bg) - contrast(a, bg));
66  return {
67    bg,
68    surface: mix(bg, fg, 0.05),
69    raised: mix(bg, fg, 0.09),
70    fg,
71    muted: mix(fg, bg, 0.35),
72    faint: mix(fg, bg, 0.6),
73    line: mix(bg, fg, 0.18),
74    accent,
75    accentFg: contrast(bg, accent) >= contrast(fg, accent) ? bg : fg,
76    danger: reds[0],
77  };
78}
79
80// The colors a scheme contributes to skittles: its chromatic slots, deduped.
81export const schemeColors = (s: Scheme) =>
82  COLORED.map((i) => s.c[i]).filter((h, i, all) => all.indexOf(h) === i);
83
84export const SKITTLES = 12;
85
86const hsl = (h: number, s: number, l: number) => {
87  const k = (n: number) => (n + h / 30) % 12;
88  const a = s * Math.min(l, 1 - l);
89  const f = (n: number) => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
90  return hex([f(0) * 255, f(8) * 255, f(4) * 255]);
91};
92
93// presetWheel builds the skittles for a built-in palette: tuxedo is grayscale,
94// so it gets a ramp of grays; anything else (nicotine) gets a muted hue wheel
95// pulled toward the foreground, which gives it the same sepia cast as the UI.
96function presetWheel(preset: string | undefined, dark: boolean, fg: string) {
97  return Array.from({ length: SKITTLES }, (_, i) => {
98    if (preset === "tuxedo") {
99      const l = dark ? 0.42 + (0.5 * i) / (SKITTLES - 1) : 0.1 + (0.4 * i) / (SKITTLES - 1);
100      return hsl(0, 0, l);
101    }
102    return mix(hsl(i * 30 + 8, 0.45, dark ? 0.66 : 0.36), fg, 0.25);
103  });
104}
105
106// skittlesPalette returns exactly SKITTLES colors readable against bg: the
107// scheme's own colors when it has them (nudged toward fg until legible),
108// padded with an evenly spaced hue wheel so every scheme gets a full set.
109export function skittlesPalette(
110  source: string[] | undefined,
111  bg: string,
112  fg: string,
113  preset?: string,
114) {
115  const dark = luminance(bg) < 0.3;
116  // Without a scheme the preset supplies the whole set, in its own character.
117  if (!source) return presetWheel(preset, dark, fg);
118  const wheel = Array.from({ length: SKITTLES }, (_, i) =>
119    hsl(i * 30 + 8, 0.6, dark ? 0.68 : 0.36),
120  );
121  const mine = (source ?? [])
122    .filter((h) => chroma(h) > 0.15)
123    .map((h) => {
124      for (let t = 0; t < 1 && contrast(h, bg) < 3.5; t += 0.1) h = mix(h, fg, 0.1);
125      return h;
126    });
127  const out = [...mine];
128  for (let i = 0; out.length < SKITTLES; i++) out.push(wheel[i % wheel.length]);
129  return out.slice(0, SKITTLES);
130}
131
132// userColor is a CSS color for a username: stable across messages, sessions and
133// devices. It reads --user-N, which applyTheme only defines while skittles is on,
134// so with it off the fallback applies and nothing else needs to know.
135export function userColor(name: string, fallback = "inherit") {
136  let h = 0x811c9dc5; // FNV-1a
137  for (let i = 0; i < name.length; i++) h = Math.imul(h ^ name.charCodeAt(i), 0x01000193);
138  return `var(--user-${(h >>> 0) % SKITTLES}, ${fallback})`;
139}