@clhaas/palette-kit 0.3.0 → 0.4.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/CHANGELOG.md +25 -0
- package/README.md +80 -87
- package/dist/contrast/contrast.d.ts +16 -0
- package/dist/contrast/contrast.js +102 -0
- package/dist/core/intent-registry.d.ts +11 -0
- package/dist/core/intent-registry.js +70 -0
- package/dist/core/oklch.d.ts +16 -0
- package/dist/core/oklch.js +56 -0
- package/dist/create-palette-kit.d.ts +9 -0
- package/dist/create-palette-kit.js +67 -0
- package/dist/engine/context/context.d.ts +13 -0
- package/dist/engine/context/context.js +37 -0
- package/dist/engine/level/curves.d.ts +17 -0
- package/dist/engine/level/curves.js +49 -0
- package/dist/engine/level/level.d.ts +4 -0
- package/dist/engine/level/level.js +13 -0
- package/dist/engine/relation/relation.d.ts +105 -0
- package/dist/engine/relation/relation.js +137 -0
- package/dist/engine/resolve/resolve.d.ts +36 -0
- package/dist/engine/resolve/resolve.js +116 -0
- package/dist/engine/state/state.d.ts +46 -0
- package/dist/engine/state/state.js +68 -0
- package/dist/engine/usage/fill.d.ts +9 -0
- package/dist/engine/usage/fill.js +9 -0
- package/dist/engine/usage/lines.d.ts +9 -0
- package/dist/engine/usage/lines.js +9 -0
- package/dist/engine/usage/overlays.d.ts +9 -0
- package/dist/engine/usage/overlays.js +9 -0
- package/dist/engine/usage/strategy.d.ts +56 -0
- package/dist/engine/usage/strategy.js +30 -0
- package/dist/engine/usage/visualVocabulary.d.ts +9 -0
- package/dist/engine/usage/visualVocabulary.js +9 -0
- package/dist/export/serialize.d.ts +14 -0
- package/dist/export/serialize.js +89 -0
- package/dist/export/types.d.ts +37 -0
- package/dist/export/types.js +31 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/operators/convert.d.ts +32 -0
- package/dist/operators/convert.js +80 -0
- package/dist/presets/presets.d.ts +95 -0
- package/dist/presets/presets.js +308 -0
- package/dist/types/index.d.ts +111 -187
- package/dist/utils/errors/errors.d.ts +17 -0
- package/dist/utils/errors/errors.js +22 -0
- package/docs/API.md +167 -0
- package/docs/Alpha.md +14 -0
- package/docs/Architecture.md +56 -0
- package/docs/CLI.md +22 -0
- package/docs/Concepts.md +73 -0
- package/docs/Config.md +144 -0
- package/docs/Diagnostics.md +22 -0
- package/docs/Exporters.md +33 -0
- package/docs/FAQ.md +59 -0
- package/docs/Migration.md +61 -0
- package/docs/Overlays.md +33 -0
- package/docs/README.md +60 -0
- package/docs/Text.md +41 -0
- package/docs/Tokens.md +42 -0
- package/docs/Usage-JSON.md +39 -0
- package/docs/Usage-ReactNative.md +63 -0
- package/docs/Usage-Web.md +66 -0
- package/docs/Validation.md +97 -0
- package/docs/Why.md +37 -0
- package/docs/_api-surface.md +53 -0
- package/docs/snippets/serialize-oklch.md +9 -0
- package/docs/spec.md +98 -0
- package/package.json +74 -59
- package/.codex/skills/color-pipeline-implementer/SKILL.md +0 -23
- package/.codex/skills/commit-message-crafter/SKILL.md +0 -63
- package/.codex/skills/commit-message-crafter/references/benchmarks.md +0 -20
- package/.codex/skills/contrast-solver-helper/SKILL.md +0 -20
- package/.codex/skills/exporters-builder/SKILL.md +0 -20
- package/.codex/skills/markdownlint-writer/SKILL.md +0 -32
- package/.codex/skills/phase-implementation-runbook/SKILL.md +0 -92
- package/.codex/skills/type-contract-auditor/SKILL.md +0 -21
- package/.github/skills/review-guide/SKILL.md +0 -23
- package/.github/skills/review-guide/references/review-guide-v0.3.md +0 -629
- package/.markdownlint.json +0 -4
- package/AGENTS.md +0 -16
- package/biome.json +0 -43
- package/dist/cli/args.d.ts +0 -12
- package/dist/cli/args.js +0 -56
- package/dist/cli/args.test.d.ts +0 -1
- package/dist/cli/args.test.js +0 -22
- package/dist/cli/codegen/__snapshots__/tokens.test.js.snap +0 -87
- package/dist/cli/codegen/tokens.d.ts +0 -12
- package/dist/cli/codegen/tokens.js +0 -139
- package/dist/cli/codegen/tokens.test.d.ts +0 -1
- package/dist/cli/codegen/tokens.test.js +0 -51
- package/dist/cli/config.d.ts +0 -40
- package/dist/cli/config.js +0 -34
- package/dist/cli/validate.d.ts +0 -2
- package/dist/cli/validate.js +0 -33
- package/dist/cli/validate.test.d.ts +0 -1
- package/dist/cli/validate.test.js +0 -40
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -148
- package/dist/contrast/apca.d.ts +0 -2
- package/dist/contrast/apca.js +0 -15
- package/dist/contrast/apca.test.d.ts +0 -1
- package/dist/contrast/apca.test.js +0 -16
- package/dist/contrast/index.d.ts +0 -4
- package/dist/contrast/index.js +0 -4
- package/dist/contrast/scoring.d.ts +0 -4
- package/dist/contrast/scoring.js +0 -31
- package/dist/contrast/scoring.test.d.ts +0 -1
- package/dist/contrast/scoring.test.js +0 -148
- package/dist/contrast/solver.d.ts +0 -13
- package/dist/contrast/solver.js +0 -170
- package/dist/contrast/solver.test.d.ts +0 -1
- package/dist/contrast/solver.test.js +0 -75
- package/dist/contrast/types.d.ts +0 -17
- package/dist/contrast/types.js +0 -1
- package/dist/contrast/utils.d.ts +0 -4
- package/dist/contrast/utils.js +0 -18
- package/dist/contrast/wcag2.d.ts +0 -3
- package/dist/contrast/wcag2.js +0 -19
- package/dist/contrast/wcag2.test.d.ts +0 -1
- package/dist/contrast/wcag2.test.js +0 -17
- package/dist/core/createTheme.d.ts +0 -35
- package/dist/core/createTheme.js +0 -24
- package/dist/core/dx-helpers.test.d.ts +0 -1
- package/dist/core/dx-helpers.test.js +0 -61
- package/dist/core/index.d.ts +0 -2
- package/dist/core/index.js +0 -2
- package/dist/core/onSolid.test.d.ts +0 -1
- package/dist/core/onSolid.test.js +0 -118
- package/dist/core/qa.v1.test.d.ts +0 -1
- package/dist/core/qa.v1.test.js +0 -112
- package/dist/core/resolve.d.ts +0 -3
- package/dist/core/resolve.js +0 -8
- package/dist/core/resolve.test.d.ts +0 -1
- package/dist/core/resolve.test.js +0 -89
- package/dist/core/resolveMany.d.ts +0 -8
- package/dist/core/resolveMany.js +0 -17
- package/dist/core/tokenRegistry.d.ts +0 -23
- package/dist/core/tokenRegistry.js +0 -83
- package/dist/core/tokenRegistry.test.d.ts +0 -1
- package/dist/core/tokenRegistry.test.js +0 -133
- package/dist/engine/applyOperators.d.ts +0 -3
- package/dist/engine/applyOperators.js +0 -23
- package/dist/engine/context.d.ts +0 -4
- package/dist/engine/context.js +0 -1
- package/dist/engine/gamut.d.ts +0 -13
- package/dist/engine/gamut.js +0 -101
- package/dist/engine/gamut.test.d.ts +0 -1
- package/dist/engine/gamut.test.js +0 -23
- package/dist/engine/generateScale.d.ts +0 -15
- package/dist/engine/generateScale.js +0 -29
- package/dist/engine/generateScale.test.d.ts +0 -1
- package/dist/engine/generateScale.test.js +0 -32
- package/dist/engine/index.d.ts +0 -8
- package/dist/engine/index.js +0 -4
- package/dist/engine/normalize.d.ts +0 -43
- package/dist/engine/normalize.js +0 -403
- package/dist/engine/normalize.test.d.ts +0 -1
- package/dist/engine/normalize.test.js +0 -136
- package/dist/engine/onSolid.d.ts +0 -3
- package/dist/engine/onSolid.js +0 -110
- package/dist/engine/resolveBaseColor.d.ts +0 -25
- package/dist/engine/resolveBaseColor.js +0 -127
- package/dist/engine/resolveBaseColor.test.d.ts +0 -1
- package/dist/engine/resolveBaseColor.test.js +0 -97
- package/dist/export/__snapshots__/exportTheme.test.js.snap +0 -74
- package/dist/export/exportTheme.d.ts +0 -47
- package/dist/export/exportTheme.js +0 -170
- package/dist/export/exportTheme.test.d.ts +0 -1
- package/dist/export/exportTheme.test.js +0 -118
- package/dist/export/index.d.ts +0 -1
- package/dist/export/index.js +0 -1
- package/dist/export/serializeColor.d.ts +0 -1
- package/dist/export/serializeColor.js +0 -1
- package/dist/export/serializeColor.test.d.ts +0 -1
- package/dist/export/serializeColor.test.js +0 -54
- package/dist/export.d.ts +0 -1
- package/dist/export.js +0 -1
- package/dist/operators/emphasis.d.ts +0 -3
- package/dist/operators/emphasis.js +0 -113
- package/dist/operators/emphasis.test.d.ts +0 -1
- package/dist/operators/emphasis.test.js +0 -69
- package/dist/operators/index.d.ts +0 -3
- package/dist/operators/index.js +0 -2
- package/dist/operators/state.d.ts +0 -3
- package/dist/operators/state.js +0 -102
- package/dist/operators/state.test.d.ts +0 -1
- package/dist/operators/state.test.js +0 -48
- package/dist/operators/types.d.ts +0 -13
- package/dist/operators/types.js +0 -1
- package/dist/operators/utils.d.ts +0 -16
- package/dist/operators/utils.js +0 -23
- package/dist/presets/curves.d.ts +0 -28
- package/dist/presets/curves.js +0 -145
- package/dist/presets/index.d.ts +0 -2
- package/dist/presets/index.js +0 -1
- package/dist/presets/tokens/index.d.ts +0 -3
- package/dist/presets/tokens/index.js +0 -3
- package/dist/presets/tokens/minimal-ui.d.ts +0 -6
- package/dist/presets/tokens/minimal-ui.js +0 -53
- package/dist/presets/tokens/modern-ui.d.ts +0 -5
- package/dist/presets/tokens/modern-ui.js +0 -83
- package/dist/presets/tokens/presets.test.d.ts +0 -1
- package/dist/presets/tokens/presets.test.js +0 -31
- package/dist/presets/tokens/radixLike-ui.d.ts +0 -6
- package/dist/presets/tokens/radixLike-ui.js +0 -77
- package/dist/serialize/index.d.ts +0 -1
- package/dist/serialize/index.js +0 -1
- package/dist/serialize/normalizeOutput.d.ts +0 -6
- package/dist/serialize/normalizeOutput.js +0 -45
- package/dist/serialize/serializeColor.d.ts +0 -21
- package/dist/serialize/serializeColor.js +0 -178
- package/dist/serialize/serializeResolved.test.d.ts +0 -1
- package/dist/serialize/serializeResolved.test.js +0 -45
- package/dist/serialize.d.ts +0 -1
- package/dist/serialize.js +0 -1
- package/dist/utils/clamp.d.ts +0 -1
- package/dist/utils/clamp.js +0 -1
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/dist/utils/lerp.d.ts +0 -1
- package/dist/utils/lerp.js +0 -1
- package/dist/utils/parseColor.d.ts +0 -6
- package/dist/utils/parseColor.js +0 -67
- package/dist/utils/parseColor.test.d.ts +0 -1
- package/dist/utils/parseColor.test.js +0 -51
- package/dist/utils/smoothstep.d.ts +0 -1
- package/dist/utils/smoothstep.js +0 -5
- package/planning/phase-10-review.md +0 -550
- package/planning/phase-7-review.md +0 -411
- package/planning/phase-8-review.md +0 -669
- package/planning/phase-9-review.md +0 -564
- package/planning/roadmap-v0.3.md +0 -284
- package/planning/spec-serializer-v0.3.md +0 -324
- package/planning/spec-v0.3.md +0 -305
- package/src/cli/args.test.ts +0 -28
- package/src/cli/args.ts +0 -66
- package/src/cli/codegen/__snapshots__/tokens.test.ts.snap +0 -87
- package/src/cli/codegen/tokens.test.ts +0 -61
- package/src/cli/codegen/tokens.ts +0 -191
- package/src/cli/config.ts +0 -71
- package/src/cli/validate.test.ts +0 -49
- package/src/cli/validate.ts +0 -38
- package/src/cli.ts +0 -183
- package/src/contrast/apca.test.ts +0 -20
- package/src/contrast/apca.ts +0 -26
- package/src/contrast/index.ts +0 -4
- package/src/contrast/scoring.test.ts +0 -188
- package/src/contrast/scoring.ts +0 -48
- package/src/contrast/solver.test.ts +0 -147
- package/src/contrast/solver.ts +0 -235
- package/src/contrast/types.ts +0 -20
- package/src/contrast/utils.ts +0 -28
- package/src/contrast/wcag2.test.ts +0 -21
- package/src/contrast/wcag2.ts +0 -24
- package/src/core/createTheme.ts +0 -78
- package/src/core/dx-helpers.test.ts +0 -82
- package/src/core/index.ts +0 -7
- package/src/core/onSolid.test.ts +0 -146
- package/src/core/qa.v1.test.ts +0 -149
- package/src/core/resolve.test.ts +0 -99
- package/src/core/resolve.ts +0 -11
- package/src/core/resolveMany.ts +0 -22
- package/src/core/tokenRegistry.test.ts +0 -153
- package/src/core/tokenRegistry.ts +0 -114
- package/src/engine/applyOperators.ts +0 -32
- package/src/engine/context.ts +0 -8
- package/src/engine/gamut.test.ts +0 -30
- package/src/engine/gamut.ts +0 -144
- package/src/engine/generateScale.test.ts +0 -46
- package/src/engine/generateScale.ts +0 -48
- package/src/engine/index.ts +0 -8
- package/src/engine/normalize.test.ts +0 -222
- package/src/engine/normalize.ts +0 -550
- package/src/engine/onSolid.ts +0 -178
- package/src/engine/resolveBaseColor.test.ts +0 -117
- package/src/engine/resolveBaseColor.ts +0 -203
- package/src/export/__snapshots__/exportTheme.test.ts.snap +0 -74
- package/src/export/exportTheme.test.ts +0 -144
- package/src/export/exportTheme.ts +0 -251
- package/src/export/index.ts +0 -1
- package/src/export/serializeColor.test.ts +0 -73
- package/src/export/serializeColor.ts +0 -1
- package/src/export.ts +0 -1
- package/src/index.ts +0 -3
- package/src/operators/emphasis.test.ts +0 -85
- package/src/operators/emphasis.ts +0 -132
- package/src/operators/index.ts +0 -3
- package/src/operators/state.test.ts +0 -66
- package/src/operators/state.ts +0 -122
- package/src/operators/types.ts +0 -14
- package/src/operators/utils.ts +0 -44
- package/src/presets/curves.ts +0 -168
- package/src/presets/index.ts +0 -2
- package/src/presets/tokens/index.ts +0 -3
- package/src/presets/tokens/minimal-ui.ts +0 -55
- package/src/presets/tokens/modern-ui.ts +0 -85
- package/src/presets/tokens/presets.test.ts +0 -46
- package/src/presets/tokens/radixLike-ui.ts +0 -79
- package/src/serialize/index.ts +0 -1
- package/src/serialize/normalizeOutput.ts +0 -63
- package/src/serialize/serializeColor.ts +0 -260
- package/src/serialize/serializeResolved.test.ts +0 -57
- package/src/serialize.ts +0 -1
- package/src/types/index.ts +0 -207
- package/src/utils/clamp.ts +0 -2
- package/src/utils/index.ts +0 -1
- package/src/utils/lerp.ts +0 -1
- package/src/utils/parseColor.test.ts +0 -66
- package/src/utils/parseColor.ts +0 -87
- package/src/utils/smoothstep.ts +0 -6
- package/tsconfig.build.json +0 -11
- package/tsconfig.json +0 -15
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { createTheme } from "../core/createTheme.js";
|
|
4
|
-
import * as gamut from "../engine/gamut.js";
|
|
5
|
-
import { exportThemeCss, exportThemeJson, type ThemeTokens } from "./exportTheme.js";
|
|
6
|
-
|
|
7
|
-
type ExportFixture = {
|
|
8
|
-
resolve: ReturnType<typeof createTheme>["resolve"];
|
|
9
|
-
tokens: ThemeTokens;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const buildTheme = (): ExportFixture => {
|
|
13
|
-
const theme = createTheme({
|
|
14
|
-
seeds: {
|
|
15
|
-
light: { neutral: "#111827", accent: "#3d63dd" },
|
|
16
|
-
dark: { neutral: "#111827", accent: "#3d63dd" },
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const tokens: ThemeTokens = {
|
|
21
|
-
"text.primary": {
|
|
22
|
-
usage: "text",
|
|
23
|
-
surface: "surface",
|
|
24
|
-
emphasis: "default",
|
|
25
|
-
},
|
|
26
|
-
"bg.app": {
|
|
27
|
-
usage: "bg",
|
|
28
|
-
surface: "app",
|
|
29
|
-
},
|
|
30
|
-
"custom.alias": {
|
|
31
|
-
role: "text.primary",
|
|
32
|
-
usage: "text",
|
|
33
|
-
surface: "surface",
|
|
34
|
-
},
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
resolve: theme.resolve.bind(theme),
|
|
39
|
-
tokens,
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
describe("exportTheme", () => {
|
|
44
|
-
it("throws when theme tokens are missing", () => {
|
|
45
|
-
const theme = createTheme({
|
|
46
|
-
seeds: {
|
|
47
|
-
light: { neutral: "#111827", accent: "#3d63dd" },
|
|
48
|
-
dark: { neutral: "#111827", accent: "#3d63dd" },
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
expect(() => exportThemeCss(theme, {})).toThrow(/Theme tokens are required for export/i);
|
|
53
|
-
expect(() => exportThemeJson(theme, {})).toThrow(/Theme tokens are required for export/i);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it("exports deterministic CSS with @supports overrides", () => {
|
|
57
|
-
const theme = buildTheme();
|
|
58
|
-
const { css } = exportThemeCss(theme, theme.tokens, {
|
|
59
|
-
preferSpace: "oklch",
|
|
60
|
-
includeSpaces: ["p3", "oklch"],
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
expect(css).toMatchSnapshot();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("includes meta when requested and omits when not", () => {
|
|
67
|
-
const theme = buildTheme();
|
|
68
|
-
|
|
69
|
-
const withMeta = exportThemeJson(theme, theme.tokens, { includeMeta: true });
|
|
70
|
-
expect(withMeta.tokens.light["bg.app"].meta?.gamutMapping).toBe("preferP3ThenCompress");
|
|
71
|
-
expect(withMeta.meta).toEqual({
|
|
72
|
-
gamutMapping: "preferP3ThenCompress",
|
|
73
|
-
preferSpace: "oklch",
|
|
74
|
-
includeSpaces: [],
|
|
75
|
-
precision: { l: 1, c: 3, h: 1, alpha: 2 },
|
|
76
|
-
srgbFormat: "hex",
|
|
77
|
-
strict: false,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const withoutMeta = exportThemeJson(theme, theme.tokens, { includeMeta: false });
|
|
81
|
-
expect(withoutMeta.tokens.light["bg.app"].meta).toBeUndefined();
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("includes export meta for CSS when requested", () => {
|
|
85
|
-
const theme = buildTheme();
|
|
86
|
-
const { meta } = exportThemeCss(theme, theme.tokens, { includeMeta: true });
|
|
87
|
-
|
|
88
|
-
expect(meta).toEqual({
|
|
89
|
-
gamutMapping: "preferP3ThenCompress",
|
|
90
|
-
preferSpace: "oklch",
|
|
91
|
-
includeSpaces: [],
|
|
92
|
-
precision: { l: 1, c: 3, h: 1, alpha: 2 },
|
|
93
|
-
srgbFormat: "hex",
|
|
94
|
-
strict: false,
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("exports deterministic JSON with light/dark contexts", () => {
|
|
99
|
-
const theme = buildTheme();
|
|
100
|
-
const { tokens } = exportThemeJson(theme, theme.tokens, {
|
|
101
|
-
preferSpace: "oklch",
|
|
102
|
-
includeSpaces: ["srgb"],
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
expect(tokens).toMatchSnapshot();
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("keeps ordering stable regardless of token map order", () => {
|
|
109
|
-
const theme = buildTheme();
|
|
110
|
-
const shuffled: ThemeTokens = {
|
|
111
|
-
"custom.alias": theme.tokens["custom.alias"],
|
|
112
|
-
"bg.app": theme.tokens["bg.app"],
|
|
113
|
-
"text.primary": theme.tokens["text.primary"],
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const cssA = exportThemeCss(theme, theme.tokens, { preferSpace: "oklch" }).css;
|
|
117
|
-
const cssB = exportThemeCss(theme, shuffled, { preferSpace: "oklch" }).css;
|
|
118
|
-
const jsonA = exportThemeJson(theme, theme.tokens, { preferSpace: "oklch" }).tokens;
|
|
119
|
-
const jsonB = exportThemeJson(theme, shuffled, { preferSpace: "oklch" }).tokens;
|
|
120
|
-
|
|
121
|
-
expect(cssA).toBe(cssB);
|
|
122
|
-
expect(jsonA).toEqual(jsonB);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it("throws when strict preferred space cannot be serialized", () => {
|
|
126
|
-
const theme = buildTheme();
|
|
127
|
-
const original = gamut.toGamutRgb;
|
|
128
|
-
const spy = vi
|
|
129
|
-
.spyOn(gamut, "toGamutRgb")
|
|
130
|
-
.mockImplementation((color, target) => (target === "p3" ? null : original(color, target)));
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
expect(() =>
|
|
134
|
-
exportThemeCss(theme, theme.tokens, {
|
|
135
|
-
preferSpace: "p3",
|
|
136
|
-
includeSpaces: ["p3"],
|
|
137
|
-
strict: true,
|
|
138
|
-
}),
|
|
139
|
-
).toThrow(/Unable to serialize preferred space: p3/i);
|
|
140
|
-
} finally {
|
|
141
|
-
spy.mockRestore();
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
});
|
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import type { OkLchColor } from "../engine/generateScale.js";
|
|
2
|
-
import { serializeColorJson } from "../serialize/serializeColor.js";
|
|
3
|
-
import type { ColorContext, ColorMeta, ColorQuery, OutputOptions } from "../types/index.js";
|
|
4
|
-
import { serializeColor } from "./serializeColor.js";
|
|
5
|
-
|
|
6
|
-
export type ThemeToken = Omit<ColorQuery, "role" | "output"> & { role?: string };
|
|
7
|
-
export type ThemeTokens = Record<string, ThemeToken>;
|
|
8
|
-
|
|
9
|
-
export type ExportableTheme = {
|
|
10
|
-
resolve: (query: ColorQuery) => { oklch: OkLchColor };
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type TokenValue = {
|
|
14
|
-
value: string;
|
|
15
|
-
srgb?: string;
|
|
16
|
-
p3?: string;
|
|
17
|
-
oklch?: string;
|
|
18
|
-
alpha: number;
|
|
19
|
-
meta?: ColorMeta;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export type ExportMeta = {
|
|
23
|
-
gamutMapping: NonNullable<OutputOptions["gamutMapping"]>;
|
|
24
|
-
preferSpace: NonNullable<OutputOptions["preferSpace"]>;
|
|
25
|
-
includeSpaces: NonNullable<OutputOptions["includeSpaces"]>;
|
|
26
|
-
precision: Required<NonNullable<OutputOptions["precision"]>>;
|
|
27
|
-
srgbFormat: NonNullable<OutputOptions["srgbFormat"]>;
|
|
28
|
-
strict: boolean;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const DEFAULT_OUTPUT = {
|
|
32
|
-
preferSpace: "oklch" as const,
|
|
33
|
-
includeSpaces: [] as NonNullable<OutputOptions["includeSpaces"]>,
|
|
34
|
-
gamutMapping: "preferP3ThenCompress" as const,
|
|
35
|
-
precision: { l: 1, c: 3, h: 1, alpha: 2 },
|
|
36
|
-
srgbFormat: "hex" as const,
|
|
37
|
-
strict: false,
|
|
38
|
-
includeMeta: false,
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const normalizeExportOutput = (output?: OutputOptions) => ({
|
|
42
|
-
preferSpace: output?.preferSpace ?? DEFAULT_OUTPUT.preferSpace,
|
|
43
|
-
includeSpaces: output?.includeSpaces ?? DEFAULT_OUTPUT.includeSpaces,
|
|
44
|
-
gamutMapping: output?.gamutMapping ?? DEFAULT_OUTPUT.gamutMapping,
|
|
45
|
-
precision: {
|
|
46
|
-
...DEFAULT_OUTPUT.precision,
|
|
47
|
-
...output?.precision,
|
|
48
|
-
},
|
|
49
|
-
srgbFormat: output?.srgbFormat ?? DEFAULT_OUTPUT.srgbFormat,
|
|
50
|
-
strict: output?.strict ?? DEFAULT_OUTPUT.strict,
|
|
51
|
-
includeMeta: output?.includeMeta ?? DEFAULT_OUTPUT.includeMeta,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const buildExportMeta = (output: ReturnType<typeof normalizeExportOutput>): ExportMeta => ({
|
|
55
|
-
gamutMapping: output.gamutMapping,
|
|
56
|
-
preferSpace: output.preferSpace,
|
|
57
|
-
includeSpaces: output.includeSpaces,
|
|
58
|
-
precision: output.precision,
|
|
59
|
-
srgbFormat: output.srgbFormat,
|
|
60
|
-
strict: output.strict,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const getTokenEntries = (tokens: ThemeTokens) => {
|
|
64
|
-
if (!tokens || Object.keys(tokens).length === 0) {
|
|
65
|
-
throw new Error("Theme tokens are required for export");
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return Object.entries(tokens).sort(([a], [b]) => a.localeCompare(b));
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const getTokenRole = (name: string, token: ThemeToken) => token.role ?? name;
|
|
72
|
-
|
|
73
|
-
const buildTokenQuery = (name: string, token: ThemeToken, context?: ColorContext): ColorQuery => {
|
|
74
|
-
const { role: _role, ...rest } = token;
|
|
75
|
-
return {
|
|
76
|
-
...rest,
|
|
77
|
-
role: getTokenRole(name, token),
|
|
78
|
-
...(context ? { context } : {}),
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const buildTokenMeta = (name: string, token: ThemeToken, context?: ColorContext): ColorMeta => ({
|
|
83
|
-
role: getTokenRole(name, token),
|
|
84
|
-
...(token.variant ? { variant: token.variant } : {}),
|
|
85
|
-
...(token.usage ? { usage: token.usage } : {}),
|
|
86
|
-
...(context ? { context } : token.context ? { context: token.context } : {}),
|
|
87
|
-
...(token.surface ? { surface: token.surface } : {}),
|
|
88
|
-
...(token.state ? { state: token.state } : {}),
|
|
89
|
-
...(token.emphasis ? { emphasis: token.emphasis } : {}),
|
|
90
|
-
...(token.on ? { on: token.on } : {}),
|
|
91
|
-
...(token.contrast ? { contrast: token.contrast } : {}),
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const buildTokenValue = (serialized: TokenValue): TokenValue => ({
|
|
95
|
-
value: serialized.value,
|
|
96
|
-
...(serialized.srgb !== undefined ? { srgb: serialized.srgb } : {}),
|
|
97
|
-
...(serialized.p3 !== undefined ? { p3: serialized.p3 } : {}),
|
|
98
|
-
...(serialized.oklch !== undefined ? { oklch: serialized.oklch } : {}),
|
|
99
|
-
alpha: serialized.alpha,
|
|
100
|
-
...(serialized.meta !== undefined ? { meta: serialized.meta } : {}),
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
const toCssVarName = (name: string) => name.replace(/\./g, "-");
|
|
104
|
-
|
|
105
|
-
const indent = (lines: string[], prefix: string) =>
|
|
106
|
-
lines.length === 0 ? "" : lines.map((line) => `${prefix}${line}`).join("\n");
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Export CSS variables for a token map with progressive fallbacks.
|
|
110
|
-
*
|
|
111
|
-
* - Base output uses sRGB for maximum compatibility.
|
|
112
|
-
* - @supports blocks override the main token value with OKLCH and/or P3 as requested.
|
|
113
|
-
*/
|
|
114
|
-
export const exportThemeCss = (
|
|
115
|
-
theme: ExportableTheme,
|
|
116
|
-
tokens: ThemeTokens,
|
|
117
|
-
output?: OutputOptions,
|
|
118
|
-
) => {
|
|
119
|
-
const normalized = normalizeExportOutput(output);
|
|
120
|
-
const entries = getTokenEntries(tokens);
|
|
121
|
-
const baseDecl: string[] = [];
|
|
122
|
-
const oklchDecl: string[] = [];
|
|
123
|
-
const p3Decl: string[] = [];
|
|
124
|
-
|
|
125
|
-
const needsOklch =
|
|
126
|
-
normalized.preferSpace === "oklch" || normalized.includeSpaces.includes("oklch");
|
|
127
|
-
const needsP3 = normalized.preferSpace === "p3" || normalized.includeSpaces.includes("p3");
|
|
128
|
-
|
|
129
|
-
for (const [name, token] of entries) {
|
|
130
|
-
const query = buildTokenQuery(name, token);
|
|
131
|
-
const resolved = theme.resolve(query);
|
|
132
|
-
const meta = normalized.includeMeta ? buildTokenMeta(name, token) : undefined;
|
|
133
|
-
const baseName = toCssVarName(name);
|
|
134
|
-
|
|
135
|
-
// Base fallback MUST be sRGB (no silent OKLCH fallback).
|
|
136
|
-
const baseSrgb = serializeColor(
|
|
137
|
-
resolved.oklch,
|
|
138
|
-
{ ...normalized, preferSpace: "srgb", includeSpaces: [], strict: normalized.strict },
|
|
139
|
-
meta,
|
|
140
|
-
).value;
|
|
141
|
-
|
|
142
|
-
baseDecl.push(`--pk-${baseName}: ${baseSrgb};`);
|
|
143
|
-
if (normalized.includeSpaces.includes("srgb")) {
|
|
144
|
-
baseDecl.push(`--pk-${baseName}-srgb: ${baseSrgb};`);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (needsOklch) {
|
|
148
|
-
const oklchValue = serializeColor(
|
|
149
|
-
resolved.oklch,
|
|
150
|
-
{ ...normalized, preferSpace: "oklch", includeSpaces: [] },
|
|
151
|
-
meta,
|
|
152
|
-
).value;
|
|
153
|
-
|
|
154
|
-
if (normalized.preferSpace === "oklch") {
|
|
155
|
-
oklchDecl.push(`--pk-${baseName}: ${oklchValue};`);
|
|
156
|
-
}
|
|
157
|
-
if (normalized.includeSpaces.includes("oklch")) {
|
|
158
|
-
oklchDecl.push(`--pk-${baseName}-oklch: ${oklchValue};`);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (needsP3) {
|
|
163
|
-
const p3Text = serializeColor(
|
|
164
|
-
resolved.oklch,
|
|
165
|
-
{ ...normalized, preferSpace: "oklch", includeSpaces: ["p3"] },
|
|
166
|
-
meta,
|
|
167
|
-
).p3;
|
|
168
|
-
|
|
169
|
-
if (normalized.preferSpace === "p3") {
|
|
170
|
-
if (!p3Text) {
|
|
171
|
-
if (normalized.strict) {
|
|
172
|
-
throw new Error("Unable to serialize preferred space: p3");
|
|
173
|
-
}
|
|
174
|
-
} else {
|
|
175
|
-
p3Decl.push(`--pk-${baseName}: ${p3Text};`);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (normalized.includeSpaces.includes("p3") && p3Text) {
|
|
180
|
-
p3Decl.push(`--pk-${baseName}-p3: ${p3Text};`);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const blocks: string[] = [];
|
|
186
|
-
blocks.push(`:root {\n${indent(baseDecl, " ")}\n}\n`);
|
|
187
|
-
|
|
188
|
-
if (oklchDecl.length > 0) {
|
|
189
|
-
blocks.push(
|
|
190
|
-
`@supports (color: oklch(0% 0 0)) {\n :root {\n${indent(oklchDecl, " ")}\n }\n}\n`,
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
if (p3Decl.length > 0) {
|
|
194
|
-
blocks.push(
|
|
195
|
-
`@supports (color: color(display-p3 1 1 1)) {\n :root {\n${indent(p3Decl, " ")}\n }\n}\n`,
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return {
|
|
200
|
-
css: `${blocks.join("\n").trimEnd()}\n`,
|
|
201
|
-
meta: normalized.includeMeta ? buildExportMeta(normalized) : undefined,
|
|
202
|
-
};
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Export JSON tokens for both light and dark contexts.
|
|
207
|
-
*/
|
|
208
|
-
export const exportThemeJson = (
|
|
209
|
-
theme: ExportableTheme,
|
|
210
|
-
tokens: ThemeTokens,
|
|
211
|
-
output?: OutputOptions,
|
|
212
|
-
) => {
|
|
213
|
-
const normalized = normalizeExportOutput(output);
|
|
214
|
-
const entries = getTokenEntries(tokens);
|
|
215
|
-
const lightTokens: Record<string, TokenValue> = {};
|
|
216
|
-
const darkTokens: Record<string, TokenValue> = {};
|
|
217
|
-
|
|
218
|
-
const contexts: ColorContext[] = ["light", "dark"];
|
|
219
|
-
|
|
220
|
-
for (const [name, token] of entries) {
|
|
221
|
-
for (const context of contexts) {
|
|
222
|
-
const query = buildTokenQuery(name, token, context);
|
|
223
|
-
const resolved = theme.resolve(query);
|
|
224
|
-
const meta = normalized.includeMeta ? buildTokenMeta(name, token, context) : undefined;
|
|
225
|
-
const serialized = serializeColorJson(resolved.oklch, normalized, meta);
|
|
226
|
-
|
|
227
|
-
const entry = buildTokenValue({
|
|
228
|
-
value: serialized.value,
|
|
229
|
-
srgb: serialized.srgb,
|
|
230
|
-
p3: serialized.p3,
|
|
231
|
-
oklch: serialized.oklch,
|
|
232
|
-
alpha: serialized.alpha,
|
|
233
|
-
meta: serialized.meta,
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
if (context === "light") {
|
|
237
|
-
lightTokens[name] = entry;
|
|
238
|
-
} else {
|
|
239
|
-
darkTokens[name] = entry;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return {
|
|
245
|
-
tokens: {
|
|
246
|
-
light: lightTokens,
|
|
247
|
-
dark: darkTokens,
|
|
248
|
-
},
|
|
249
|
-
meta: normalized.includeMeta ? buildExportMeta(normalized) : undefined,
|
|
250
|
-
};
|
|
251
|
-
};
|
package/src/export/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./exportTheme.js";
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { serializeColor } from "./serializeColor.js";
|
|
4
|
-
import { serializeColorJson } from "../serialize/serializeColor.js";
|
|
5
|
-
import * as gamut from "../engine/gamut.js";
|
|
6
|
-
|
|
7
|
-
describe("serializeColor", () => {
|
|
8
|
-
it("serializes CSS with precision and includeSpaces", () => {
|
|
9
|
-
const color = { l: 60.12, c: 0.2345, h: 40.49, alpha: 0.5 };
|
|
10
|
-
const result = serializeColor(color, {
|
|
11
|
-
preferSpace: "oklch",
|
|
12
|
-
includeSpaces: ["srgb", "p3", "oklch"],
|
|
13
|
-
gamutMapping: "compressChroma",
|
|
14
|
-
precision: { l: 1, c: 2, h: 0, alpha: 2 },
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
expect(result.value).toBe("oklch(60.1% 0.23 40 / 0.5)");
|
|
18
|
-
expect(result.oklch).toBe("oklch(60.1% 0.23 40 / 0.5)");
|
|
19
|
-
expect(result.srgb).toMatch(/^#[0-9a-f]{8}$/i);
|
|
20
|
-
expect(result.p3?.startsWith("color(display-p3 ")).toBe(true);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("serializes JSON with includeSpaces and rounding", () => {
|
|
24
|
-
const color = { l: 60.12, c: 0.2345, h: 40.49, alpha: 0.5 };
|
|
25
|
-
const result = serializeColorJson(color, {
|
|
26
|
-
preferSpace: "p3",
|
|
27
|
-
includeSpaces: ["srgb", "oklch"],
|
|
28
|
-
gamutMapping: "preferP3ThenCompress",
|
|
29
|
-
precision: { l: 1, c: 2, h: 0, alpha: 2 },
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
expect(result.value.startsWith("color(display-p3 ")).toBe(true);
|
|
33
|
-
expect(result.oklch).toBeDefined();
|
|
34
|
-
expect(result.srgb).toBeDefined();
|
|
35
|
-
expect(result.oklch).toBe("oklch(60.1% 0.23 40 / 0.5)");
|
|
36
|
-
expect(result.alpha).toBe(0.5);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("clamps alpha to [0..1] in CSS and JSON outputs", () => {
|
|
40
|
-
const colorHi = { l: 60, c: 0.2, h: 40, alpha: 2 };
|
|
41
|
-
const cssHi = serializeColor(colorHi, { preferSpace: "oklch", precision: { alpha: 2 } });
|
|
42
|
-
expect(cssHi.alpha).toBe(1);
|
|
43
|
-
expect(cssHi.value).toMatch(/^oklch\(/);
|
|
44
|
-
|
|
45
|
-
const colorLo = { l: 60, c: 0.2, h: 40, alpha: -1 };
|
|
46
|
-
const jsonLo = serializeColorJson(colorLo, { preferSpace: "oklch", precision: { alpha: 2 } });
|
|
47
|
-
expect(jsonLo.alpha).toBe(0);
|
|
48
|
-
expect(jsonLo.value).toMatch(/\/\s*0(\.0+)?\)/);
|
|
49
|
-
expect(jsonLo.oklch).toMatch(/\/\s*0(\.0+)?\)/);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("throws in strict mode when preferred space cannot be serialized", () => {
|
|
53
|
-
const spy = vi.spyOn(gamut, "toGamutRgb").mockReturnValue(null);
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
expect(() =>
|
|
57
|
-
serializeColor(
|
|
58
|
-
{ l: 60, c: 0.2, h: 40, alpha: 1 },
|
|
59
|
-
{ preferSpace: "srgb", includeSpaces: ["srgb"], strict: true },
|
|
60
|
-
),
|
|
61
|
-
).toThrow(/Unable to serialize preferred space: srgb/i);
|
|
62
|
-
|
|
63
|
-
expect(() =>
|
|
64
|
-
serializeColorJson(
|
|
65
|
-
{ l: 60, c: 0.2, h: 40, alpha: 1 },
|
|
66
|
-
{ preferSpace: "p3", includeSpaces: ["p3"], strict: true },
|
|
67
|
-
),
|
|
68
|
-
).toThrow(/Unable to serialize preferred space: p3/i);
|
|
69
|
-
} finally {
|
|
70
|
-
spy.mockRestore();
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { serializeColor } from "../serialize/serializeColor.js";
|
package/src/export.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./export/index.js";
|
package/src/index.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { applyEmphasisOperator } from "./emphasis.js";
|
|
4
|
-
import { getStepLightness } from "./utils.js";
|
|
5
|
-
|
|
6
|
-
const base = {
|
|
7
|
-
oklch: { l: 62, c: 0.06, h: 215 },
|
|
8
|
-
context: "light" as const,
|
|
9
|
-
surface: "surface" as const,
|
|
10
|
-
usage: "text" as const,
|
|
11
|
-
state: "default" as const,
|
|
12
|
-
emphasis: "default" as const,
|
|
13
|
-
preset: "modern" as const,
|
|
14
|
-
step: 11,
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
describe("applyEmphasisOperator", () => {
|
|
18
|
-
it("muted and subtle reduce chroma predictably", () => {
|
|
19
|
-
const muted = applyEmphasisOperator({ ...base, emphasis: "muted" });
|
|
20
|
-
const subtle = applyEmphasisOperator({ ...base, emphasis: "subtle" });
|
|
21
|
-
|
|
22
|
-
expect(muted.c).toBeLessThan(base.oklch.c);
|
|
23
|
-
expect(subtle.c).toBeLessThan(base.oklch.c);
|
|
24
|
-
expect(subtle.c).toBeGreaterThan(muted.c);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("strong increases chroma", () => {
|
|
28
|
-
const strong = applyEmphasisOperator({ ...base, emphasis: "strong" });
|
|
29
|
-
|
|
30
|
-
expect(strong.c).toBeGreaterThan(base.oklch.c);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("strong shifts text lightness by context", () => {
|
|
34
|
-
const strongLight = applyEmphasisOperator({ ...base, emphasis: "strong", usage: "text" });
|
|
35
|
-
const strongDark = applyEmphasisOperator({
|
|
36
|
-
...base,
|
|
37
|
-
emphasis: "strong",
|
|
38
|
-
usage: "text",
|
|
39
|
-
context: "dark",
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
expect(strongLight.l).toBeLessThan(base.oklch.l);
|
|
43
|
-
expect(strongDark.l).toBeGreaterThan(base.oklch.l);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("does not change hue", () => {
|
|
47
|
-
const muted = applyEmphasisOperator({ ...base, emphasis: "muted" });
|
|
48
|
-
|
|
49
|
-
expect(muted.h).toBeCloseTo(base.oklch.h, 6);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("inverted leaves backgrounds unchanged", () => {
|
|
53
|
-
const invertedBg = applyEmphasisOperator({
|
|
54
|
-
...base,
|
|
55
|
-
emphasis: "inverted",
|
|
56
|
-
usage: "bg",
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
expect(invertedBg.l).toBeCloseTo(base.oklch.l, 6);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it("inverted leaves non-text usage unchanged", () => {
|
|
63
|
-
const invertedBorder = applyEmphasisOperator({
|
|
64
|
-
...base,
|
|
65
|
-
emphasis: "inverted",
|
|
66
|
-
usage: "border",
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
expect(invertedBorder.l).toBeCloseTo(base.oklch.l, 6);
|
|
70
|
-
expect(invertedBorder.c).toBeCloseTo(base.oklch.c, 6);
|
|
71
|
-
expect(invertedBorder.h).toBeCloseTo(base.oklch.h, 6);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("inverted moves text toward the target step without changing hue", () => {
|
|
75
|
-
const targetL = getStepLightness(base.preset, base.surface, base.context, 12);
|
|
76
|
-
const inverted = applyEmphasisOperator({
|
|
77
|
-
...base,
|
|
78
|
-
emphasis: "inverted",
|
|
79
|
-
usage: "text",
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
expect(Math.abs(inverted.l - targetL)).toBeLessThan(Math.abs(base.oklch.l - targetL));
|
|
83
|
-
expect(inverted.h).toBeCloseTo(base.oklch.h, 6);
|
|
84
|
-
});
|
|
85
|
-
});
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import type { OkLchColor } from "../engine/generateScale.js";
|
|
2
|
-
import { clamp } from "../utils/clamp.js";
|
|
3
|
-
import type { OperatorInput } from "./types.js";
|
|
4
|
-
import { getNeutralL, getStepLightness, getSurfaceRange } from "./utils.js";
|
|
5
|
-
|
|
6
|
-
type EmphasisTuning = {
|
|
7
|
-
mutedChroma: number;
|
|
8
|
-
subtleChroma: number;
|
|
9
|
-
strongChroma: number;
|
|
10
|
-
mutedNeutralPull: number;
|
|
11
|
-
subtleNeutralPull: number;
|
|
12
|
-
strongDelta: number;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const EMPHASIS_TUNING: Record<OperatorInput["surface"], EmphasisTuning> = {
|
|
16
|
-
app: {
|
|
17
|
-
mutedChroma: 0.55,
|
|
18
|
-
subtleChroma: 0.75,
|
|
19
|
-
strongChroma: 1.1,
|
|
20
|
-
mutedNeutralPull: 0.45,
|
|
21
|
-
subtleNeutralPull: 0.2,
|
|
22
|
-
strongDelta: 2,
|
|
23
|
-
},
|
|
24
|
-
surface: {
|
|
25
|
-
mutedChroma: 0.55,
|
|
26
|
-
subtleChroma: 0.75,
|
|
27
|
-
strongChroma: 1.1,
|
|
28
|
-
mutedNeutralPull: 0.45,
|
|
29
|
-
subtleNeutralPull: 0.2,
|
|
30
|
-
strongDelta: 2.5,
|
|
31
|
-
},
|
|
32
|
-
subtle: {
|
|
33
|
-
mutedChroma: 0.55,
|
|
34
|
-
subtleChroma: 0.75,
|
|
35
|
-
strongChroma: 1.1,
|
|
36
|
-
mutedNeutralPull: 0.5,
|
|
37
|
-
subtleNeutralPull: 0.25,
|
|
38
|
-
strongDelta: 3,
|
|
39
|
-
},
|
|
40
|
-
solid: {
|
|
41
|
-
mutedChroma: 0.55,
|
|
42
|
-
subtleChroma: 0.75,
|
|
43
|
-
strongChroma: 1.1,
|
|
44
|
-
mutedNeutralPull: 0.5,
|
|
45
|
-
subtleNeutralPull: 0.25,
|
|
46
|
-
strongDelta: 4,
|
|
47
|
-
},
|
|
48
|
-
overlay: {
|
|
49
|
-
mutedChroma: 0.55,
|
|
50
|
-
subtleChroma: 0.75,
|
|
51
|
-
strongChroma: 1.1,
|
|
52
|
-
mutedNeutralPull: 0.5,
|
|
53
|
-
subtleNeutralPull: 0.25,
|
|
54
|
-
strongDelta: 3,
|
|
55
|
-
},
|
|
56
|
-
data: {
|
|
57
|
-
mutedChroma: 0.55,
|
|
58
|
-
subtleChroma: 0.75,
|
|
59
|
-
strongChroma: 1.1,
|
|
60
|
-
mutedNeutralPull: 0.5,
|
|
61
|
-
subtleNeutralPull: 0.25,
|
|
62
|
-
strongDelta: 4,
|
|
63
|
-
},
|
|
64
|
-
transparent: {
|
|
65
|
-
mutedChroma: 0.55,
|
|
66
|
-
subtleChroma: 0.75,
|
|
67
|
-
strongChroma: 1.1,
|
|
68
|
-
mutedNeutralPull: 0.45,
|
|
69
|
-
subtleNeutralPull: 0.2,
|
|
70
|
-
strongDelta: 2,
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const INVERTED_LERP_FACTOR = 0.8;
|
|
75
|
-
|
|
76
|
-
const clampOkLch = (color: OkLchColor, maxChroma: number): OkLchColor => ({
|
|
77
|
-
...color,
|
|
78
|
-
l: clamp(color.l, 0, 100),
|
|
79
|
-
c: clamp(color.c, 0, maxChroma),
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
export const applyEmphasisOperator = (input: OperatorInput): OkLchColor => {
|
|
83
|
-
const { emphasis, context, surface, usage } = input;
|
|
84
|
-
const tuning = EMPHASIS_TUNING[surface];
|
|
85
|
-
const range = getSurfaceRange(input.preset, surface, context);
|
|
86
|
-
const neutralL = getNeutralL(input.preset, surface, context);
|
|
87
|
-
|
|
88
|
-
if (emphasis === "default") {
|
|
89
|
-
return input.oklch;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const next: OkLchColor = { ...input.oklch };
|
|
93
|
-
|
|
94
|
-
switch (emphasis) {
|
|
95
|
-
case "muted":
|
|
96
|
-
next.c *= tuning.mutedChroma;
|
|
97
|
-
next.l += (neutralL - next.l) * tuning.mutedNeutralPull;
|
|
98
|
-
break;
|
|
99
|
-
case "subtle":
|
|
100
|
-
next.c *= tuning.subtleChroma;
|
|
101
|
-
next.l += (neutralL - next.l) * tuning.subtleNeutralPull;
|
|
102
|
-
break;
|
|
103
|
-
case "strong": {
|
|
104
|
-
const diff = next.l - neutralL;
|
|
105
|
-
let direction = diff === 0 ? (context === "light" ? -1 : 1) : Math.sign(diff);
|
|
106
|
-
|
|
107
|
-
if (usage === "text" || usage === "icon") {
|
|
108
|
-
direction = context === "light" ? -1 : 1;
|
|
109
|
-
} else if (context === "light" && direction > 0) {
|
|
110
|
-
direction = -1;
|
|
111
|
-
} else if (context === "dark" && direction < 0) {
|
|
112
|
-
direction = 1;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
next.c *= tuning.strongChroma;
|
|
116
|
-
next.l += direction * tuning.strongDelta;
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
case "inverted": {
|
|
120
|
-
if (usage === "text" || usage === "icon") {
|
|
121
|
-
const targetStep = input.step === 12 ? 11 : 12;
|
|
122
|
-
const targetL = getStepLightness(input.preset, surface, context, targetStep);
|
|
123
|
-
next.l += (targetL - next.l) * INVERTED_LERP_FACTOR;
|
|
124
|
-
}
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
default:
|
|
128
|
-
return input.oklch;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return clampOkLch(next, range.cMax);
|
|
132
|
-
};
|