@radheim/theme 0.0.0-stage → 2.4.3

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.
@@ -0,0 +1,353 @@
1
+ import { RGBA } from "@opentui/core";
2
+ import { oklchToHex } from "./color.js";
3
+ import { HueStep } from "./schema.js";
4
+ import { base, byte, categoricalTokens, chromaticHues, detectMode, dialogBackground, hex, inferHues, referenceHues, resolveV1, selectedForeground, toOklch, } from "./v1-migrate.js";
5
+ // Temporary V1 migration for the "new-designs" experiment. It lays V1 colors out on the opencode theme's steps and
6
+ // replaces migrateV1 once the experiment ships.
7
+ export function migrateV1NewDesigns(theme) {
8
+ const light = resolveV1(theme, "light");
9
+ const dark = resolveV1(theme, "dark");
10
+ if (light.background.a > 0 && dark.background.a > 0 && light.background.equals(dark.background)) {
11
+ const lightMode = detectMode(light);
12
+ const darkMode = detectMode(dark);
13
+ if (lightMode === darkMode) {
14
+ const definition = migrateMode(lightMode === "light" ? light : dark, lightMode);
15
+ if (lightMode === "light")
16
+ return { base: base(definition), light: lightOverrides(definition, light) };
17
+ return { base: base(definition), dark: { hue: definition.hue } };
18
+ }
19
+ }
20
+ const lightDefinition = migrateMode(light, "light");
21
+ const darkDefinition = migrateMode(dark, "dark");
22
+ return {
23
+ base: base(lightDefinition),
24
+ light: lightOverrides(lightDefinition, light),
25
+ dark: darkDefinition,
26
+ };
27
+ }
28
+ // Light dialogs sit a step below the page like the opencode theme's, so rows can hover above them.
29
+ const lightDialog = {
30
+ background: {
31
+ base: "$hue.neutral.800",
32
+ raised: { high: "$hue.neutral.700" },
33
+ action: {
34
+ primary: { $hovered: "$hue.neutral.600" },
35
+ secondary: { $hovered: "$hue.neutral.600" },
36
+ },
37
+ },
38
+ };
39
+ function lightOverrides(definition, theme) {
40
+ if (theme.backgroundPanel.a === 1)
41
+ return { hue: definition.hue, "@dialog": lightDialog };
42
+ return {
43
+ hue: definition.hue,
44
+ "@dialog": { ...lightDialog, background: { ...lightDialog.background, base: dialogBackground(theme) } },
45
+ };
46
+ }
47
+ // Mirrors the opencode theme's token layout: text at 100/200/300, the page at 900, raised surfaces at 700/600/500,
48
+ // and each V1 color anchored at 400 with quieter steps around it.
49
+ function migrateMode(theme, mode) {
50
+ const color = (key) => hex(theme[key]);
51
+ const neutral = neutralScale(theme, mode);
52
+ const primary = "$hue.interactive.400";
53
+ return referenceHues({
54
+ ...paletteHues(theme, neutral.scale, (color) => hueScale(color, mode, neutral)),
55
+ text: {
56
+ base: "$hue.neutral.100",
57
+ muted: "$hue.neutral.200",
58
+ faint: "$hue.neutral.300",
59
+ action: {
60
+ primary: {
61
+ base: "$text.base",
62
+ $disabled: "$text.faint",
63
+ $focused: "$text.base",
64
+ $selected: primary,
65
+ },
66
+ secondary: { base: "$text.muted", $hovered: "$text.base", $focused: "$hue.interactive.200" },
67
+ destructive: { base: hex(selectedForeground(theme, theme.error)), $disabled: "$text.faint" },
68
+ },
69
+ formfield: {
70
+ base: "$text.base",
71
+ $hovered: primary,
72
+ $focused: primary,
73
+ $pressed: primary,
74
+ $disabled: "$text.faint",
75
+ $selected: primary,
76
+ },
77
+ feedback: {
78
+ error: { base: color("error") },
79
+ warning: { base: color("warning") },
80
+ success: { base: color("success") },
81
+ info: { base: color("info") },
82
+ },
83
+ },
84
+ background: {
85
+ base: "$hue.neutral.900",
86
+ raised: {
87
+ base: "$hue.neutral.700",
88
+ high: "$hue.neutral.600",
89
+ max: "$hue.neutral.500",
90
+ },
91
+ action: {
92
+ primary: {
93
+ base: "transparent",
94
+ $hovered: "$hue.neutral.700",
95
+ $focused: "$hue.interactive.600",
96
+ $selected: "transparent",
97
+ },
98
+ secondary: { base: "transparent" },
99
+ destructive: { base: color("error") },
100
+ },
101
+ formfield: {
102
+ base: "$background.base",
103
+ },
104
+ feedback: {
105
+ error: { base: "$background.base" },
106
+ warning: { base: "$background.base" },
107
+ success: { base: "$background.base" },
108
+ info: { base: "$background.base" },
109
+ },
110
+ },
111
+ ...paletteTokens(theme),
112
+ "@dialog": {
113
+ background: {
114
+ base: dialogBackground(theme),
115
+ action: {
116
+ primary: { $hovered: "$background.raised.max" },
117
+ secondary: { $hovered: "$background.raised.max" },
118
+ },
119
+ },
120
+ },
121
+ });
122
+ }
123
+ // Step profiles measured from the opencode theme. Only lightness and chroma are taken from it; hues and
124
+ // anchors always come from the V1 theme being migrated.
125
+ const profiles = {
126
+ dark: {
127
+ // Lightness of each surface step above the page background.
128
+ surface: { 900: 0, 800: 0.039, 700: 0.057, 600: 0.11, 500: 0.175, 400: 0.253 },
129
+ // Where muted and faint text sit between the page (0) and the text color (1).
130
+ text: { 200: 0.719, 300: 0.538 },
131
+ // Lightness of each chromatic step relative to the neutral step of the same number.
132
+ hueLightness: { 100: -0.035, 200: -0.023, 300: -0.003, 600: 0.095, 700: 0.071, 800: 0.024, 900: 0.012 },
133
+ // Chroma of each chromatic step as a share of the anchor's chroma.
134
+ hueChroma: { 100: 0.1, 200: 0.24, 300: 0.22, 500: 0.64, 600: 0.2, 700: 0.14, 800: 0.09, 900: 0.07 },
135
+ // Step 500 sits this far from the anchor toward step 600.
136
+ hueMid: 0.55,
137
+ // Least share of the text-on-page contrast that text keeps on each background step.
138
+ readable: { 800: 0.95, 700: 0.92, 600: 0.81, 500: 0.66, 900: 0.99 },
139
+ readableHue: { 600: 0.7, 700: 0.82, 800: 0.92, 900: 0.99 },
140
+ },
141
+ light: {
142
+ surface: { 900: 0, 800: -0.015, 700: -0.039, 600: -0.102, 500: -0.145, 400: -0.227 },
143
+ text: { 200: 0.688, 300: 0.484 },
144
+ hueLightness: { 100: 0.083, 200: 0.13, 300: 0.116, 600: 0.021, 700: -0.002, 800: -0.004, 900: -0.008 },
145
+ hueChroma: { 100: 0.19, 200: 0.12, 300: 0.16, 500: 0.38, 600: 0.17, 700: 0.1, 800: 0.05, 900: 0.02 },
146
+ hueMid: 0.65,
147
+ readable: { 800: 0.96, 700: 0.89, 600: 0.74, 500: 0.64, 900: 0.97 },
148
+ readableHue: { 600: 0.78, 700: 0.88, 800: 0.93, 900: 0.97 },
149
+ },
150
+ };
151
+ // Body text on surfaces and selections should stay comfortable to read even in low-contrast themes.
152
+ const minimumSurfaceContrast = 4.5;
153
+ const maximumDimming = 0.6;
154
+ function neutralScale(theme, mode) {
155
+ const profile = profiles[mode];
156
+ const opaque = (color) => color.toInts()[3] > 0;
157
+ const element = [theme.backgroundElement, theme.backgroundMenu].find(opaque);
158
+ const sources = [
159
+ { step: 900, color: theme.background },
160
+ { step: 700, color: theme.backgroundPanel },
161
+ { step: 600, color: element },
162
+ ];
163
+ const reference = sources.find((source) => source.color && opaque(source.color));
164
+ const page = reference
165
+ ? toOklch(reference.color)
166
+ : toOklch(mode === "dark" ? RGBA.fromInts(0, 0, 0) : RGBA.fromInts(255, 255, 255));
167
+ const pageLightness = page.l - (reference ? profile.surface[reference.step] : 0);
168
+ const tint = (...colors) => {
169
+ const color = colors.find((color) => color && opaque(color));
170
+ return color ? toOklch(color) : page;
171
+ };
172
+ const surfaceTint = {
173
+ 900: tint(theme.background, theme.backgroundPanel, element),
174
+ 800: tint(theme.backgroundPanel, theme.background, element),
175
+ 700: tint(theme.backgroundPanel, theme.background, element),
176
+ 600: tint(element, theme.backgroundPanel, theme.background),
177
+ 500: tint(element, theme.backgroundPanel, theme.background),
178
+ 400: tint(element, theme.backgroundPanel, theme.background),
179
+ };
180
+ const lightness = {};
181
+ // Lightness before readability dimming, so chromatic steps keep their place relative to the profile.
182
+ const planned = {};
183
+ const scale = {};
184
+ const text = toOklch(theme.text);
185
+ const pageColor = { ...surfaceTint[900], l: pageLightness };
186
+ const pageContrast = contrast(theme.text, RGBA.fromHex(oklchToHex(pageColor)));
187
+ // Pulls a generated background back toward the page until text on it keeps enough contrast.
188
+ const readable = (color, share) => {
189
+ const target = Math.max(pageContrast * share, Math.min(minimumSurfaceContrast, pageContrast * 0.95));
190
+ const passes = (amount) => contrast(theme.text, RGBA.fromHex(oklchToHex(mix(color, pageColor, amount)))) >= target;
191
+ if (passes(0))
192
+ return color;
193
+ // Stop short of the page so surfaces and selections stay visible in low-contrast themes.
194
+ if (!passes(maximumDimming))
195
+ return mix(color, pageColor, maximumDimming);
196
+ let [low, high] = [0, maximumDimming];
197
+ for (let index = 0; index < 12; index++) {
198
+ const amount = (low + high) / 2;
199
+ if (passes(amount))
200
+ high = amount;
201
+ else
202
+ low = amount;
203
+ }
204
+ return mix(color, pageColor, high);
205
+ };
206
+ for (const step of [800, 700, 600, 500, 400]) {
207
+ const generated = { ...surfaceTint[step], l: pageLightness + profile.surface[step] };
208
+ const color = step === 400 ? generated : readable(generated, profile.readable[step]);
209
+ planned[step] = generated.l;
210
+ lightness[step] = color.l;
211
+ scale[step] = oklchToHex(color);
212
+ }
213
+ lightness[900] = pageLightness;
214
+ scale[900] = hex(theme.background);
215
+ // Transparent V1 surfaces stay transparent so the terminal shows through, as they did before.
216
+ for (const [step, color] of [
217
+ [800, theme.backgroundPanel],
218
+ [700, theme.backgroundPanel],
219
+ [600, theme.backgroundElement],
220
+ ]) {
221
+ if (!opaque(color))
222
+ scale[step] = hex(color);
223
+ }
224
+ lightness[100] = text.l;
225
+ scale[100] = hex(theme.text);
226
+ for (const step of [200, 300]) {
227
+ const generated = mix(pageColor, text, profile.text[step]);
228
+ lightness[step] = generated.l;
229
+ scale[step] = oklchToHex(generated);
230
+ }
231
+ // Keep the theme's own muted text on whichever text step it is closest to.
232
+ const muted = toOklch(theme.textMuted);
233
+ const mutedStep = Math.abs(muted.l - lightness[200]) <= Math.abs(muted.l - lightness[300]) ? 200 : 300;
234
+ lightness[mutedStep] = muted.l;
235
+ scale[mutedStep] = hex(theme.textMuted);
236
+ return { scale, lightness: { ...lightness, ...planned }, readable };
237
+ }
238
+ function hueScale(color, mode, neutral) {
239
+ const profile = profiles[mode];
240
+ const anchor = toOklch(color);
241
+ const alpha = color.toInts()[3];
242
+ const generate = (generated) => {
243
+ const value = oklchToHex(generated);
244
+ return alpha === 255 ? value : `${value}${byte(alpha)}`;
245
+ };
246
+ const lightness = (step) => neutral.lightness[step] + profile.hueLightness[step];
247
+ return Object.fromEntries(HueStep.literals.map((step) => {
248
+ if (step === 400)
249
+ return [step, hex(color)];
250
+ const generated = {
251
+ l: step === 500 ? anchor.l + (lightness(600) - anchor.l) * profile.hueMid : lightness(step),
252
+ c: anchor.c * profile.hueChroma[step],
253
+ h: anchor.h,
254
+ };
255
+ if (step === 600 || step === 700 || step === 800 || step === 900)
256
+ return [step, generate(neutral.readable(generated, profile.readableHue[step]))];
257
+ return [step, generate(generated)];
258
+ }));
259
+ }
260
+ function contrast(first, second) {
261
+ const luminance = (color) => {
262
+ const channel = (value) => (value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4);
263
+ return 0.2126 * channel(color.r) + 0.7152 * channel(color.g) + 0.0722 * channel(color.b);
264
+ };
265
+ const [lighter, darker] = [luminance(first), luminance(second)].sort((a, b) => b - a);
266
+ return (lighter + 0.05) / (darker + 0.05);
267
+ }
268
+ function mix(start, end, amount) {
269
+ const startHue = start.c > 0.002 ? start.h : end.h;
270
+ const endHue = end.c > 0.002 ? end.h : startHue;
271
+ const hue = ((((endHue - startHue) % 360) + 540) % 360) - 180;
272
+ return {
273
+ l: start.l + (end.l - start.l) * amount,
274
+ c: start.c + (end.c - start.c) * amount,
275
+ h: startHue + hue * amount,
276
+ };
277
+ }
278
+ function paletteHues(theme, gray, scale) {
279
+ const hues = inferHues(theme);
280
+ const categorical = categoricalTokens.flatMap((token) => {
281
+ const hue = hues.byToken[token];
282
+ return hue ? [hue] : [];
283
+ });
284
+ const uniqueCategorical = categorical.filter((hue, index) => categorical.indexOf(hue) === index);
285
+ return {
286
+ hue: {
287
+ gray,
288
+ ...Object.fromEntries(chromaticHues.flatMap((name) => {
289
+ const match = hues.byHue[name];
290
+ return match ? [[name, scale(match.color)]] : [];
291
+ })),
292
+ accent: hues.byToken.accent ? `$hue.${hues.byToken.accent}` : "$hue.gray",
293
+ interactive: hues.byToken.primary ? `$hue.${hues.byToken.primary}` : "$hue.gray",
294
+ neutral: "$hue.gray",
295
+ },
296
+ categorical: uniqueCategorical.length ? uniqueCategorical : ["neutral"],
297
+ };
298
+ }
299
+ function paletteTokens(theme) {
300
+ const color = (key) => hex(theme[key]);
301
+ return {
302
+ border: { base: color("border") },
303
+ scrollbar: { base: color("borderActive") },
304
+ diff: {
305
+ text: {
306
+ added: color("diffAdded"),
307
+ removed: color("diffRemoved"),
308
+ context: color("diffContext"),
309
+ hunkHeader: color("diffHunkHeader"),
310
+ },
311
+ background: {
312
+ added: color("diffAddedBg"),
313
+ removed: color("diffRemovedBg"),
314
+ context: color("diffContextBg"),
315
+ },
316
+ highlight: { added: color("diffHighlightAdded"), removed: color("diffHighlightRemoved") },
317
+ lineNumber: {
318
+ text: color("diffLineNumber"),
319
+ background: {
320
+ added: color("diffAddedLineNumberBg"),
321
+ removed: color("diffRemovedLineNumberBg"),
322
+ },
323
+ },
324
+ },
325
+ syntax: {
326
+ comment: color("syntaxComment"),
327
+ keyword: color("syntaxKeyword"),
328
+ function: color("syntaxFunction"),
329
+ variable: color("syntaxVariable"),
330
+ string: color("syntaxString"),
331
+ number: color("syntaxNumber"),
332
+ type: color("syntaxType"),
333
+ operator: color("syntaxOperator"),
334
+ punctuation: color("syntaxPunctuation"),
335
+ },
336
+ markdown: {
337
+ text: color("markdownText"),
338
+ heading: color("markdownHeading"),
339
+ link: color("markdownLink"),
340
+ linkText: color("markdownLinkText"),
341
+ code: color("markdownCode"),
342
+ blockQuote: color("markdownBlockQuote"),
343
+ emphasis: color("markdownEmph"),
344
+ strong: color("markdownStrong"),
345
+ horizontalRule: color("markdownHorizontalRule"),
346
+ listItem: color("markdownListItem"),
347
+ listEnumeration: color("markdownListEnumeration"),
348
+ image: color("markdownImage"),
349
+ imageText: color("markdownImageText"),
350
+ codeBlock: color("markdownCodeBlock"),
351
+ },
352
+ };
353
+ }
@@ -0,0 +1,30 @@
1
+ import { RGBA } from "@opentui/core";
2
+ import type { BaseThemeDefinition, Mode, ThemeDefinition, ThemeDocument } from "./index.js";
3
+ import type { Theme, ThemeV1Json } from "./v1.js";
4
+ export type ThemeColor = Exclude<keyof Theme, "thinkingOpacity" | "_hasSelectedListItemText">;
5
+ type ChromaticHue = "red" | "orange" | "yellow" | "green" | "cyan" | "blue" | "purple";
6
+ type V1HueToken = "secondary" | "accent" | "success" | "warning" | "primary" | "error" | "info";
7
+ export declare const chromaticHues: readonly ChromaticHue[];
8
+ export declare const categoricalTokens: readonly V1HueToken[];
9
+ export declare function migrateV1(theme: ThemeV1Json): ThemeDocument;
10
+ export declare function base(definition: ThemeDefinition): BaseThemeDefinition;
11
+ export declare function detectMode(theme: Theme): Mode;
12
+ export declare function dialogBackground(theme: Theme): string;
13
+ export declare function referenceHues(theme: ThemeDefinition): ThemeDefinition;
14
+ export declare function inferHues(theme: Theme): {
15
+ byHue: Partial<Record<ChromaticHue, {
16
+ color: RGBA;
17
+ distance: number;
18
+ }>>;
19
+ byToken: Partial<Record<V1HueToken, ChromaticHue>>;
20
+ };
21
+ export declare function resolveV1(theme: ThemeV1Json, mode: "dark" | "light"): Theme;
22
+ export declare function selectedForeground(theme: Theme, background: RGBA): RGBA;
23
+ export declare function toOklch(color: RGBA): {
24
+ l: number;
25
+ c: number;
26
+ h: number;
27
+ };
28
+ export declare function hex(color: RGBA): string;
29
+ export declare function byte(value: number): string;
30
+ export {};