@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.
- package/dist/tui/color.d.ts +8 -0
- package/dist/tui/color.js +67 -0
- package/dist/tui/expand.d.ts +4 -0
- package/dist/tui/expand.js +95 -0
- package/dist/tui/index.d.ts +9 -0
- package/dist/tui/index.js +8 -0
- package/dist/tui/resolve.d.ts +3362 -0
- package/dist/tui/resolve.js +265 -0
- package/dist/tui/schema.d.ts +4963 -0
- package/dist/tui/schema.js +240 -0
- package/dist/tui/select.d.ts +8 -0
- package/dist/tui/select.js +18 -0
- package/dist/tui/syntax.d.ts +3 -0
- package/dist/tui/syntax.js +78 -0
- package/dist/tui/types.d.ts +96 -0
- package/dist/tui/types.js +1 -0
- package/dist/tui/v1-migrate-new-designs.d.ts +3 -0
- package/dist/tui/v1-migrate-new-designs.js +353 -0
- package/dist/tui/v1-migrate.d.ts +30 -0
- package/dist/tui/v1-migrate.js +476 -0
- package/dist/tui/v1.d.ts +74 -0
- package/dist/tui/v1.js +1 -0
- package/package.json +43 -4
- package/README.md +0 -3
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
function clamp(value, min, max) {
|
|
2
|
+
return Math.max(min, Math.min(max, value));
|
|
3
|
+
}
|
|
4
|
+
function hue(value) {
|
|
5
|
+
return ((value % 360) + 360) % 360;
|
|
6
|
+
}
|
|
7
|
+
function linearToSrgb(value) {
|
|
8
|
+
if (value <= 0.0031308)
|
|
9
|
+
return value * 12.92;
|
|
10
|
+
return 1.055 * Math.pow(value, 1 / 2.4) - 0.055;
|
|
11
|
+
}
|
|
12
|
+
function srgbToLinear(value) {
|
|
13
|
+
if (value <= 0.04045)
|
|
14
|
+
return value / 12.92;
|
|
15
|
+
return Math.pow((value + 0.055) / 1.055, 2.4);
|
|
16
|
+
}
|
|
17
|
+
export function rgbToOklch(red, green, blue) {
|
|
18
|
+
const linearRed = srgbToLinear(red);
|
|
19
|
+
const linearGreen = srgbToLinear(green);
|
|
20
|
+
const linearBlue = srgbToLinear(blue);
|
|
21
|
+
const lRoot = Math.cbrt(0.4122214708 * linearRed + 0.5363325363 * linearGreen + 0.0514459929 * linearBlue);
|
|
22
|
+
const mRoot = Math.cbrt(0.2119034982 * linearRed + 0.6806995451 * linearGreen + 0.1073969566 * linearBlue);
|
|
23
|
+
const sRoot = Math.cbrt(0.0883024619 * linearRed + 0.2817188376 * linearGreen + 0.6299787005 * linearBlue);
|
|
24
|
+
const lightness = 0.2104542553 * lRoot + 0.793617785 * mRoot - 0.0040720468 * sRoot;
|
|
25
|
+
const a = 1.9779984951 * lRoot - 2.428592205 * mRoot + 0.4505937099 * sRoot;
|
|
26
|
+
const b = 0.0259040371 * lRoot + 0.7827717662 * mRoot - 0.808675766 * sRoot;
|
|
27
|
+
const chroma = Math.sqrt(a * a + b * b);
|
|
28
|
+
const angle = Math.atan2(b, a) * (180 / Math.PI);
|
|
29
|
+
return { l: lightness, c: chroma, h: angle < 0 ? angle + 360 : angle };
|
|
30
|
+
}
|
|
31
|
+
function oklchToRgb(color) {
|
|
32
|
+
const a = color.c * Math.cos((color.h * Math.PI) / 180);
|
|
33
|
+
const b = color.c * Math.sin((color.h * Math.PI) / 180);
|
|
34
|
+
const lRoot = color.l + 0.3963377774 * a + 0.2158037573 * b;
|
|
35
|
+
const mRoot = color.l - 0.1055613458 * a - 0.0638541728 * b;
|
|
36
|
+
const sRoot = color.l - 0.0894841775 * a - 1.291485548 * b;
|
|
37
|
+
const l = lRoot * lRoot * lRoot;
|
|
38
|
+
const m = mRoot * mRoot * mRoot;
|
|
39
|
+
const s = sRoot * sRoot * sRoot;
|
|
40
|
+
return {
|
|
41
|
+
r: linearToSrgb(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s),
|
|
42
|
+
g: linearToSrgb(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s),
|
|
43
|
+
b: linearToSrgb(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function fitOklch(color) {
|
|
47
|
+
const base = { l: clamp(color.l, 0, 1), c: Math.max(0, color.c), h: hue(color.h) };
|
|
48
|
+
const rgb = oklchToRgb(base);
|
|
49
|
+
if (rgb.r >= 0 && rgb.r <= 1 && rgb.g >= 0 && rgb.g <= 1 && rgb.b >= 0 && rgb.b <= 1)
|
|
50
|
+
return base;
|
|
51
|
+
const fitted = Array.from({ length: 24 }).reduce((result, _, index) => {
|
|
52
|
+
if (result)
|
|
53
|
+
return result;
|
|
54
|
+
const next = { ...base, c: base.c * Math.pow(0.9, index + 1) };
|
|
55
|
+
const output = oklchToRgb(next);
|
|
56
|
+
if (output.r >= 0 && output.r <= 1 && output.g >= 0 && output.g <= 1 && output.b >= 0 && output.b <= 1)
|
|
57
|
+
return next;
|
|
58
|
+
}, undefined);
|
|
59
|
+
return fitted ?? { ...base, c: 0 };
|
|
60
|
+
}
|
|
61
|
+
export function oklchToHex(color) {
|
|
62
|
+
const rgb = oklchToRgb(fitOklch(color));
|
|
63
|
+
const toHex = (value) => Math.round(clamp(value, 0, 1) * 255)
|
|
64
|
+
.toString(16)
|
|
65
|
+
.padStart(2, "0");
|
|
66
|
+
return `#${toHex(rgb.r)}${toHex(rgb.g)}${toHex(rgb.b)}`;
|
|
67
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ModeDefinition, ThemeTokensDefinition } from "./index.js";
|
|
2
|
+
export declare function expandTheme<Definition extends ModeDefinition>(definition: Definition): Definition;
|
|
3
|
+
export declare function expandTokens(definition: ThemeTokensDefinition): ThemeTokensDefinition;
|
|
4
|
+
export declare function mergeTheme(...values: unknown[]): Record<string, unknown>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { ActionState } from "./schema.js";
|
|
2
|
+
export function expandTheme(definition) {
|
|
3
|
+
return {
|
|
4
|
+
...definition,
|
|
5
|
+
...expandTokens(definition),
|
|
6
|
+
...(definition["@dialog"] ? { "@dialog": expandDialog(definition["@dialog"]) } : {}),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function expandDialog(definition) {
|
|
10
|
+
return {
|
|
11
|
+
...expandTokens(definition),
|
|
12
|
+
...(definition["@footer"] ? { "@footer": expandTokens(definition["@footer"]) } : {}),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function expandTokens(definition) {
|
|
16
|
+
return {
|
|
17
|
+
...definition,
|
|
18
|
+
text: expandText(definition.text),
|
|
19
|
+
background: expandBackground(definition.background),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function mergeTheme(...values) {
|
|
23
|
+
return values.reduce((result, value) => {
|
|
24
|
+
if (!isRecord(value))
|
|
25
|
+
return result;
|
|
26
|
+
return Object.entries(value).reduce((next, [key, item]) => {
|
|
27
|
+
if (item === undefined)
|
|
28
|
+
return next;
|
|
29
|
+
return {
|
|
30
|
+
...next,
|
|
31
|
+
[key]: isRecord(item) ? mergeTheme(next[key], item) : item,
|
|
32
|
+
};
|
|
33
|
+
}, result);
|
|
34
|
+
}, {});
|
|
35
|
+
}
|
|
36
|
+
function expandText(definition) {
|
|
37
|
+
if (!definition)
|
|
38
|
+
return;
|
|
39
|
+
return {
|
|
40
|
+
...definition,
|
|
41
|
+
muted: definition.muted ?? (definition.base ? "$text.base" : undefined),
|
|
42
|
+
faint: definition.faint ?? (definition.muted || definition.base ? "$text.muted" : undefined),
|
|
43
|
+
// Selected text reads like the focused row of a list unless the theme picks its own colors.
|
|
44
|
+
selection: definition.selection ?? (definition.base ? "$text.action.primary.$focused" : undefined),
|
|
45
|
+
action: expandActions(definition.action, "text.action"),
|
|
46
|
+
formfield: expandFormfield(definition.formfield, "text.formfield"),
|
|
47
|
+
feedback: definition.feedback
|
|
48
|
+
? Object.fromEntries(Object.entries(definition.feedback).map(([kind, feedback]) => {
|
|
49
|
+
return [
|
|
50
|
+
kind,
|
|
51
|
+
{
|
|
52
|
+
...feedback,
|
|
53
|
+
muted: feedback.muted ?? (feedback.base ? `$text.feedback.${kind}.base` : undefined),
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
}))
|
|
57
|
+
: undefined,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function expandBackground(definition) {
|
|
61
|
+
if (!definition)
|
|
62
|
+
return;
|
|
63
|
+
return {
|
|
64
|
+
...definition,
|
|
65
|
+
selection: definition.selection ?? (definition.base ? "$background.action.primary.$focused" : undefined),
|
|
66
|
+
action: expandActions(definition.action, "background.action"),
|
|
67
|
+
formfield: expandFormfield(definition.formfield, "background.formfield"),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function expandFormfield(definition, path) {
|
|
71
|
+
if (!definition?.base)
|
|
72
|
+
return definition;
|
|
73
|
+
return {
|
|
74
|
+
...definition,
|
|
75
|
+
...Object.fromEntries(ActionState.literals.map((state) => [`$${state}`, definition[`$${state}`] ?? `$${path}.base`])),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function expandActions(definition, path) {
|
|
79
|
+
if (!definition)
|
|
80
|
+
return;
|
|
81
|
+
return Object.fromEntries(Object.entries(definition).map(([variant, value]) => {
|
|
82
|
+
if (!value?.base)
|
|
83
|
+
return [variant, value];
|
|
84
|
+
return [
|
|
85
|
+
variant,
|
|
86
|
+
{
|
|
87
|
+
...value,
|
|
88
|
+
...Object.fromEntries(ActionState.literals.map((state) => [`$${state}`, value[`$${state}`] ?? `$${path}.${variant}.base`])),
|
|
89
|
+
},
|
|
90
|
+
];
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
function isRecord(value) {
|
|
94
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
95
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { ActionState, type ActionStateKey, ActionVariant, BaseThemeDefinition, CategoricalDefinition, FeedbackKind, FormfieldState, type FormfieldStateKey, HueName, HueStep, MarkdownDefinition, MarkdownToken, ModeDefinition, SurfaceName, SyntaxDefinition, SyntaxToken, ThemeDefinition, ThemeDocument, type BackgroundDefinition, type DialogTokensDefinition, type DiffDefinition, type FormfieldColorDefinition, type HueDefinition, type Mode, type SemanticHue, type StatefulColorDefinition, type TextDefinition, type ThemeTokensDefinition, } from "./schema.js";
|
|
2
|
+
export type { ActionStates, Categorical, FormfieldColor, Hue, HueSource, HueScale, ResolvedActionState, ResolvedFormfieldState, ResolvedTheme, ResolvedThemeTokens, StatefulColor, } from "./types.js";
|
|
3
|
+
export { rgbToOklch } from "./color.js";
|
|
4
|
+
export { expandTheme } from "./expand.js";
|
|
5
|
+
export { migrateV1 } from "./v1-migrate.js";
|
|
6
|
+
export { migrateV1NewDesigns } from "./v1-migrate-new-designs.js";
|
|
7
|
+
export { parseThemeDocument, resolveTheme, resolveThemeDocument, themeDecodeError } from "./resolve.js";
|
|
8
|
+
export { selectTheme, selectThemeMode, supportsThemeMode, themeModes } from "./select.js";
|
|
9
|
+
export { generateSyntax } from "./syntax.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { ActionState, ActionVariant, BaseThemeDefinition, CategoricalDefinition, FeedbackKind, FormfieldState, HueName, HueStep, MarkdownDefinition, MarkdownToken, ModeDefinition, SurfaceName, SyntaxDefinition, SyntaxToken, ThemeDefinition, ThemeDocument, } from "./schema.js";
|
|
2
|
+
export { rgbToOklch } from "./color.js";
|
|
3
|
+
export { expandTheme } from "./expand.js";
|
|
4
|
+
export { migrateV1 } from "./v1-migrate.js";
|
|
5
|
+
export { migrateV1NewDesigns } from "./v1-migrate-new-designs.js";
|
|
6
|
+
export { parseThemeDocument, resolveTheme, resolveThemeDocument, themeDecodeError } from "./resolve.js";
|
|
7
|
+
export { selectTheme, selectThemeMode, supportsThemeMode, themeModes } from "./select.js";
|
|
8
|
+
export { generateSyntax } from "./syntax.js";
|