NGU-Web/src/lib/color.ts
Zaldimmar d5428297a4 Timeline years as leaves, green now and turning autumn going back; notices instead of dashed boxes
A year's leaf eases from green to gold to autumn between this year
and 2000 (leafColor in color.ts); every year before 2000 is fully
turned. Empty-section and form notes use one notice style, a flat
tinted panel with a solid left bar.

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

112 lines
5.6 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: '#004c52',
green: '#00521b',
olive: '#27371b',
}
/* ── The palette ─────────────────────────────────────────────────
The site's 16 colors: four families, each a column of the grid
(its primary first). 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). Light
shades are for fills behind text, dark shades for text and fills
under white text; the primaries are for decoration only (#008fa8
is 3.8:1 on the page, #88b668 2.3:1). */
export const PALETTE = {
teal: ['#008fa8', '#004552', '#5ce7ff', '#c2f6ff'],
green: ['#88b668', '#dfebd6', '#aac992', '#27371b'],
mint: ['#fcfffd', '#c2ffd6', '#00b83d', '#00521b'],
ink: ['#001011', '#004c52', '#5cf4ff', '#c2fbff'],
} as const
/* Fills that take the page's ink text (#001011, 15:1 or better) and
fills that take white text (9:1 or better), for things colored
from the palette by data, like an initials tile. */
export const PALETTE_LIGHT = ['#c2f6ff', '#dfebd6', '#c2ffd6', '#c2fbff', '#aac992', '#5ce7ff'] as const
export const PALETTE_DARK = ['#004552', '#27371b', '#00521b', '#004c52'] 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]
}
/* ── Timeline leaves ─────────────────────────────────────────────
Each year on a timeline is a leaf, green for this year and
turning autumn as the years go back: gold, then orange, reaching
full autumn at 2000; every year before 2000 is fully turned. The
green is the palette's, deepened so a leaf stands out on the
page; the autumn shades are the one set of colors outside the
palette, since the palette has none. */
const LEAF_GREEN = 'color-mix(in oklch, #00b83d 60%, #00521b)'
const LEAF_GOLD = '#c98a0b'
const LEAF_AUTUMN = '#b8461b'
const LEAF_TURNED_BY = 2000
export function leafColor(year: number, thisYear = new Date().getFullYear()): string {
if (year < LEAF_TURNED_BY) return LEAF_AUTUMN
const span = Math.max(1, thisYear - LEAF_TURNED_BY)
const turned = Math.min(1, Math.max(0, (thisYear - year) / span))
// Green to gold over the first half of the span, gold to autumn over the second.
return turned <= 0.5
? `color-mix(in oklch, ${LEAF_GOLD} ${Math.round(turned * 200)}%, ${LEAF_GREEN})`
: `color-mix(in oklch, ${LEAF_AUTUMN} ${Math.round((turned - 0.5) * 200)}%, ${LEAF_GOLD})`
}