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