NGU-Web/src/lib/color.ts
Zaldimmar 79f1d09eef Try the revised palette (teal, green, amber, ink)
The suggested grid as the theme defaults, light and dark, with
PALETTE, the accents (olive becomes amber), the backdrop shades
and every literal from the old grid mapped over. Every text pair
passes the admin's 4.5:1 check; the hero's filled button is amber.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 16:51:14 -05:00

90 lines
4.4 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
COLOR
Two helpers for colors that arrive as data (a page's accent, an
event's or region's color), so they work with the theme tokens
in index.css as well as with hex.
withAlpha(color, 20) is that color at 20% opacity, for tints and
borders. It replaces `${color}33`, which only works on hex.
accentText(color) is that color as text: itself in light mode,
mixed halfway to white in dark mode (--accent-text-mix), so an
accent picked for a white page still reads on a dark one.
═══════════════════════════════════════════════════════════════ */
export const withAlpha = (color: string, percent: number): string =>
`color-mix(in srgb, ${color} ${percent}%, transparent)`
export const accentText = (color: string): string =>
`color-mix(in srgb, ${color} var(--accent-text-mix), #ffffff)`
/* Text for on top of a color that arrives as data: white or deep
ink, whichever has more contrast (WCAG), so a pale color an admin
picks still gets readable text. Anything that isn't #rrggbb (a
CSS variable, say) gets white, which suits the site's own dark
accents. */
export function readableOn(color: string): string {
const hex = /^#([0-9a-f]{6})$/i.exec(color ?? '')?.[1]
if (!hex) return '#ffffff'
const channel = (i: number) => {
const c = parseInt(hex.slice(i, i + 2), 16) / 255
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
}
const lum = 0.2126 * channel(0) + 0.7152 * channel(2) + 0.0722 * channel(4)
const INK_LUM = 0.0041 // #001011
const onWhite = 1.05 / (lum + 0.05)
const onInk = (lum + 0.05) / (INK_LUM + 0.05)
return onWhite >= onInk ? '#ffffff' : '#001011'
}
/* ── The site's accents ──────────────────────────────────────────
BRAND is the teal every page uses as its accent; it's the button
token, so the admin's palette (Site settings → Colors) reaches it.
ACCENTS are the other section colors a page may give a band: the
palette's dark shades, all readable as text on its light ones
(7:1 or better). Use these rather than a new hex. */
export const BRAND = 'var(--color-action)'
export const ACCENTS = {
teal: BRAND,
deep: '#003a45',
green: '#4a7431',
amber: '#8f5b00',
}
/* ── The palette ─────────────────────────────────────────────────
The site's 16 colors: four families, each [main, darkest, light,
lightest]. The theme tokens above are chosen from these; so is
anything drawn from the palette as data (a person's initials
tile, the page background, the admin home's sections). Each main
shade reads as text on white and takes white text on it (5:1 or
better); the darkest are for text, the light and lightest for
fills behind text. */
export const PALETTE = {
teal: ['#00748a', '#003a45', '#5cd3e6', '#d6f5fa'],
green: ['#4a7431', '#23361a', '#9cc47c', '#e6f0dd'],
amber: ['#8f5b00', '#4a2e00', '#f0bd4f', '#fdf1d6'],
ink: ['#3b5456', '#001011', '#a3b8b9', '#f5f9f8'],
} as const
/* Fills that take the page's ink text (#001011, 9:1 or better) and
fills that take white text (5:1 or better), for things colored
from the palette by data, like an initials tile. */
export const PALETTE_LIGHT = ['#d6f5fa', '#e6f0dd', '#fdf1d6', '#f0bd4f', '#9cc47c', '#5cd3e6'] as const
export const PALETTE_DARK = ['#003a45', '#23361a', '#4a2e00', '#00748a', '#4a7431', '#8f5b00', '#3b5456'] as const
/* A palette fill for something colored by its name, like the
initials tile of a person with no photo: the same name always gets
the same fill, and each fill comes with its readable text (ink on
the light ones, white on the dark ones). */
const NAMED_FILLS = [
...PALETTE_LIGHT.map((background) => ({ background, color: '#001011' })),
...PALETTE_DARK.map((background) => ({ background, color: '#ffffff' })),
]
export function paletteFill(name: string): { background: string; color: string } {
let hash = 0
for (const char of name.trim().toLowerCase()) hash = (hash * 31 + char.charCodeAt(0)) >>> 0
return NAMED_FILLS[hash % NAMED_FILLS.length]
}