Draw charts from one categorical palette, --chart-1…8

The one-hue ramp told lines apart by lightness alone. --chart-1 is now
--primary and slots 2-8 are distinct hues (orange, violet, green, orchid,
ochre, teal, magenta) that clear 3:1 on --card and stay apart under
protanopia and deuteranopia; red and sage are left out as the status
colours. A chart takes the slots in order instead of spreading a ramp,
and a dashboard's own palette continues after its primary and listed
colours with slots 2-8 rather than tints of its scheme. Line charts stay
at five series. Tokens mirror index's, as design-check requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013GAeg4Jo6xjZzjwy9QDWMQ
This commit is contained in:
2026-10-06 07:05:09 +02:00
co-authored by Claude Opus 5.5
parent d7ca933478
commit 4f4de1fd9d
12 changed files with 151 additions and 149 deletions
@@ -125,7 +125,7 @@ export type BarReading = {
fraction: number
precision: number | null
unit?: string
/** A colour, or a slot of the app's ramp — whatever the palette named. */
/** A colour, or a slot of the app's palette — whatever was named. */
color: string
}
@@ -15,12 +15,12 @@
*/
import assert from "node:assert/strict"
import { readFileSync } from "node:fs"
import {
contrast,
isLight,
luminance,
MAX_ROLES,
mix,
onColor,
parsePalette,
@@ -96,14 +96,34 @@ assert.equal(named.surface, CHOSEN[1])
assert.equal(named.primary, CHOSEN[2])
assert.equal(named.accent, CHOSEN[3])
assert.equal(named.foreground, CHOSEN[4])
assert.deepEqual(
named.series.slice(0, 2),
[CHOSEN[2], CHOSEN[3]],
"a chart draws with the primary first, then the accent",
assert.equal(named.series[0], CHOSEN[2], "a chart draws with the primary first")
assert.ok(
!named.series.includes(CHOSEN[3]),
"the accent is a role, not a data colour",
)
const sixth = rolesOf([...CHOSEN, "#8ab17d"])
assert.equal(sixth?.series[2], "#8ab17d", "a sixth colour is the third series")
assert.equal(sixth?.series[1], "#8ab17d", "a sixth colour is the second series")
// After the palette's own, the shared hues: the literals `theme.ts` carries
// for a canvas are the ones `index.css` declares, theme by theme.
const css = readFileSync(
new URL("../../../../index.css", import.meta.url),
"utf8",
)
const block = (opener: string) => css.slice(css.indexOf(opener)).split("}")[0]
const slots = (text: string) =>
[...text.matchAll(/--chart-([2-8]): (#[0-9a-f]{6});/g)].map((m) => m[2])
assert.deepEqual(
rolesOf(["#ffffff"])?.series.slice(1),
slots(block(":root {")),
"a light dashboard continues with the light --chart-2…8",
)
assert.deepEqual(
rolesOf(["#0a0a0a"])?.series.slice(1),
slots(block(".dark {")),
"a dark dashboard continues with the dark --chart-2…8",
)
const dark = rolesOf(["#0f172a"])
assert.ok(dark)
@@ -126,8 +146,8 @@ assert.equal(rolesOf([]), null, "no colours is no palette")
for (const roles of [named, dark, light, sixth]) {
assert.ok(roles)
assert.ok(
roles.series.length >= MAX_ROLES,
"a chart of five lines never has to repeat a colour",
roles.series.length >= 8,
"a chart never has to repeat a colour before its ninth line",
)
}
@@ -4,6 +4,7 @@
* A palette is an ordered list of hex colours — pasted from a coolors.co link
* or typed — and position is the role: background, surface, primary, accent,
* text, then anything after that is another colour for a chart to draw with.
* A chart starts on the primary, then those, then the shared palette's own.
* Trailing roles may simply be left off; what is missing is derived from what
* is there, so three colours are a whole dashboard.
*
@@ -39,6 +40,32 @@ const BRAND = {
dark: { primary: "#7ba3b8", accent: "#e5a184" },
} as const
/**
* `--chart-2…8` from `index.css`, per theme, as literals: a chart canvas cannot
* read a custom property, and the slot a dashboard's own colours push them to
* is not the one the stylesheet put them in. Keep in step with `index.css`.
*/
const CHART = {
light: [
"#e26f00",
"#6f49ca",
"#00721b",
"#c66ae1",
"#966a00",
"#00a093",
"#b32474",
],
dark: [
"#d26600",
"#8d71e6",
"#55c15e",
"#dd8bf6",
"#ae7c00",
"#00c0b1",
"#bf4483",
],
} as const
/**
* The hex colours in a piece of text, in the order they appear.
*
@@ -150,7 +177,7 @@ export type Roles = {
primary: string
accent: string
foreground: string
/** Data colours, in draw order. At least five, so a full chart never repeats. */
/** Data colours, in draw order. At least eight, so a full chart never repeats. */
series: string[]
}
@@ -164,7 +191,8 @@ export function rolesOf(hexes: string[]): Roles | null {
const [background, surface, primary, accent, foreground, ...extra] = hexes
if (!background) return null
const brand = BRAND[isLight(background) ? "light" : "dark"]
const theme = isLight(background) ? "light" : "dark"
const brand = BRAND[theme]
const fore = foreground ?? onColor(background)
const roles: Roles = {
background,
@@ -178,17 +206,9 @@ export function rolesOf(hexes: string[]): Roles | null {
foreground: fore,
series: [],
}
roles.series = [roles.primary, roles.accent, ...extra]
// Enough colours for a full chart, taken from the two the palette named
// rather than from a ramp the dashboard never chose.
for (const tint of [
mix(roles.primary, fore, 0.35),
mix(roles.accent, fore, 0.35),
mix(roles.primary, roles.accent, 0.5),
]) {
if (roles.series.length >= MAX_ROLES) break
roles.series.push(tint)
}
// The palette's own primary leads, as `--primary` leads the shared one, and
// the rest are hues chosen to stand apart rather than shades of the scheme.
roles.series = [roles.primary, ...extra, ...CHART[theme]]
return roles
}
@@ -229,5 +249,8 @@ export function tokenStyle(roles: Roles): Record<string, string> {
"--chart-3": series[2],
"--chart-4": series[3],
"--chart-5": series[4],
"--chart-6": series[5],
"--chart-7": series[6],
"--chart-8": series[7],
}
}
@@ -70,7 +70,7 @@ export function useAnimatedFraction(fraction: number): MotionValue<number> {
/**
* A palette entry as CSS.
*
* A dashboard names colours, and a slot of the app's own ramp is what a
* A dashboard names colours, and a slot of the app's own palette is what a
* document written before it could says instead — so one is used as it stands
* and the other is looked up.
*/