@adea-ai/themes 0.11.7 → 0.11.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/accents.d.ts +19 -19
- package/dist/accents.d.ts.map +1 -1
- package/dist/accents.js +45 -93
- package/dist/accents.js.map +1 -1
- package/dist/adapters/base24.d.ts +21 -12
- package/dist/adapters/base24.d.ts.map +1 -1
- package/dist/adapters/base24.js +47 -72
- package/dist/adapters/base24.js.map +1 -1
- package/dist/adapters/css-variables.d.ts +29 -0
- package/dist/adapters/css-variables.d.ts.map +1 -0
- package/dist/adapters/css-variables.js +56 -0
- package/dist/adapters/css-variables.js.map +1 -0
- package/dist/adapters/css.d.ts +2 -4
- package/dist/adapters/css.d.ts.map +1 -1
- package/dist/adapters/css.js +7 -39
- package/dist/adapters/css.js.map +1 -1
- package/dist/adapters/shadcn.d.ts +8 -4
- package/dist/adapters/shadcn.d.ts.map +1 -1
- package/dist/adapters/shadcn.js +38 -57
- package/dist/adapters/shadcn.js.map +1 -1
- package/dist/adapters/shiki.d.ts +2 -10
- package/dist/adapters/shiki.d.ts.map +1 -1
- package/dist/adapters/shiki.js +16 -29
- package/dist/adapters/shiki.js.map +1 -1
- package/dist/adapters/tailwind.d.ts +3 -3
- package/dist/adapters/tailwind.d.ts.map +1 -1
- package/dist/adapters/tailwind.js +7 -28
- package/dist/adapters/tailwind.js.map +1 -1
- package/dist/adapters/xterm.d.ts.map +1 -1
- package/dist/adapters/xterm.js +9 -21
- package/dist/adapters/xterm.js.map +1 -1
- package/dist/base24-synthesis.d.ts +31 -0
- package/dist/base24-synthesis.d.ts.map +1 -0
- package/dist/base24-synthesis.js +25 -0
- package/dist/base24-synthesis.js.map +1 -0
- package/dist/derive.d.ts +1 -1
- package/dist/derive.d.ts.map +1 -1
- package/dist/derive.js +11 -26
- package/dist/derive.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/normalize.d.ts +0 -8
- package/dist/normalize.d.ts.map +1 -1
- package/dist/normalize.js +105 -232
- package/dist/normalize.js.map +1 -1
- package/dist/oklch.d.ts +1 -7
- package/dist/oklch.d.ts.map +1 -1
- package/dist/oklch.js +15 -35
- package/dist/oklch.js.map +1 -1
- package/dist/policy.d.ts +14 -4
- package/dist/policy.d.ts.map +1 -1
- package/dist/policy.js +16 -4
- package/dist/policy.js.map +1 -1
- package/dist/render.d.ts +30 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +74 -0
- package/dist/render.js.map +1 -0
- package/dist/sources.d.ts +26 -33
- package/dist/sources.d.ts.map +1 -1
- package/dist/sources.js +136 -305
- package/dist/sources.js.map +1 -1
- package/dist/step-search.d.ts +10 -0
- package/dist/step-search.d.ts.map +1 -0
- package/dist/step-search.js +21 -0
- package/dist/step-search.js.map +1 -0
- package/dist/theme-token-projection-script.d.ts +2 -0
- package/dist/theme-token-projection-script.js +2 -0
- package/dist/theme-token-projection.d.ts +35 -0
- package/dist/theme-token-projection.d.ts.map +1 -0
- package/dist/theme-token-projection.js +79 -0
- package/dist/theme-token-projection.js.map +1 -0
- package/dist/validate.d.ts +12 -17
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +17 -63
- package/dist/validate.js.map +1 -1
- package/package.json +13 -3
- package/src/accents.ts +57 -96
- package/src/adapters/base24.ts +53 -75
- package/src/adapters/css-variables.ts +67 -0
- package/src/adapters/css.ts +9 -54
- package/src/adapters/shadcn.ts +34 -60
- package/src/adapters/shiki.ts +17 -30
- package/src/adapters/tailwind.ts +7 -37
- package/src/adapters/xterm.ts +9 -21
- package/src/base24-synthesis.ts +29 -0
- package/src/derive.ts +12 -27
- package/src/index.ts +6 -5
- package/src/normalize.ts +140 -267
- package/src/oklch.ts +20 -35
- package/src/policy.ts +18 -4
- package/src/render.ts +82 -0
- package/src/sources.ts +160 -330
- package/src/step-search.ts +25 -0
- package/src/theme-token-projection.ts +126 -0
- package/src/validate.ts +31 -74
package/src/oklch.ts
CHANGED
|
@@ -23,6 +23,9 @@
|
|
|
23
23
|
* the colour recognisably the one that was asked for.
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
+
import { REPAIR_STEP } from './policy.js'
|
|
27
|
+
import { offsetSize, smallestOffset } from './step-search.js'
|
|
28
|
+
|
|
26
29
|
/** A colour in OKLCH. `l` and `c` are unbounded here; use {@link inGamut} to test. */
|
|
27
30
|
export type Oklch = {
|
|
28
31
|
/** Perceptual lightness, nominally 0–1. */
|
|
@@ -83,8 +86,6 @@ export const oklchToOklab = ({ l, c, h }: Oklch): Oklab => {
|
|
|
83
86
|
|
|
84
87
|
export const oklabToOklch = ({ l, a, b }: Oklab): Oklch => {
|
|
85
88
|
const c = Math.hypot(a, b)
|
|
86
|
-
// Hue is undefined for a neutral; report 0 rather than NaN so that a grey
|
|
87
|
-
// survives a round trip and can still be interpolated.
|
|
88
89
|
const h = c < 1e-6 ? 0 : ((Math.atan2(b, a) * 180) / Math.PI + 360) % 360
|
|
89
90
|
return { l, c, h }
|
|
90
91
|
}
|
|
@@ -98,12 +99,6 @@ export function inGamut(color: Oklch, tolerance = 1e-4): boolean {
|
|
|
98
99
|
|
|
99
100
|
/**
|
|
100
101
|
* Brings a colour into sRGB by reducing chroma at constant lightness and hue.
|
|
101
|
-
*
|
|
102
|
-
* Reduction is a bisection because the sRGB boundary along a constant-hue line is
|
|
103
|
-
* not a linear function of chroma — a fixed number of decrements either stops
|
|
104
|
-
* short of the boundary (leaving a clipped colour) or overshoots it (desaturating
|
|
105
|
-
* far more than necessary). 24 iterations resolve chroma to well under one
|
|
106
|
-
* 8-bit-steps' worth of difference.
|
|
107
102
|
*/
|
|
108
103
|
export function gamutMap(color: Oklch): Oklch {
|
|
109
104
|
const lightness = Math.min(1, Math.max(0, color.l))
|
|
@@ -290,11 +285,6 @@ export function repairContrast(
|
|
|
290
285
|
minimum: number,
|
|
291
286
|
budget = 0.22
|
|
292
287
|
): ContrastRepair {
|
|
293
|
-
// Measured in the canonical form, for the reason {@link canonical} gives: the caller
|
|
294
|
-
// commits `formatOklch`'s rounded output, and a repair that converges on the exact
|
|
295
|
-
// floor of an unrounded value ships a value that fails it. The lower a floor is, the
|
|
296
|
-
// more this matters, and the scale starts at 1.2:1 — so a selection colour's whole
|
|
297
|
-
// range is a few rounding steps wide.
|
|
298
288
|
const initial = contrastRatio(canonical(foreground), background)
|
|
299
289
|
if (initial >= minimum) {
|
|
300
290
|
return { color: foreground, ratio: initial, delta: 0, satisfied: true }
|
|
@@ -306,30 +296,25 @@ export function repairContrast(
|
|
|
306
296
|
// mid grey on a near-white canvas, and *lightening* it has 0.03 to work with while
|
|
307
297
|
// darkening it has the whole range.
|
|
308
298
|
const preferred = contrastDirection(foreground, background)
|
|
309
|
-
const
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
if (ratio >= minimum) {
|
|
318
|
-
return {
|
|
319
|
-
color: canonical(candidate),
|
|
320
|
-
ratio,
|
|
321
|
-
delta: Number((step * index).toFixed(4)),
|
|
322
|
-
satisfied: true,
|
|
323
|
-
}
|
|
324
|
-
}
|
|
299
|
+
const shifted = (offset: number): Oklch => canonical({ ...foreground, l: foreground.l + offset })
|
|
300
|
+
const offset = smallestOffset(
|
|
301
|
+
REPAIR_STEP,
|
|
302
|
+
Math.floor(budget / REPAIR_STEP),
|
|
303
|
+
[preferred, -preferred],
|
|
304
|
+
(trial) => {
|
|
305
|
+
const l = foreground.l + trial
|
|
306
|
+
return l > 0 && l < 1 && contrastRatio(shifted(trial), background) >= minimum
|
|
325
307
|
}
|
|
308
|
+
)
|
|
309
|
+
if (offset === undefined) {
|
|
310
|
+
return { color: foreground, ratio: initial, delta: 0, satisfied: false }
|
|
326
311
|
}
|
|
327
|
-
|
|
312
|
+
const color = shifted(offset)
|
|
328
313
|
return {
|
|
329
|
-
color
|
|
330
|
-
ratio:
|
|
331
|
-
delta:
|
|
332
|
-
satisfied:
|
|
314
|
+
color,
|
|
315
|
+
ratio: contrastRatio(color, background),
|
|
316
|
+
delta: offsetSize(offset),
|
|
317
|
+
satisfied: true,
|
|
333
318
|
}
|
|
334
319
|
}
|
|
335
320
|
|
|
@@ -374,7 +359,7 @@ export function shiftLightness(color: Oklch, delta: number): Oklch {
|
|
|
374
359
|
return { l, c: color.c * headroom, h: color.h }
|
|
375
360
|
}
|
|
376
361
|
|
|
377
|
-
/** Scales chroma, clamped to
|
|
362
|
+
/** Scales chroma, clamped to 0–0.4. */
|
|
378
363
|
export function scaleChroma(color: Oklch, factor: number): Oklch {
|
|
379
364
|
return { ...color, c: Math.max(0, Math.min(0.4, color.c * factor)) }
|
|
380
365
|
}
|
package/src/policy.ts
CHANGED
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
* The catalogue's policy: the contrast floors, the repair budgets and the accent
|
|
3
3
|
* preference.
|
|
4
4
|
*
|
|
5
|
-
* Kept apart from the normalizer, which applies them, so that the accent axis
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* `normalize.ts` re-exports everything here.
|
|
5
|
+
* Kept apart from the normalizer, which applies them, so that the accent axis,
|
|
6
|
+
* the validator and the source list read the same numbers without importing the
|
|
7
|
+
* normalizer.
|
|
9
8
|
*/
|
|
10
9
|
|
|
11
10
|
import type { AnsiKey } from './schema.js'
|
|
@@ -70,10 +69,25 @@ export const CONTRAST_FLOORS = Object.freeze({
|
|
|
70
69
|
export const REPAIR_BUDGET = Object.freeze({
|
|
71
70
|
/** Body text: some palettes publish a foreground that fails on their own canvas. */
|
|
72
71
|
text: 0.2,
|
|
72
|
+
textMuted: 0.3,
|
|
73
|
+
textSubtle: 0.3,
|
|
73
74
|
status: 0.24,
|
|
74
75
|
accent: 0.24,
|
|
76
|
+
accentForeground: 0.6,
|
|
77
|
+
selection: 0.5,
|
|
75
78
|
})
|
|
76
79
|
|
|
80
|
+
/** The lightness increment every contrast repair searches in. */
|
|
81
|
+
export const REPAIR_STEP = 0.002
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Whether a measured ratio clears a floor, allowing for floating-point noise in a
|
|
85
|
+
* ratio computed to land exactly on it.
|
|
86
|
+
*/
|
|
87
|
+
export function clearsFloor(ratio: number, floor: number): boolean {
|
|
88
|
+
return ratio >= floor - 1e-9
|
|
89
|
+
}
|
|
90
|
+
|
|
77
91
|
/** The accent preference order, best first. */
|
|
78
92
|
export const ACCENT_PREFERENCE = [
|
|
79
93
|
'blue',
|
package/src/render.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colours as a screen draws them, and the choices made by measuring them.
|
|
3
|
+
*
|
|
4
|
+
* A browser stores three eight-bit sRGB channels and composites translucent fills in
|
|
5
|
+
* gamma-encoded sRGB, and an accessibility audit samples that result. The helpers
|
|
6
|
+
* here reproduce it, so a floor measured in this package is a floor on what is drawn.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Oklch } from './oklch.js'
|
|
10
|
+
import { contrastRatio, hexToOklch, oklchToHex, parseColor, repairContrast } from './oklch.js'
|
|
11
|
+
import { clearsFloor } from './policy.js'
|
|
12
|
+
|
|
13
|
+
/** A colour string as hex, or the input unchanged when it does not parse. */
|
|
14
|
+
export function toHex(value: string): string {
|
|
15
|
+
const parsed = parseColor(value)
|
|
16
|
+
return parsed ? oklchToHex(parsed) : value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A colour string as the screen draws it: through eight-bit sRGB. */
|
|
20
|
+
export function renderedColor(value: string): Oklch | undefined {
|
|
21
|
+
const parsed = parseColor(value)
|
|
22
|
+
return parsed ? hexToOklch(oklchToHex(parsed)) : undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A translucent fill painted over an opaque one: `alpha` (0–1) of `front` over
|
|
27
|
+
* `back`, composited per eight-bit sRGB channel the way a browser paints
|
|
28
|
+
* `color-mix(in oklch, <front> N%, transparent)` and `--colour/90`.
|
|
29
|
+
*/
|
|
30
|
+
export function compositeOver(front: string, back: string, alpha: number): Oklch | undefined {
|
|
31
|
+
const frontColor = parseColor(front)
|
|
32
|
+
const backColor = parseColor(back)
|
|
33
|
+
if (!frontColor || !backColor) return undefined
|
|
34
|
+
const frontHex = oklchToHex(frontColor)
|
|
35
|
+
const backHex = oklchToHex(backColor)
|
|
36
|
+
const channels = [1, 3, 5].map((offset) => {
|
|
37
|
+
const top = Number.parseInt(frontHex.slice(offset, offset + 2), 16)
|
|
38
|
+
const bottom = Number.parseInt(backHex.slice(offset, offset + 2), 16)
|
|
39
|
+
return Math.round(top * alpha + bottom * (1 - alpha))
|
|
40
|
+
.toString(16)
|
|
41
|
+
.padStart(2, '0')
|
|
42
|
+
})
|
|
43
|
+
return hexToOklch(`#${channels.join('')}`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The candidate with the highest contrast against `target`; the earliest wins a tie. */
|
|
47
|
+
export function mostContrasting<T extends Oklch>(target: Oklch, candidates: readonly T[]): T {
|
|
48
|
+
let best = candidates[0] as T
|
|
49
|
+
let bestRatio = contrastRatio(best, target)
|
|
50
|
+
for (const candidate of candidates.slice(1)) {
|
|
51
|
+
const ratio = contrastRatio(candidate, target)
|
|
52
|
+
if (ratio > bestRatio) {
|
|
53
|
+
best = candidate
|
|
54
|
+
bestRatio = ratio
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return best
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The smallest lightness repair of `foreground` whose hex encoding clears `floor`
|
|
62
|
+
* against `background`, as hex, or `undefined` when `budget` cannot reach it.
|
|
63
|
+
*
|
|
64
|
+
* A value repaired to exactly the floor can round to an eight-bit colour just below
|
|
65
|
+
* it, so the requested floor is raised a thousandth at a time until the encoded value
|
|
66
|
+
* itself clears.
|
|
67
|
+
*/
|
|
68
|
+
export function repairToHex(
|
|
69
|
+
foreground: Oklch,
|
|
70
|
+
background: Oklch,
|
|
71
|
+
floor: number,
|
|
72
|
+
budget: number
|
|
73
|
+
): string | undefined {
|
|
74
|
+
for (let margin = 0; margin <= 0.05; margin += 0.001) {
|
|
75
|
+
const repaired = repairContrast(foreground, background, floor + margin, budget)
|
|
76
|
+
if (!repaired.satisfied) continue
|
|
77
|
+
const hex = oklchToHex(repaired.color)
|
|
78
|
+
const drawn = parseColor(hex)
|
|
79
|
+
if (drawn && clearsFloor(contrastRatio(drawn, background), floor)) return hex
|
|
80
|
+
}
|
|
81
|
+
return undefined
|
|
82
|
+
}
|