@adea-ai/ui 0.123.1 → 0.125.0
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/dist/NOTICE +36 -0
- package/dist/components/theme/appearance-panel.js +1 -1
- package/dist/components/theme/index.d.ts +1 -1
- package/dist/components/theme/index.d.ts.map +1 -1
- package/dist/components/theme/index.js +5 -2
- package/dist/components/theme/theme-context.d.ts +13 -0
- package/dist/components/theme/theme-context.d.ts.map +1 -0
- package/dist/components/theme/theme-context.js +41 -0
- package/dist/components/theme/theme-context.js.map +1 -0
- package/dist/components/theme/theme-document.d.ts +82 -0
- package/dist/components/theme/theme-document.d.ts.map +1 -0
- package/dist/components/theme/theme-document.js +98 -0
- package/dist/components/theme/theme-document.js.map +1 -0
- package/dist/components/theme/theme-persistence.d.ts +50 -0
- package/dist/components/theme/theme-persistence.d.ts.map +1 -0
- package/dist/components/theme/theme-persistence.js +87 -0
- package/dist/components/theme/theme-persistence.js.map +1 -0
- package/dist/components/theme/theme-provider.d.ts +52 -75
- package/dist/components/theme/theme-provider.d.ts.map +1 -1
- package/dist/components/theme/theme-provider.js +60 -116
- package/dist/components/theme/theme-provider.js.map +1 -1
- package/dist/components/theme/theme-state.d.ts +167 -0
- package/dist/components/theme/theme-state.d.ts.map +1 -0
- package/dist/components/theme/theme-state.js +122 -0
- package/dist/components/theme/theme-state.js.map +1 -0
- package/dist/components/theme/theme-toggle.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/lib/accent-roles.d.ts +124 -0
- package/dist/lib/accent-roles.d.ts.map +1 -0
- package/dist/lib/accent-roles.js +163 -0
- package/dist/lib/accent-roles.js.map +1 -0
- package/dist/lib/appearance-font-settings.d.ts +1 -1
- package/dist/lib/appearance-font-settings.d.ts.map +1 -1
- package/dist/lib/appearance-font-settings.js.map +1 -1
- package/dist/lib/themes.d.ts +71 -1
- package/dist/lib/themes.d.ts.map +1 -1
- package/dist/lib/themes.js +172 -48
- package/dist/lib/themes.js.map +1 -1
- package/dist/r/lib.json +5 -0
- package/dist/r/registry.json +25 -0
- package/dist/r/src/components/theme/index.ts +3 -0
- package/dist/r/src/components/theme/theme-context.ts +43 -0
- package/dist/r/src/components/theme/theme-document.ts +184 -0
- package/dist/r/src/components/theme/theme-persistence.ts +143 -0
- package/dist/r/src/components/theme/theme-provider.tsx +147 -253
- package/dist/r/src/components/theme/theme-state.ts +274 -0
- package/dist/r/src/lib/accent-roles.ts +338 -0
- package/dist/r/src/lib/appearance-font-settings.ts +1 -1
- package/dist/r/src/lib/themes.ts +225 -59
- package/dist/r/theming.json +20 -0
- package/package.json +1 -1
- package/registry.json +25 -0
- package/src/components/theme/index.ts +3 -0
- package/src/components/theme/theme-context.ts +43 -0
- package/src/components/theme/theme-document.ts +184 -0
- package/src/components/theme/theme-persistence.ts +143 -0
- package/src/components/theme/theme-provider.tsx +147 -253
- package/src/components/theme/theme-state.ts +274 -0
- package/src/index.ts +11 -0
- package/src/lib/accent-roles.ts +338 -0
- package/src/lib/appearance-font-settings.ts +1 -1
- package/src/lib/themes.ts +225 -59
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_APPEARANCE_EDITOR_FONT_SETTINGS,
|
|
3
|
+
normalizeAppearanceEditorFontSettings,
|
|
4
|
+
type AppearanceEditorFontSettings,
|
|
5
|
+
} from '../../lib/appearance-font-settings'
|
|
6
|
+
import type { AccentPreset } from '@adea-ai/themes'
|
|
7
|
+
import { normalizeCustomAccent, type AccentRoles } from '../../lib/accent-roles'
|
|
8
|
+
import {
|
|
9
|
+
accentRolesForSelection,
|
|
10
|
+
defaultDarkThemeId,
|
|
11
|
+
defaultLightThemeId,
|
|
12
|
+
resolveVariant,
|
|
13
|
+
type ThemeAppearance,
|
|
14
|
+
type ThemeVariant,
|
|
15
|
+
} from '../../lib/themes'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The pure half of the theme provider: what a selection is, how two selections
|
|
19
|
+
* merge, and what a selection resolves to.
|
|
20
|
+
*
|
|
21
|
+
* Nothing here touches the document, storage or a signal, so each rule can be
|
|
22
|
+
* tested by calling it. `ThemeProvider` owns the reactivity and wires these to it;
|
|
23
|
+
* `theme-document.ts` owns the writes; `theme-persistence.ts` owns the store.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Four independent choices, and keeping them independent is the point:
|
|
28
|
+
*
|
|
29
|
+
* - **appearance** — light, dark, or follow the system.
|
|
30
|
+
* - **a theme per appearance** — so a user can run Catppuccin Mocha by night and
|
|
31
|
+
* Solarized Light by day without re-choosing when the system flips.
|
|
32
|
+
* - **an accent** — adea's six presets, or one of the accents the selected theme
|
|
33
|
+
* pair carries itself (`ansi-blue`…), which re-colour the interactive roles on
|
|
34
|
+
* top of whichever theme is active.
|
|
35
|
+
* - **fonts** — separate family and size choices for UI, content and code.
|
|
36
|
+
*/
|
|
37
|
+
export type ThemeSelection = {
|
|
38
|
+
/** `system` follows `prefers-color-scheme` and updates when it changes. */
|
|
39
|
+
appearance: ThemeAppearance | 'system'
|
|
40
|
+
/** The variant to use when the resolved appearance is light. */
|
|
41
|
+
lightThemeId: string
|
|
42
|
+
/** The variant to use when the resolved appearance is dark. */
|
|
43
|
+
darkThemeId: string
|
|
44
|
+
/**
|
|
45
|
+
* An `accentPresets` id, a theme accent id from `themeAccents()` (`ansi-blue`…),
|
|
46
|
+
* `theme` for the variant's own primary, or a custom colour as `#rrggbb`. A theme
|
|
47
|
+
* accent the current pair does not offer falls back to the variant's own primary.
|
|
48
|
+
*
|
|
49
|
+
* A custom colour is stored lower case and is raised to the contrast floor when it
|
|
50
|
+
* is painted, never when it is stored: the selection keeps what the user chose.
|
|
51
|
+
* Anything else that is not an id is dropped at paint, not rejected here.
|
|
52
|
+
*/
|
|
53
|
+
accent: string
|
|
54
|
+
/** UI-family alias for consumers of the original single-font preference. */
|
|
55
|
+
font: string
|
|
56
|
+
/** UI, content and code font settings. Omitted legacy records are migrated from `font`. */
|
|
57
|
+
fonts?: AppearanceEditorFontSettings
|
|
58
|
+
/**
|
|
59
|
+
* `comfortable` is the system's default geometry; `compact` moves the control
|
|
60
|
+
* ladder down one rung.
|
|
61
|
+
*
|
|
62
|
+
* Optional, and defaulted on read, so a preference stored before density existed
|
|
63
|
+
* keeps working and `ThemeSelection` stays assignable without it.
|
|
64
|
+
*/
|
|
65
|
+
density?: ThemeDensity
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The two density rungs.
|
|
70
|
+
*
|
|
71
|
+
* `comfortable` is the geometry in `theme.css`, so it is the absence of an
|
|
72
|
+
* attribute rather than a value: a document that ships no `data-density` at all
|
|
73
|
+
* renders correctly, which is what keeps this additive.
|
|
74
|
+
*/
|
|
75
|
+
export type ThemeDensity = 'comfortable' | 'compact'
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Deliberately lists only the fields every consumer has always had. A field added
|
|
79
|
+
* to {@link ThemeSelection} later gets no default here: `JSON.stringify` drops an
|
|
80
|
+
* `undefined`, so a store written by a consumer that never sets the field stays
|
|
81
|
+
* byte-identical to what it was before the field existed.
|
|
82
|
+
*/
|
|
83
|
+
export const defaultThemeSelection: ThemeSelection = {
|
|
84
|
+
appearance: 'system',
|
|
85
|
+
lightThemeId: defaultLightThemeId,
|
|
86
|
+
darkThemeId: defaultDarkThemeId,
|
|
87
|
+
accent: 'theme',
|
|
88
|
+
font: 'system',
|
|
89
|
+
fonts: DEFAULT_APPEARANCE_EDITOR_FONT_SETTINGS,
|
|
90
|
+
density: 'comfortable',
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A stored selection's density, defaulted.
|
|
95
|
+
*
|
|
96
|
+
* The stored value is read through here rather than trusted, because a preference
|
|
97
|
+
* written before density existed has no `density` key and a hand-edited store may
|
|
98
|
+
* hold anything.
|
|
99
|
+
*/
|
|
100
|
+
export function resolvedDensity(selection: ThemeSelection): ThemeDensity {
|
|
101
|
+
return selection.density === 'compact' ? 'compact' : 'comfortable'
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export type ThemeContextValue = {
|
|
105
|
+
/** The preference as stored. A {@link ThemeContextValue.preview} does not change it. */
|
|
106
|
+
selection: () => ThemeSelection
|
|
107
|
+
/** What the preference resolves to right now, after `system`. */
|
|
108
|
+
resolvedAppearance: () => ThemeAppearance
|
|
109
|
+
/** The variant in use. */
|
|
110
|
+
variant: () => ThemeVariant
|
|
111
|
+
/** Update part of the preference. */
|
|
112
|
+
setSelection: (patch: Partial<ThemeSelection>) => void
|
|
113
|
+
/**
|
|
114
|
+
* The built-in catalogue, so a picker does not import it separately. A plain
|
|
115
|
+
* array: {@link ThemeContextValue.allThemes} is the one that follows a host's
|
|
116
|
+
* extra themes.
|
|
117
|
+
*/
|
|
118
|
+
themes: readonly ThemeVariant[]
|
|
119
|
+
/**
|
|
120
|
+
* The accents the selected light/dark pair carries, offered beside the presets.
|
|
121
|
+
* Follows the selection: a different theme pair may offer different slots.
|
|
122
|
+
*/
|
|
123
|
+
themeAccents: () => readonly AccentPreset[]
|
|
124
|
+
/**
|
|
125
|
+
* The built-in catalogue followed by the provider's `themes` prop. Reactive when
|
|
126
|
+
* that prop is a function that reads signals.
|
|
127
|
+
*/
|
|
128
|
+
allThemes: () => readonly ThemeVariant[]
|
|
129
|
+
/**
|
|
130
|
+
* Paint the document from `draft` laid over the stored selection, without
|
|
131
|
+
* persisting it or changing what {@link ThemeContextValue.selection} returns.
|
|
132
|
+
* `undefined` ends the preview. A {@link ThemeContextValue.setSelection} ends it
|
|
133
|
+
* too, because the committed selection is then what should be showing.
|
|
134
|
+
*/
|
|
135
|
+
preview: (draft: Partial<ThemeSelection> | undefined) => void
|
|
136
|
+
/** True while a {@link ThemeContextValue.preview} draft is on the document. */
|
|
137
|
+
previewing: () => boolean
|
|
138
|
+
/**
|
|
139
|
+
* The roles `accent` paints over `variantId` (default: the variant in use), held
|
|
140
|
+
* to the provider's `accentContrastFloor`, for an element-scoped override with
|
|
141
|
+
* `paintAccentRoles` — a workspace that carries its own colour. `undefined` for
|
|
142
|
+
* `null`, for `theme`, for an id that resolves to nothing, and for a variant the
|
|
143
|
+
* registry does not hold: each means "keep the inherited accent".
|
|
144
|
+
*/
|
|
145
|
+
accentRolesFor: (accent: string | null, variantId?: string) => AccentRoles | undefined
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A selection with the `undefined` keys of a patch left out. */
|
|
149
|
+
function definedOnly<T extends object>(value: T): Partial<T> {
|
|
150
|
+
return Object.fromEntries(
|
|
151
|
+
Object.entries(value).filter(([, entry]) => entry !== undefined)
|
|
152
|
+
) as Partial<T>
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Keep the deprecated single-font view synchronized with the UI axis. */
|
|
156
|
+
export function mergeThemeSelection(
|
|
157
|
+
base: ThemeSelection,
|
|
158
|
+
patch: Partial<ThemeSelection>
|
|
159
|
+
): ThemeSelection {
|
|
160
|
+
const merged = { ...base, ...patch }
|
|
161
|
+
let fonts: AppearanceEditorFontSettings
|
|
162
|
+
|
|
163
|
+
if (patch.fonts !== undefined) {
|
|
164
|
+
fonts = normalizeAppearanceEditorFontSettings(patch.fonts).settings
|
|
165
|
+
} else if (patch.font !== undefined) {
|
|
166
|
+
const currentFonts = normalizeAppearanceEditorFontSettings(base.fonts, base.font).settings
|
|
167
|
+
fonts = normalizeAppearanceEditorFontSettings({
|
|
168
|
+
...currentFonts,
|
|
169
|
+
ui: { ...currentFonts.ui, family: patch.font },
|
|
170
|
+
}).settings
|
|
171
|
+
} else {
|
|
172
|
+
fonts = normalizeAppearanceEditorFontSettings(merged.fonts, merged.font).settings
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
...merged,
|
|
177
|
+
// One spelling for a custom colour, so equal choices compare and store equal.
|
|
178
|
+
accent: normalizeCustomAccent(merged.accent) ?? merged.accent,
|
|
179
|
+
fonts,
|
|
180
|
+
font: fonts.ui.family,
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The selection a provider starts from: the defaults, then whatever the store
|
|
186
|
+
* returned, then `initial`.
|
|
187
|
+
*
|
|
188
|
+
* `initial` wins over the store on every mount. That precedence is part of the
|
|
189
|
+
* contract Cortana relies on (its `initial` pins `appearance` and `font`), and it
|
|
190
|
+
* is why the store's answer is only the middle layer. A store that returned
|
|
191
|
+
* nothing, or threw, or answered with explicit `undefined`s, leaves the defaults
|
|
192
|
+
* standing for those fields.
|
|
193
|
+
*/
|
|
194
|
+
export function initialThemeSelection(
|
|
195
|
+
stored: Partial<ThemeSelection> | undefined,
|
|
196
|
+
initial: Partial<ThemeSelection> | undefined
|
|
197
|
+
): ThemeSelection {
|
|
198
|
+
const base = mergeThemeSelection(defaultThemeSelection, definedOnly(stored ?? {}))
|
|
199
|
+
return mergeThemeSelection(base, initial ?? {})
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** `system` resolved against what the operating system reports. */
|
|
203
|
+
export function resolveAppearance(
|
|
204
|
+
preference: ThemeSelection['appearance'],
|
|
205
|
+
system: ThemeAppearance
|
|
206
|
+
): ThemeAppearance {
|
|
207
|
+
return preference === 'system' ? system : preference
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The theme id the selection names for a resolved appearance. */
|
|
211
|
+
export function selectedThemeId(selection: ThemeSelection, appearance: ThemeAppearance): string {
|
|
212
|
+
return appearance === 'dark' ? selection.darkThemeId : selection.lightThemeId
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* What a selection paints: the resolved appearance and the variant for it.
|
|
217
|
+
*
|
|
218
|
+
* An id the registry does not hold resolves to the appearance's default rather than
|
|
219
|
+
* being rewritten (see `resolveVariant`), so the selection a host stores is never
|
|
220
|
+
* changed by being unresolvable.
|
|
221
|
+
*/
|
|
222
|
+
export function resolveThemeState(
|
|
223
|
+
selection: ThemeSelection,
|
|
224
|
+
system: ThemeAppearance,
|
|
225
|
+
registry: readonly ThemeVariant[]
|
|
226
|
+
): { appearance: ThemeAppearance; variant: ThemeVariant } {
|
|
227
|
+
const appearance = resolveAppearance(selection.appearance, system)
|
|
228
|
+
return {
|
|
229
|
+
appearance,
|
|
230
|
+
variant: resolveVariant(registry, selectedThemeId(selection, appearance), appearance),
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The registry a provider resolves against: the catalogue, then the host's extras.
|
|
236
|
+
*
|
|
237
|
+
* The catalogue wins an id collision, because it is listed first and `resolveVariant`
|
|
238
|
+
* takes the first match: a host's variant can add a theme but cannot shadow one.
|
|
239
|
+
*/
|
|
240
|
+
export function mergeRegistry(
|
|
241
|
+
builtin: readonly ThemeVariant[],
|
|
242
|
+
extra: readonly ThemeVariant[] | undefined
|
|
243
|
+
): readonly ThemeVariant[] {
|
|
244
|
+
return extra && extra.length > 0 ? [...builtin, ...extra] : builtin
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The roles an accent paints over a variant, for {@link ThemeContextValue.accentRolesFor}.
|
|
249
|
+
*
|
|
250
|
+
* Resolved against the committed selection's theme pair, so an `ansi-*` accent means
|
|
251
|
+
* the same colour here as it does on `<html>`.
|
|
252
|
+
*/
|
|
253
|
+
export function accentRolesForState(
|
|
254
|
+
accent: string | null,
|
|
255
|
+
variantId: string | undefined,
|
|
256
|
+
state: {
|
|
257
|
+
selection: ThemeSelection
|
|
258
|
+
variant: ThemeVariant
|
|
259
|
+
registry: readonly ThemeVariant[]
|
|
260
|
+
accentContrastFloor?: number
|
|
261
|
+
}
|
|
262
|
+
): AccentRoles | undefined {
|
|
263
|
+
if (accent === null) return undefined
|
|
264
|
+
const variant =
|
|
265
|
+
variantId === undefined
|
|
266
|
+
? state.variant
|
|
267
|
+
: state.registry.find((candidate) => candidate.id === variantId)
|
|
268
|
+
if (!variant) return undefined
|
|
269
|
+
return accentRolesForSelection(accent, variant.appearance, state.selection, {
|
|
270
|
+
background: variant.colors.background,
|
|
271
|
+
floor: state.accentContrastFloor,
|
|
272
|
+
variantId: variant.id,
|
|
273
|
+
})
|
|
274
|
+
}
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Wing
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*
|
|
5
|
+
* The colour helpers, the contrast gate and the custom-accent grammar are
|
|
6
|
+
* substantially translated from Zeron's `Color` and `AccentRoles`
|
|
7
|
+
* (https://github.com/zeronsh/zeron), as Adea's accepted port of them in
|
|
8
|
+
* `packages/ui/src/components/appearance.ts` expresses it. Extracted here so the
|
|
9
|
+
* shared provider and Adea run one implementation; `@adea-ai/themes` owns the
|
|
10
|
+
* colour engine (parsing, contrast, repair, the preset values and their roles).
|
|
11
|
+
* See NOTICE.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
ACCENTS,
|
|
16
|
+
accentRoles,
|
|
17
|
+
accentValue,
|
|
18
|
+
primarySubtleCss,
|
|
19
|
+
type AccentPreset,
|
|
20
|
+
type AccentRoles as CatalogueAccentRoles,
|
|
21
|
+
} from '@adea-ai/themes'
|
|
22
|
+
import {
|
|
23
|
+
contrastRatio as canonicalContrastRatio,
|
|
24
|
+
oklchToHex,
|
|
25
|
+
parseColor as parseCanonicalColor,
|
|
26
|
+
repairContrast,
|
|
27
|
+
type Oklch,
|
|
28
|
+
} from '@adea-ai/themes/oklch'
|
|
29
|
+
import type { ThemeAppearance } from './themes'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Accent colour maths and the roles an accent paints.
|
|
33
|
+
*
|
|
34
|
+
* Three questions live here, and none of them touches the document or a signal:
|
|
35
|
+
*
|
|
36
|
+
* - **What colour is this?** `parseColor`, `colorToHex`, `contrastRatio` and
|
|
37
|
+
* `ensureContrast` are the hex helpers a host needs around an accent (an
|
|
38
|
+
* imported theme's colours, a colour field). Hex is the host preference format;
|
|
39
|
+
* the catalogue's colour engine does the maths.
|
|
40
|
+
* - **Is this accent readable on this surface?** The *contrast floor*. A preset is
|
|
41
|
+
* curated, so the catalogue already measured it against its own themes and the
|
|
42
|
+
* floor is the identity for it; a custom `#rrggbb` is anyone's choice, so it is
|
|
43
|
+
* raised until it clears the floor and can never ship unreadable. A host that
|
|
44
|
+
* imports themes (whose backgrounds the catalogue never saw) turns the floor on
|
|
45
|
+
* for presets too.
|
|
46
|
+
* - **Which custom properties does an accent own?** `AccentRoles`, written on
|
|
47
|
+
* `<html>` by the provider and onto one element by {@link paintAccentRoles}.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/* Hex is the host preference format; the shared Themes package owns colour maths. */
|
|
51
|
+
|
|
52
|
+
export type RgbColor = Readonly<{ r: number; g: number; b: number; a: number }>
|
|
53
|
+
|
|
54
|
+
const HEX = /^#([0-9a-fA-F]+)$/
|
|
55
|
+
|
|
56
|
+
/** Parse a hex colour (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`). `undefined` for anything else. */
|
|
57
|
+
export function parseColor(value: string): RgbColor | undefined {
|
|
58
|
+
const match = HEX.exec(value.trim())
|
|
59
|
+
const digits = match?.[1]
|
|
60
|
+
if (!digits) return undefined
|
|
61
|
+
if (digits.length === 3 || digits.length === 4) {
|
|
62
|
+
// Each nibble is doubled: `#abc` is `#aabbcc`.
|
|
63
|
+
const [r, g, b, a] = [...digits].map((digit) => Number.parseInt(digit + digit, 16))
|
|
64
|
+
return { r: r!, g: g!, b: b!, a: a ?? 255 }
|
|
65
|
+
}
|
|
66
|
+
if (digits.length === 6 || digits.length === 8) {
|
|
67
|
+
const byte = (index: number) => Number.parseInt(digits.slice(index, index + 2), 16)
|
|
68
|
+
return { r: byte(0), g: byte(2), b: byte(4), a: digits.length === 8 ? byte(6) : 255 }
|
|
69
|
+
}
|
|
70
|
+
return undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function toHexByte(value: number): string {
|
|
74
|
+
return value.toString(16).padStart(2, '0')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** `#rrggbb`, or `#rrggbbaa` when the colour is not opaque. */
|
|
78
|
+
export function colorToHex(color: RgbColor): string {
|
|
79
|
+
const opaque = `#${toHexByte(color.r)}${toHexByte(color.g)}${toHexByte(color.b)}`
|
|
80
|
+
return color.a === 255 ? opaque : `${opaque}${toHexByte(color.a)}`
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function blendOver(foreground: RgbColor, background: RgbColor): RgbColor {
|
|
84
|
+
const alpha = foreground.a / 255
|
|
85
|
+
const blend = (front: number, back: number) => Math.round(front * alpha + back * (1 - alpha))
|
|
86
|
+
return {
|
|
87
|
+
r: blend(foreground.r, background.r),
|
|
88
|
+
g: blend(foreground.g, background.g),
|
|
89
|
+
b: blend(foreground.b, background.b),
|
|
90
|
+
a: 255,
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function toCanonicalColor(color: RgbColor): Oklch | undefined {
|
|
95
|
+
return parseCanonicalColor(colorToHex({ ...color, a: 255 }))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** WCAG contrast ratio between two colours. Alpha composites over the background first. */
|
|
99
|
+
export function contrastRatio(
|
|
100
|
+
foreground: RgbColor | string,
|
|
101
|
+
background: RgbColor | string
|
|
102
|
+
): number {
|
|
103
|
+
const front = typeof foreground === 'string' ? parseColor(foreground) : foreground
|
|
104
|
+
const back = typeof background === 'string' ? parseColor(background) : background
|
|
105
|
+
if (!front || !back) return 0
|
|
106
|
+
const opaqueFront = front.a === 255 ? front : blendOver(front, back)
|
|
107
|
+
const foregroundColor = toCanonicalColor(opaqueFront)
|
|
108
|
+
const backgroundColor = toCanonicalColor(back)
|
|
109
|
+
if (!foregroundColor || !backgroundColor) return 0
|
|
110
|
+
return canonicalContrastRatio(foregroundColor, backgroundColor)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const WHITE: RgbColor = { r: 255, g: 255, b: 255, a: 255 }
|
|
114
|
+
const WHITE_OKLCH: Oklch = { l: 1, c: 0, h: 0 }
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The nearest colour to `source` that reaches `minimum` against `canvas`, as hex.
|
|
118
|
+
*
|
|
119
|
+
* The catalogue's `repairContrast` is asked for a little more than the floor, in
|
|
120
|
+
* 0.001 steps up to 0.05, because the result is rounded to 8-bit hex and the
|
|
121
|
+
* rounded colour is what is measured: asking for exactly the floor can land a hair
|
|
122
|
+
* under it after rounding. If nothing in that range survives the rounding, white or
|
|
123
|
+
* black stands in, whichever clears the floor.
|
|
124
|
+
*/
|
|
125
|
+
function repairedHex(source: Oklch, canvas: Oklch, minimum: number): string {
|
|
126
|
+
for (let margin = 0; margin <= 0.05; margin = Number((margin + 0.001).toFixed(3))) {
|
|
127
|
+
const repaired = repairContrast(source, canvas, minimum + margin)
|
|
128
|
+
if (!repaired.satisfied) continue
|
|
129
|
+
const hex = oklchToHex(repaired.color)
|
|
130
|
+
const rounded = parseCanonicalColor(hex)
|
|
131
|
+
if (rounded && canonicalContrastRatio(rounded, canvas) >= minimum) return hex
|
|
132
|
+
}
|
|
133
|
+
return canonicalContrastRatio(WHITE_OKLCH, canvas) >= minimum ? '#ffffff' : '#000000'
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Preserve the host hex shape while delegating contrast repair to the catalogue. */
|
|
137
|
+
export function ensureContrast(color: RgbColor, background: RgbColor, minimum: number): RgbColor {
|
|
138
|
+
if (contrastRatio(color, background) >= minimum) return color
|
|
139
|
+
|
|
140
|
+
const renderedColor = color.a === 255 ? color : blendOver(color, background)
|
|
141
|
+
const source = toCanonicalColor(renderedColor)
|
|
142
|
+
const canvas = toCanonicalColor(background)
|
|
143
|
+
if (!source || !canvas) return color
|
|
144
|
+
return parseColor(repairedHex(source, canvas, minimum))!
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The contrast an accent is held to against its surface unless the host sets another. */
|
|
148
|
+
export const DEFAULT_ACCENT_CONTRAST_FLOOR = 3
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Normalise a custom accent against a surface: unparseable input is rejected
|
|
152
|
+
* (`undefined`), and any accepted colour is raised to `minimum` so an accent can
|
|
153
|
+
* never ship unreadable. Accepts every CSS hex length; for the stored grammar use
|
|
154
|
+
* {@link normalizeCustomAccent}.
|
|
155
|
+
*/
|
|
156
|
+
export function normalizeAccentValue(
|
|
157
|
+
value: string,
|
|
158
|
+
background: string,
|
|
159
|
+
minimum: number = DEFAULT_ACCENT_CONTRAST_FLOOR
|
|
160
|
+
): string | undefined {
|
|
161
|
+
const parsed = parseColor(value)
|
|
162
|
+
if (!parsed) return undefined
|
|
163
|
+
const backdrop = parseColor(background) ?? WHITE
|
|
164
|
+
return colorToHex(ensureContrast(parsed, backdrop, minimum))
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The stored form of a custom accent: `#rrggbb`, six digits, opaque, lower case.
|
|
169
|
+
*
|
|
170
|
+
* Deliberately narrower than {@link parseColor}. A stored accent is compared,
|
|
171
|
+
* deduplicated and used as an attribute, so it has one spelling; a host that
|
|
172
|
+
* accepts `#abc` in a field expands it before storing. Anything else — a name, an
|
|
173
|
+
* `rgb()`, a short or alpha form, a non-string — is not an accent and returns
|
|
174
|
+
* `undefined`.
|
|
175
|
+
*/
|
|
176
|
+
export function normalizeCustomAccent(value: unknown): string | undefined {
|
|
177
|
+
return typeof value === 'string' && /^#[0-9a-fA-F]{6}$/.test(value)
|
|
178
|
+
? value.toLowerCase()
|
|
179
|
+
: undefined
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Whether a selection's accent is the custom `#rrggbb` form (any letter case). */
|
|
183
|
+
export function isCustomAccent(value: unknown): boolean {
|
|
184
|
+
return normalizeCustomAccent(value) !== undefined
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The roles an accent paints, as CSS values: the catalogue's `AccentRoles`, so the
|
|
189
|
+
* provider, the generated stylesheet and a host's own paint cannot disagree about
|
|
190
|
+
* what a role is. `primarySubtle` is an expression over `var(--primary)`; the
|
|
191
|
+
* document writer never sets it (see {@link accentOverrideVariables}), an element
|
|
192
|
+
* paint must.
|
|
193
|
+
*/
|
|
194
|
+
export type AccentRoles = CatalogueAccentRoles
|
|
195
|
+
|
|
196
|
+
/** What an accent resolves against: the surface it sits on and the theme it sits in. */
|
|
197
|
+
export type AccentTarget = {
|
|
198
|
+
/** The theme, or only its appearance. A theme gives the tint its measured strength. */
|
|
199
|
+
target: Parameters<typeof accentRoles>[1]
|
|
200
|
+
/** The surface the accent must read on. Without it nothing can be measured or repaired. */
|
|
201
|
+
background: string | undefined
|
|
202
|
+
/**
|
|
203
|
+
* The contrast to hold the accent to against `background`. Absent, a preset is
|
|
204
|
+
* painted verbatim and a custom colour is held to {@link DEFAULT_ACCENT_CONTRAST_FLOOR}.
|
|
205
|
+
*/
|
|
206
|
+
floor?: number
|
|
207
|
+
/**
|
|
208
|
+
* The accents the selected theme pair carries (`ansi-blue`…), for an id that is
|
|
209
|
+
* neither a preset nor a colour.
|
|
210
|
+
*/
|
|
211
|
+
themeAccents?: readonly AccentPreset[]
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function appearanceOf(target: AccentTarget['target']): ThemeAppearance {
|
|
215
|
+
return typeof target === 'string' ? target : target.appearance
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* `value` raised to `floor` against `background` as hex, or `value` itself when it
|
|
220
|
+
* already clears the floor, is not a colour, or there is no surface to measure it on.
|
|
221
|
+
*
|
|
222
|
+
* Returning the argument itself is what makes the floor the identity for a preset
|
|
223
|
+
* that already reads: the caller compares and keeps the catalogue's own roles.
|
|
224
|
+
*/
|
|
225
|
+
function gateColor(value: string, background: string | undefined, floor: number): string {
|
|
226
|
+
const source = parseCanonicalColor(value)
|
|
227
|
+
const canvas = background ? parseCanonicalColor(background) : undefined
|
|
228
|
+
if (!source || !canvas) return value
|
|
229
|
+
if (canonicalContrastRatio(source, canvas) >= floor) return value
|
|
230
|
+
return repairedHex(source, canvas, floor)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The roles for a preset (or a custom colour made into one), held to the floor.
|
|
235
|
+
*
|
|
236
|
+
* A preset that clears the floor keeps the catalogue's roles untouched. One that
|
|
237
|
+
* does not is repaired to the nearest passing colour and the whole role set is
|
|
238
|
+
* rebuilt from the repaired value, so the label, hover and ring follow it instead
|
|
239
|
+
* of describing a colour that is no longer on screen.
|
|
240
|
+
*/
|
|
241
|
+
export function accentRolesForPreset(
|
|
242
|
+
preset: AccentPreset,
|
|
243
|
+
context: AccentTarget
|
|
244
|
+
): AccentRoles | undefined {
|
|
245
|
+
const appearance = appearanceOf(context.target)
|
|
246
|
+
const requested = accentValue(preset, appearance)
|
|
247
|
+
if (!parseCanonicalColor(requested)) return undefined
|
|
248
|
+
|
|
249
|
+
const floor = context.floor
|
|
250
|
+
if (floor === undefined) return accentRoles(preset, context.target)
|
|
251
|
+
|
|
252
|
+
const gated = gateColor(requested, context.background, floor)
|
|
253
|
+
if (gated === requested) return accentRoles(preset, context.target)
|
|
254
|
+
|
|
255
|
+
const roles = accentRoles({ ...preset, light: gated, dark: gated }, context.target)
|
|
256
|
+
// The tint's strength is measured against the primary it will sit over, and the
|
|
257
|
+
// repaired primary is not one the theme's own measurement saw.
|
|
258
|
+
return { ...roles, primarySubtle: primarySubtleCss(context.target, '--primary', [gated]) }
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Resolve an accent selection to its roles, or `undefined` when it overrides nothing.
|
|
263
|
+
*
|
|
264
|
+
* `undefined` is "use the theme's own primary": the literal `theme`, an id that is
|
|
265
|
+
* neither a preset, an offered theme accent nor a colour, and a colour that does not
|
|
266
|
+
* parse. A host that wants one of those to mean something else decides before
|
|
267
|
+
* asking (Adea's codec maps a stored `theme` to its default preset when it reads).
|
|
268
|
+
*/
|
|
269
|
+
export function resolveAccentRoles(accent: string, context: AccentTarget): AccentRoles | undefined {
|
|
270
|
+
if (accent === 'theme') return undefined
|
|
271
|
+
|
|
272
|
+
const custom = normalizeCustomAccent(accent)
|
|
273
|
+
if (custom) {
|
|
274
|
+
const floor = context.floor ?? DEFAULT_ACCENT_CONTRAST_FLOOR
|
|
275
|
+
return accentRolesForPreset(
|
|
276
|
+
{ id: 'custom', label: 'Custom', description: '', light: custom, dark: custom },
|
|
277
|
+
{ ...context, floor }
|
|
278
|
+
)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const preset =
|
|
282
|
+
ACCENTS.find((candidate) => candidate.id === accent) ??
|
|
283
|
+
context.themeAccents?.find((candidate) => candidate.id === accent)
|
|
284
|
+
return preset ? accentRolesForPreset(preset, context) : undefined
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The custom properties an accent override owns on `<html>`.
|
|
289
|
+
*
|
|
290
|
+
* `--primary-subtle` is deliberately not among them. It is a `color-mix()` over
|
|
291
|
+
* `var(--primary)`, so on the same element it follows an accent by itself, and its
|
|
292
|
+
* strength is the theme's, measured to keep body text at 4.5:1 on the tint.
|
|
293
|
+
* Writing one here would replace that measurement with the argument's.
|
|
294
|
+
*/
|
|
295
|
+
export function accentOverrideVariables(roles: AccentRoles): Record<string, string> {
|
|
296
|
+
return {
|
|
297
|
+
'--primary': roles.primary,
|
|
298
|
+
'--primary-foreground': roles.primaryForeground,
|
|
299
|
+
'--primary-hover': roles.primaryHover,
|
|
300
|
+
'--ring': roles.ring,
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The custom properties {@link paintAccentRoles} writes and clears. */
|
|
305
|
+
export const ACCENT_PAINT_PROPERTIES = [
|
|
306
|
+
'--primary',
|
|
307
|
+
'--primary-foreground',
|
|
308
|
+
'--primary-hover',
|
|
309
|
+
'--primary-subtle',
|
|
310
|
+
'--ring',
|
|
311
|
+
] as const
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Scope an accent to one element and its descendants.
|
|
315
|
+
*
|
|
316
|
+
* Unlike the document, an element must write `--primary-subtle` too. A custom
|
|
317
|
+
* property that uses `var()` is resolved where it is declared, so the tint inherited
|
|
318
|
+
* from `<html>` is already a mix over *`<html>`'s* primary and an element that only
|
|
319
|
+
* overrides `--primary` would keep the old tint. Declared here, the same expression
|
|
320
|
+
* resolves against this element's primary.
|
|
321
|
+
*
|
|
322
|
+
* `undefined` clears every property, and the element inherits the document's accent
|
|
323
|
+
* again.
|
|
324
|
+
*/
|
|
325
|
+
export function paintAccentRoles(
|
|
326
|
+
element: { style: Pick<CSSStyleDeclaration, 'setProperty' | 'removeProperty'> },
|
|
327
|
+
roles: AccentRoles | undefined
|
|
328
|
+
): void {
|
|
329
|
+
if (!roles) {
|
|
330
|
+
for (const property of ACCENT_PAINT_PROPERTIES) element.style.removeProperty(property)
|
|
331
|
+
return
|
|
332
|
+
}
|
|
333
|
+
element.style.setProperty('--primary', roles.primary)
|
|
334
|
+
element.style.setProperty('--primary-foreground', roles.primaryForeground)
|
|
335
|
+
element.style.setProperty('--primary-hover', roles.primaryHover)
|
|
336
|
+
element.style.setProperty('--primary-subtle', roles.primarySubtle)
|
|
337
|
+
element.style.setProperty('--ring', roles.ring)
|
|
338
|
+
}
|
|
@@ -181,7 +181,7 @@ export function normalizeAppearanceEditorFontSettings(
|
|
|
181
181
|
* stylesheet and survives CSS import reordering.
|
|
182
182
|
*/
|
|
183
183
|
export function applyAppearanceFontSettings(
|
|
184
|
-
root: HTMLElement,
|
|
184
|
+
root: Pick<HTMLElement, 'removeAttribute' | 'setAttribute' | 'style'>,
|
|
185
185
|
value: unknown
|
|
186
186
|
): NormalizedAppearanceEditorFontSettings {
|
|
187
187
|
const normalized = normalizeAppearanceEditorFontSettings(value)
|