Replaced by an explicit list of forms only (ten exported names, runtime env names, project file names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are not touched. package.json keeps the five old bin names beside the five new ones. The old names stop working in this commit and come back as aliases in the next; docs follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
80 lines
3.7 KiB
TypeScript
80 lines
3.7 KiB
TypeScript
import { type ColorRole, INVERSE_SCOPE } from './catalog.js'
|
|
import { contrastRatio, parseColor } from './color.js'
|
|
import { resolveColors, type TokensInput } from './define.js'
|
|
|
|
/** One checked pair. `level: 'warn'` pairs are reported but do not fail the check. */
|
|
export type ContrastRow = {
|
|
bg: string
|
|
fg: string
|
|
level: 'error' | 'warn'
|
|
min: number
|
|
pass: boolean
|
|
/** null = a colour could not be parsed (reported, not failed). */
|
|
ratio: null | number
|
|
scope: 'default' | 'inverse'
|
|
theme: 'dark' | 'light'
|
|
}
|
|
|
|
type Pair = { bg: ColorRole; fg: ColorRole; level?: 'warn'; min: number }
|
|
|
|
/** Text 4.5:1 (WCAG 1.4.3 AA), focus rings and UI parts 3:1 (1.4.11). */
|
|
const PAIRS: Pair[] = [
|
|
...(['bg', 'surface', 'surface-muted', 'surface-raised'] as const).flatMap((bg) => [
|
|
{ bg, fg: 'text' as const, min: 4.5 },
|
|
{ bg, fg: 'text-strong' as const, min: 4.5 },
|
|
{ bg, fg: 'text-muted' as const, min: 4.5 },
|
|
{ bg, fg: 'link' as const, min: 4.5 },
|
|
]),
|
|
{ bg: 'primary', fg: 'primary-text', min: 4.5 },
|
|
{ bg: 'primary-hover', fg: 'primary-text', min: 4.5 },
|
|
{ bg: 'secondary', fg: 'secondary-text', min: 4.5 },
|
|
{ bg: 'accent', fg: 'accent-text', min: 4.5 },
|
|
{ bg: 'surface-inverse', fg: 'text-inverse', min: 4.5 },
|
|
{ bg: 'surface-inverse', fg: 'link-inverse', min: 4.5 },
|
|
{ bg: 'bg', fg: 'focus', min: 3 },
|
|
{ bg: 'surface-inverse', fg: 'focus-inverse', min: 3 },
|
|
...(['success', 'warning', 'danger', 'info'] as const).flatMap((state) => [
|
|
{ bg: 'bg' as const, fg: state, min: 4.5 },
|
|
{ bg: `${state}-surface` as const, fg: state, min: 4.5 },
|
|
]),
|
|
]
|
|
|
|
/** Content inside an inverted section: muted text and links on the inverse surface. */
|
|
const INVERSE_PAIRS: Pair[] = [
|
|
// primary is re-mapped to primary-inverse: a dark brand colour needs a lighter one here
|
|
{ bg: 'surface-inverse', fg: 'primary', level: 'warn', min: 3 },
|
|
{ bg: 'primary', fg: 'primary-text', min: 4.5 },
|
|
{ bg: 'surface-inverse', fg: 'text-muted', min: 4.5 },
|
|
{ bg: 'surface', fg: 'text', min: 4.5 },
|
|
{ bg: 'surface', fg: 'link', min: 4.5 },
|
|
]
|
|
|
|
/**
|
|
* WCAG contrast for every text/background pair the token roles promise, in each
|
|
* theme and inside the inverse scope. `iapl-tokens` prints it and fails on an error row.
|
|
*/
|
|
export function contrastReport(input: TokensInput = {}): ContrastRow[] {
|
|
const themes = input.theme === 'light' || input.theme === 'dark' ? [input.theme] : (['light', 'dark'] as const)
|
|
const resolved = resolveColors(input)
|
|
return themes.flatMap((theme) => {
|
|
const colors: Record<string, string> = resolved[theme]
|
|
const outer = (name: string) => (name.startsWith('ipal-') ? colors[name.slice(5)] : undefined)
|
|
// Roles the scope does not declare are inherited, i.e. computed on the ancestor: resolve them there first.
|
|
const inherited = Object.fromEntries(
|
|
Object.entries(colors).flatMap(([role, value]) => {
|
|
const rgb = parseColor(value, outer)
|
|
return rgb ? [[role, `rgb(${rgb.r * 255} ${rgb.g * 255} ${rgb.b * 255} / ${rgb.a})`]] : []
|
|
}),
|
|
)
|
|
const inverse: Record<string, string> = { ...inherited, ...INVERSE_SCOPE }
|
|
const check = (map: Record<string, string>, scope: ContrastRow['scope']) => (pair: Pair): ContrastRow => {
|
|
const lookup = (name: string) => (name.startsWith('ipal-') ? map[name.slice(5)] : undefined)
|
|
const fg = parseColor(map[pair.fg], lookup)
|
|
const bg = parseColor(map[pair.bg], lookup)
|
|
const ratio = fg && bg ? Math.round(contrastRatio(fg, bg) * 100) / 100 : null
|
|
return { bg: pair.bg, fg: pair.fg, level: pair.level ?? 'error', min: pair.min, pass: ratio === null || ratio >= pair.min, ratio, scope, theme }
|
|
}
|
|
return [...PAIRS.map(check(colors, 'default')), ...INVERSE_PAIRS.map(check(inverse, 'inverse'))]
|
|
})
|
|
}
|