@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.
Files changed (312) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +80 -87
  3. package/dist/contrast/contrast.d.ts +16 -0
  4. package/dist/contrast/contrast.js +102 -0
  5. package/dist/core/intent-registry.d.ts +11 -0
  6. package/dist/core/intent-registry.js +70 -0
  7. package/dist/core/oklch.d.ts +16 -0
  8. package/dist/core/oklch.js +56 -0
  9. package/dist/create-palette-kit.d.ts +9 -0
  10. package/dist/create-palette-kit.js +67 -0
  11. package/dist/engine/context/context.d.ts +13 -0
  12. package/dist/engine/context/context.js +37 -0
  13. package/dist/engine/level/curves.d.ts +17 -0
  14. package/dist/engine/level/curves.js +49 -0
  15. package/dist/engine/level/level.d.ts +4 -0
  16. package/dist/engine/level/level.js +13 -0
  17. package/dist/engine/relation/relation.d.ts +105 -0
  18. package/dist/engine/relation/relation.js +137 -0
  19. package/dist/engine/resolve/resolve.d.ts +36 -0
  20. package/dist/engine/resolve/resolve.js +116 -0
  21. package/dist/engine/state/state.d.ts +46 -0
  22. package/dist/engine/state/state.js +68 -0
  23. package/dist/engine/usage/fill.d.ts +9 -0
  24. package/dist/engine/usage/fill.js +9 -0
  25. package/dist/engine/usage/lines.d.ts +9 -0
  26. package/dist/engine/usage/lines.js +9 -0
  27. package/dist/engine/usage/overlays.d.ts +9 -0
  28. package/dist/engine/usage/overlays.js +9 -0
  29. package/dist/engine/usage/strategy.d.ts +56 -0
  30. package/dist/engine/usage/strategy.js +30 -0
  31. package/dist/engine/usage/visualVocabulary.d.ts +9 -0
  32. package/dist/engine/usage/visualVocabulary.js +9 -0
  33. package/dist/export/serialize.d.ts +14 -0
  34. package/dist/export/serialize.js +89 -0
  35. package/dist/export/types.d.ts +37 -0
  36. package/dist/export/types.js +31 -0
  37. package/dist/index.d.ts +3 -3
  38. package/dist/index.js +2 -2
  39. package/dist/operators/convert.d.ts +32 -0
  40. package/dist/operators/convert.js +80 -0
  41. package/dist/presets/presets.d.ts +95 -0
  42. package/dist/presets/presets.js +308 -0
  43. package/dist/types/index.d.ts +111 -187
  44. package/dist/utils/errors/errors.d.ts +17 -0
  45. package/dist/utils/errors/errors.js +22 -0
  46. package/docs/API.md +167 -0
  47. package/docs/Alpha.md +14 -0
  48. package/docs/Architecture.md +56 -0
  49. package/docs/CLI.md +22 -0
  50. package/docs/Concepts.md +73 -0
  51. package/docs/Config.md +144 -0
  52. package/docs/Diagnostics.md +22 -0
  53. package/docs/Exporters.md +33 -0
  54. package/docs/FAQ.md +59 -0
  55. package/docs/Migration.md +61 -0
  56. package/docs/Overlays.md +33 -0
  57. package/docs/README.md +60 -0
  58. package/docs/Text.md +41 -0
  59. package/docs/Tokens.md +42 -0
  60. package/docs/Usage-JSON.md +39 -0
  61. package/docs/Usage-ReactNative.md +63 -0
  62. package/docs/Usage-Web.md +66 -0
  63. package/docs/Validation.md +97 -0
  64. package/docs/Why.md +37 -0
  65. package/docs/_api-surface.md +53 -0
  66. package/docs/snippets/serialize-oklch.md +9 -0
  67. package/docs/spec.md +98 -0
  68. package/package.json +74 -59
  69. package/.codex/skills/color-pipeline-implementer/SKILL.md +0 -23
  70. package/.codex/skills/commit-message-crafter/SKILL.md +0 -63
  71. package/.codex/skills/commit-message-crafter/references/benchmarks.md +0 -20
  72. package/.codex/skills/contrast-solver-helper/SKILL.md +0 -20
  73. package/.codex/skills/exporters-builder/SKILL.md +0 -20
  74. package/.codex/skills/markdownlint-writer/SKILL.md +0 -32
  75. package/.codex/skills/phase-implementation-runbook/SKILL.md +0 -92
  76. package/.codex/skills/type-contract-auditor/SKILL.md +0 -21
  77. package/.github/skills/review-guide/SKILL.md +0 -23
  78. package/.github/skills/review-guide/references/review-guide-v0.3.md +0 -629
  79. package/.markdownlint.json +0 -4
  80. package/AGENTS.md +0 -16
  81. package/biome.json +0 -43
  82. package/dist/cli/args.d.ts +0 -12
  83. package/dist/cli/args.js +0 -56
  84. package/dist/cli/args.test.d.ts +0 -1
  85. package/dist/cli/args.test.js +0 -22
  86. package/dist/cli/codegen/__snapshots__/tokens.test.js.snap +0 -87
  87. package/dist/cli/codegen/tokens.d.ts +0 -12
  88. package/dist/cli/codegen/tokens.js +0 -139
  89. package/dist/cli/codegen/tokens.test.d.ts +0 -1
  90. package/dist/cli/codegen/tokens.test.js +0 -51
  91. package/dist/cli/config.d.ts +0 -40
  92. package/dist/cli/config.js +0 -34
  93. package/dist/cli/validate.d.ts +0 -2
  94. package/dist/cli/validate.js +0 -33
  95. package/dist/cli/validate.test.d.ts +0 -1
  96. package/dist/cli/validate.test.js +0 -40
  97. package/dist/cli.d.ts +0 -2
  98. package/dist/cli.js +0 -148
  99. package/dist/contrast/apca.d.ts +0 -2
  100. package/dist/contrast/apca.js +0 -15
  101. package/dist/contrast/apca.test.d.ts +0 -1
  102. package/dist/contrast/apca.test.js +0 -16
  103. package/dist/contrast/index.d.ts +0 -4
  104. package/dist/contrast/index.js +0 -4
  105. package/dist/contrast/scoring.d.ts +0 -4
  106. package/dist/contrast/scoring.js +0 -31
  107. package/dist/contrast/scoring.test.d.ts +0 -1
  108. package/dist/contrast/scoring.test.js +0 -148
  109. package/dist/contrast/solver.d.ts +0 -13
  110. package/dist/contrast/solver.js +0 -170
  111. package/dist/contrast/solver.test.d.ts +0 -1
  112. package/dist/contrast/solver.test.js +0 -75
  113. package/dist/contrast/types.d.ts +0 -17
  114. package/dist/contrast/types.js +0 -1
  115. package/dist/contrast/utils.d.ts +0 -4
  116. package/dist/contrast/utils.js +0 -18
  117. package/dist/contrast/wcag2.d.ts +0 -3
  118. package/dist/contrast/wcag2.js +0 -19
  119. package/dist/contrast/wcag2.test.d.ts +0 -1
  120. package/dist/contrast/wcag2.test.js +0 -17
  121. package/dist/core/createTheme.d.ts +0 -35
  122. package/dist/core/createTheme.js +0 -24
  123. package/dist/core/dx-helpers.test.d.ts +0 -1
  124. package/dist/core/dx-helpers.test.js +0 -61
  125. package/dist/core/index.d.ts +0 -2
  126. package/dist/core/index.js +0 -2
  127. package/dist/core/onSolid.test.d.ts +0 -1
  128. package/dist/core/onSolid.test.js +0 -118
  129. package/dist/core/qa.v1.test.d.ts +0 -1
  130. package/dist/core/qa.v1.test.js +0 -112
  131. package/dist/core/resolve.d.ts +0 -3
  132. package/dist/core/resolve.js +0 -8
  133. package/dist/core/resolve.test.d.ts +0 -1
  134. package/dist/core/resolve.test.js +0 -89
  135. package/dist/core/resolveMany.d.ts +0 -8
  136. package/dist/core/resolveMany.js +0 -17
  137. package/dist/core/tokenRegistry.d.ts +0 -23
  138. package/dist/core/tokenRegistry.js +0 -83
  139. package/dist/core/tokenRegistry.test.d.ts +0 -1
  140. package/dist/core/tokenRegistry.test.js +0 -133
  141. package/dist/engine/applyOperators.d.ts +0 -3
  142. package/dist/engine/applyOperators.js +0 -23
  143. package/dist/engine/context.d.ts +0 -4
  144. package/dist/engine/context.js +0 -1
  145. package/dist/engine/gamut.d.ts +0 -13
  146. package/dist/engine/gamut.js +0 -101
  147. package/dist/engine/gamut.test.d.ts +0 -1
  148. package/dist/engine/gamut.test.js +0 -23
  149. package/dist/engine/generateScale.d.ts +0 -15
  150. package/dist/engine/generateScale.js +0 -29
  151. package/dist/engine/generateScale.test.d.ts +0 -1
  152. package/dist/engine/generateScale.test.js +0 -32
  153. package/dist/engine/index.d.ts +0 -8
  154. package/dist/engine/index.js +0 -4
  155. package/dist/engine/normalize.d.ts +0 -43
  156. package/dist/engine/normalize.js +0 -403
  157. package/dist/engine/normalize.test.d.ts +0 -1
  158. package/dist/engine/normalize.test.js +0 -136
  159. package/dist/engine/onSolid.d.ts +0 -3
  160. package/dist/engine/onSolid.js +0 -110
  161. package/dist/engine/resolveBaseColor.d.ts +0 -25
  162. package/dist/engine/resolveBaseColor.js +0 -127
  163. package/dist/engine/resolveBaseColor.test.d.ts +0 -1
  164. package/dist/engine/resolveBaseColor.test.js +0 -97
  165. package/dist/export/__snapshots__/exportTheme.test.js.snap +0 -74
  166. package/dist/export/exportTheme.d.ts +0 -47
  167. package/dist/export/exportTheme.js +0 -170
  168. package/dist/export/exportTheme.test.d.ts +0 -1
  169. package/dist/export/exportTheme.test.js +0 -118
  170. package/dist/export/index.d.ts +0 -1
  171. package/dist/export/index.js +0 -1
  172. package/dist/export/serializeColor.d.ts +0 -1
  173. package/dist/export/serializeColor.js +0 -1
  174. package/dist/export/serializeColor.test.d.ts +0 -1
  175. package/dist/export/serializeColor.test.js +0 -54
  176. package/dist/export.d.ts +0 -1
  177. package/dist/export.js +0 -1
  178. package/dist/operators/emphasis.d.ts +0 -3
  179. package/dist/operators/emphasis.js +0 -113
  180. package/dist/operators/emphasis.test.d.ts +0 -1
  181. package/dist/operators/emphasis.test.js +0 -69
  182. package/dist/operators/index.d.ts +0 -3
  183. package/dist/operators/index.js +0 -2
  184. package/dist/operators/state.d.ts +0 -3
  185. package/dist/operators/state.js +0 -102
  186. package/dist/operators/state.test.d.ts +0 -1
  187. package/dist/operators/state.test.js +0 -48
  188. package/dist/operators/types.d.ts +0 -13
  189. package/dist/operators/types.js +0 -1
  190. package/dist/operators/utils.d.ts +0 -16
  191. package/dist/operators/utils.js +0 -23
  192. package/dist/presets/curves.d.ts +0 -28
  193. package/dist/presets/curves.js +0 -145
  194. package/dist/presets/index.d.ts +0 -2
  195. package/dist/presets/index.js +0 -1
  196. package/dist/presets/tokens/index.d.ts +0 -3
  197. package/dist/presets/tokens/index.js +0 -3
  198. package/dist/presets/tokens/minimal-ui.d.ts +0 -6
  199. package/dist/presets/tokens/minimal-ui.js +0 -53
  200. package/dist/presets/tokens/modern-ui.d.ts +0 -5
  201. package/dist/presets/tokens/modern-ui.js +0 -83
  202. package/dist/presets/tokens/presets.test.d.ts +0 -1
  203. package/dist/presets/tokens/presets.test.js +0 -31
  204. package/dist/presets/tokens/radixLike-ui.d.ts +0 -6
  205. package/dist/presets/tokens/radixLike-ui.js +0 -77
  206. package/dist/serialize/index.d.ts +0 -1
  207. package/dist/serialize/index.js +0 -1
  208. package/dist/serialize/normalizeOutput.d.ts +0 -6
  209. package/dist/serialize/normalizeOutput.js +0 -45
  210. package/dist/serialize/serializeColor.d.ts +0 -21
  211. package/dist/serialize/serializeColor.js +0 -178
  212. package/dist/serialize/serializeResolved.test.d.ts +0 -1
  213. package/dist/serialize/serializeResolved.test.js +0 -45
  214. package/dist/serialize.d.ts +0 -1
  215. package/dist/serialize.js +0 -1
  216. package/dist/utils/clamp.d.ts +0 -1
  217. package/dist/utils/clamp.js +0 -1
  218. package/dist/utils/index.d.ts +0 -1
  219. package/dist/utils/index.js +0 -1
  220. package/dist/utils/lerp.d.ts +0 -1
  221. package/dist/utils/lerp.js +0 -1
  222. package/dist/utils/parseColor.d.ts +0 -6
  223. package/dist/utils/parseColor.js +0 -67
  224. package/dist/utils/parseColor.test.d.ts +0 -1
  225. package/dist/utils/parseColor.test.js +0 -51
  226. package/dist/utils/smoothstep.d.ts +0 -1
  227. package/dist/utils/smoothstep.js +0 -5
  228. package/planning/phase-10-review.md +0 -550
  229. package/planning/phase-7-review.md +0 -411
  230. package/planning/phase-8-review.md +0 -669
  231. package/planning/phase-9-review.md +0 -564
  232. package/planning/roadmap-v0.3.md +0 -284
  233. package/planning/spec-serializer-v0.3.md +0 -324
  234. package/planning/spec-v0.3.md +0 -305
  235. package/src/cli/args.test.ts +0 -28
  236. package/src/cli/args.ts +0 -66
  237. package/src/cli/codegen/__snapshots__/tokens.test.ts.snap +0 -87
  238. package/src/cli/codegen/tokens.test.ts +0 -61
  239. package/src/cli/codegen/tokens.ts +0 -191
  240. package/src/cli/config.ts +0 -71
  241. package/src/cli/validate.test.ts +0 -49
  242. package/src/cli/validate.ts +0 -38
  243. package/src/cli.ts +0 -183
  244. package/src/contrast/apca.test.ts +0 -20
  245. package/src/contrast/apca.ts +0 -26
  246. package/src/contrast/index.ts +0 -4
  247. package/src/contrast/scoring.test.ts +0 -188
  248. package/src/contrast/scoring.ts +0 -48
  249. package/src/contrast/solver.test.ts +0 -147
  250. package/src/contrast/solver.ts +0 -235
  251. package/src/contrast/types.ts +0 -20
  252. package/src/contrast/utils.ts +0 -28
  253. package/src/contrast/wcag2.test.ts +0 -21
  254. package/src/contrast/wcag2.ts +0 -24
  255. package/src/core/createTheme.ts +0 -78
  256. package/src/core/dx-helpers.test.ts +0 -82
  257. package/src/core/index.ts +0 -7
  258. package/src/core/onSolid.test.ts +0 -146
  259. package/src/core/qa.v1.test.ts +0 -149
  260. package/src/core/resolve.test.ts +0 -99
  261. package/src/core/resolve.ts +0 -11
  262. package/src/core/resolveMany.ts +0 -22
  263. package/src/core/tokenRegistry.test.ts +0 -153
  264. package/src/core/tokenRegistry.ts +0 -114
  265. package/src/engine/applyOperators.ts +0 -32
  266. package/src/engine/context.ts +0 -8
  267. package/src/engine/gamut.test.ts +0 -30
  268. package/src/engine/gamut.ts +0 -144
  269. package/src/engine/generateScale.test.ts +0 -46
  270. package/src/engine/generateScale.ts +0 -48
  271. package/src/engine/index.ts +0 -8
  272. package/src/engine/normalize.test.ts +0 -222
  273. package/src/engine/normalize.ts +0 -550
  274. package/src/engine/onSolid.ts +0 -178
  275. package/src/engine/resolveBaseColor.test.ts +0 -117
  276. package/src/engine/resolveBaseColor.ts +0 -203
  277. package/src/export/__snapshots__/exportTheme.test.ts.snap +0 -74
  278. package/src/export/exportTheme.test.ts +0 -144
  279. package/src/export/exportTheme.ts +0 -251
  280. package/src/export/index.ts +0 -1
  281. package/src/export/serializeColor.test.ts +0 -73
  282. package/src/export/serializeColor.ts +0 -1
  283. package/src/export.ts +0 -1
  284. package/src/index.ts +0 -3
  285. package/src/operators/emphasis.test.ts +0 -85
  286. package/src/operators/emphasis.ts +0 -132
  287. package/src/operators/index.ts +0 -3
  288. package/src/operators/state.test.ts +0 -66
  289. package/src/operators/state.ts +0 -122
  290. package/src/operators/types.ts +0 -14
  291. package/src/operators/utils.ts +0 -44
  292. package/src/presets/curves.ts +0 -168
  293. package/src/presets/index.ts +0 -2
  294. package/src/presets/tokens/index.ts +0 -3
  295. package/src/presets/tokens/minimal-ui.ts +0 -55
  296. package/src/presets/tokens/modern-ui.ts +0 -85
  297. package/src/presets/tokens/presets.test.ts +0 -46
  298. package/src/presets/tokens/radixLike-ui.ts +0 -79
  299. package/src/serialize/index.ts +0 -1
  300. package/src/serialize/normalizeOutput.ts +0 -63
  301. package/src/serialize/serializeColor.ts +0 -260
  302. package/src/serialize/serializeResolved.test.ts +0 -57
  303. package/src/serialize.ts +0 -1
  304. package/src/types/index.ts +0 -207
  305. package/src/utils/clamp.ts +0 -2
  306. package/src/utils/index.ts +0 -1
  307. package/src/utils/lerp.ts +0 -1
  308. package/src/utils/parseColor.test.ts +0 -66
  309. package/src/utils/parseColor.ts +0 -87
  310. package/src/utils/smoothstep.ts +0 -6
  311. package/tsconfig.build.json +0 -11
  312. package/tsconfig.json +0 -15
@@ -1,153 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { createTheme } from "./createTheme.js";
4
- import { resolveToken, resolveTokenRegistry, validateTokenRegistry } from "./tokenRegistry.js";
5
- import type { TokenRegistry } from "../types/index.js";
6
-
7
- describe("token registry", () => {
8
- const theme = createTheme({
9
- seeds: {
10
- light: { neutral: "#8B8D98", accent: "#3D63DD" },
11
- dark: { neutral: "#8B8D98", accent: "#3D63DD" },
12
- },
13
- preset: "modern",
14
- });
15
-
16
- it("validates tokens and resolves through theme.resolve", () => {
17
- const registry: TokenRegistry = {
18
- tokens: {
19
- "bg.app": {
20
- name: "bg.app",
21
- description: "App background",
22
- category: "background",
23
- query: { role: "bg.app", usage: "bg", surface: "app" },
24
- states: { hover: true },
25
- },
26
- },
27
- };
28
-
29
- validateTokenRegistry(registry);
30
- const resolved = resolveTokenRegistry(registry, theme);
31
-
32
- expect(resolved["bg.app"].step).toBeGreaterThan(0);
33
-
34
- const single = resolveToken(registry.tokens["bg.app"], theme);
35
- const expected = theme.resolve(registry.tokens["bg.app"].query);
36
- expect(single.step).toBe(expected.step);
37
- });
38
-
39
- it("throws when required query fields are missing", () => {
40
- const registry: TokenRegistry = {
41
- tokens: {
42
- "text.primary": {
43
- name: "text.primary",
44
- query: { role: "text.primary", usage: "text" },
45
- },
46
- },
47
- };
48
-
49
- expect(() => validateTokenRegistry(registry)).toThrow(/requires a surface/i);
50
- });
51
-
52
- it("throws when output options are provided", () => {
53
- const registry: TokenRegistry = {
54
- tokens: {
55
- "bg.app": {
56
- name: "bg.app",
57
- query: {
58
- role: "bg.app",
59
- usage: "bg",
60
- surface: "app",
61
- output: { strict: true },
62
- } as never,
63
- },
64
- },
65
- };
66
-
67
- expect(() => validateTokenRegistry(registry)).toThrow(/must not include output/i);
68
- });
69
-
70
- it("treats whitespace-padded default state as default", () => {
71
- const registry: TokenRegistry = {
72
- tokens: {
73
- "bg.app": {
74
- name: "bg.app",
75
- query: {
76
- role: "bg.app",
77
- usage: "bg",
78
- surface: "app",
79
- state: " default " as never,
80
- } as never,
81
- },
82
- },
83
- };
84
-
85
- expect(() => validateTokenRegistry(registry)).not.toThrow();
86
- });
87
-
88
- it("throws when token query encodes a non-default state", () => {
89
- const registry: TokenRegistry = {
90
- tokens: {
91
- "bg.app": {
92
- name: "bg.app",
93
- query: { role: "bg.app", usage: "bg", surface: "app", state: "hover" as never } as never,
94
- },
95
- },
96
- };
97
-
98
- expect(() => validateTokenRegistry(registry)).toThrow(/must not encode state/i);
99
- });
100
-
101
- it("throws when token query includes a literal background color hint", () => {
102
- const registry: TokenRegistry = {
103
- tokens: {
104
- "bg.app": {
105
- name: "bg.app",
106
- query: {
107
- role: "bg.app",
108
- usage: "bg",
109
- surface: "app",
110
- on: { kind: "color", value: "#fff" } as never,
111
- },
112
- },
113
- },
114
- };
115
-
116
- expect(() => validateTokenRegistry(registry)).toThrow(/literal background color/i);
117
- });
118
-
119
- it("throws when token states include invalid keys or non-true values", () => {
120
- const invalidKey: TokenRegistry = {
121
- tokens: {
122
- "bg.app": {
123
- name: "bg.app",
124
- query: { role: "bg.app", usage: "bg", surface: "app" },
125
- states: { nope: true } as never,
126
- },
127
- },
128
- };
129
- expect(() => validateTokenRegistry(invalidKey)).toThrow(/Invalid token state/i);
130
-
131
- const includesDefault: TokenRegistry = {
132
- tokens: {
133
- "bg.app": {
134
- name: "bg.app",
135
- query: { role: "bg.app", usage: "bg", surface: "app" },
136
- states: { default: true } as never,
137
- },
138
- },
139
- };
140
- expect(() => validateTokenRegistry(includesDefault)).toThrow(/Invalid token state/i);
141
-
142
- const nonTrueValue: TokenRegistry = {
143
- tokens: {
144
- "bg.app": {
145
- name: "bg.app",
146
- query: { role: "bg.app", usage: "bg", surface: "app" },
147
- states: { hover: false } as never,
148
- },
149
- },
150
- };
151
- expect(() => validateTokenRegistry(nonTrueValue)).toThrow(/must be true/i);
152
- });
153
- });
@@ -1,114 +0,0 @@
1
- import { COLOR_STATES, normalizeQuery } from "../engine/normalize.js";
2
- import type { BaseResolvedColor } from "../engine/resolveBaseColor.js";
3
- import type { TokenDefinition, TokenRegistry, TokenState } from "../types/index.js";
4
- import type { PaletteTheme } from "./createTheme.js";
5
-
6
- const ALLOWED_TOKEN_STATES: TokenState[] = COLOR_STATES.filter(
7
- (state): state is TokenState => state !== "default",
8
- );
9
-
10
- const validateTokenStates = (states: TokenDefinition["states"], name: string) => {
11
- if (!states) return;
12
-
13
- for (const [state, enabled] of Object.entries(states)) {
14
- if (enabled !== true) {
15
- throw new Error(`Token "${name}" state "${state}" must be true`);
16
- }
17
-
18
- if (!ALLOWED_TOKEN_STATES.includes(state as TokenState)) {
19
- throw new Error(`Invalid token state "${state}" for "${name}"`);
20
- }
21
- }
22
- };
23
-
24
- /**
25
- * Validate a token definition for safe registry usage.
26
- *
27
- * Guarantees (Phase 3):
28
- * - registry stays declarative (no output options / no embedded color literals)
29
- * - tokens are base tokens (interactive states declared via `token.states`)
30
- */
31
- export const validateTokenDefinition = (token: TokenDefinition): void => {
32
- if (typeof token.name !== "string" || !token.name.trim()) {
33
- throw new Error("Token name is required");
34
- }
35
-
36
- if (typeof token.query?.role !== "string" || !token.query.role.trim()) {
37
- throw new Error(`Token "${token.name}" requires a query role`);
38
- }
39
-
40
- if (!token.query.usage) {
41
- throw new Error(`Token "${token.name}" requires a usage`);
42
- }
43
-
44
- if (!token.query.surface) {
45
- throw new Error(`Token "${token.name}" requires a surface`);
46
- }
47
-
48
- const rawState = (token.query as { state?: unknown }).state;
49
- const normalizedState = typeof rawState === "string" ? rawState.trim() : (rawState as undefined);
50
- if (normalizedState && normalizedState !== "default") {
51
- throw new Error(
52
- `Token "${token.name}" must not encode state in query; declare supported states via token.states`,
53
- );
54
- }
55
-
56
- const onKind = (token.query.on as { kind?: string } | undefined)?.kind;
57
- if (onKind === "color") {
58
- throw new Error(
59
- `Token "${token.name}" must not include a literal background color hint; use { kind: "role" } or { kind: "auto" }`,
60
- );
61
- }
62
-
63
- if (token.query.output) {
64
- throw new Error(`Token "${token.name}" must not include output options`);
65
- }
66
-
67
- validateTokenStates(token.states, token.name);
68
-
69
- // Delegate to core validation for strict field validation.
70
- normalizeQuery({ ...token.query, output: { strict: true } });
71
- };
72
-
73
- /**
74
- * Validate a token registry and each token definition it contains.
75
- */
76
- export const validateTokenRegistry = (registry: TokenRegistry): void => {
77
- const entries = Object.entries(registry.tokens);
78
-
79
- if (entries.length === 0) {
80
- throw new Error("Token registry must include at least one token");
81
- }
82
-
83
- for (const [name, token] of entries) {
84
- if (token.name !== name) {
85
- throw new Error(`Token name mismatch: expected "${name}", got "${token.name}"`);
86
- }
87
- validateTokenDefinition(token);
88
- }
89
- };
90
-
91
- /**
92
- * Resolve a token definition through the provided theme.
93
- */
94
- export const resolveToken = (token: TokenDefinition, theme: PaletteTheme): BaseResolvedColor => {
95
- validateTokenDefinition(token);
96
- return theme.resolve(token.query);
97
- };
98
-
99
- /**
100
- * Resolve all tokens in a registry while preserving key order.
101
- */
102
- export const resolveTokenRegistry = (
103
- registry: TokenRegistry,
104
- theme: PaletteTheme,
105
- ): Record<string, BaseResolvedColor> => {
106
- validateTokenRegistry(registry);
107
- const resolved: Record<string, BaseResolvedColor> = {};
108
-
109
- for (const [name, token] of Object.entries(registry.tokens)) {
110
- resolved[name] = theme.resolve(token.query);
111
- }
112
-
113
- return resolved;
114
- };
@@ -1,32 +0,0 @@
1
- import { applyEmphasisOperator } from "../operators/emphasis.js";
2
- import { applyStateOperator } from "../operators/state.js";
3
- import { mapColorContextToEngine } from "./context.js";
4
- import type { NormalizedQuery } from "./normalize.js";
5
- import type { BaseResolvedColor, ThemeConfig } from "./resolveBaseColor.js";
6
-
7
- export const applyOperators = (
8
- base: BaseResolvedColor,
9
- normalized: NormalizedQuery,
10
- theme: ThemeConfig,
11
- ): BaseResolvedColor => {
12
- const context = mapColorContextToEngine(normalized.context);
13
- const operatorInput = {
14
- oklch: base.oklch,
15
- context,
16
- surface: normalized.surface,
17
- usage: normalized.usage,
18
- state: normalized.state,
19
- emphasis: normalized.emphasis,
20
- preset: theme.preset,
21
- step: base.step,
22
- };
23
-
24
- // Emphasis defines the baseline hierarchy; state applies transient interaction.
25
- const emphasized = applyEmphasisOperator(operatorInput);
26
- const stated = applyStateOperator({ ...operatorInput, oklch: emphasized });
27
-
28
- return {
29
- ...base,
30
- oklch: stated,
31
- };
32
- };
@@ -1,8 +0,0 @@
1
- import type { ColorContext } from "../types/index.js";
2
- import type { NormalizedQuery } from "./normalize.js";
3
-
4
- export type EngineContext = "light" | "dark";
5
-
6
- export const mapColorContextToEngine = (
7
- context: ColorContext | NormalizedQuery["context"],
8
- ): EngineContext => (context === "dark" || context === "dimmed" ? "dark" : "light");
@@ -1,30 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { isInGamut, mapToGamut } from "./gamut.js";
4
- import type { OkLchColor } from "./generateScale.js";
5
-
6
- describe("gamut mapping", () => {
7
- const outOfSrgbInP3: OkLchColor = { l: 60, c: 0.2, h: 40 };
8
-
9
- it("detects out-of-gamut sRGB colors", () => {
10
- expect(isInGamut(outOfSrgbInP3, "srgb")).toBe(false);
11
- expect(isInGamut(outOfSrgbInP3, "p3")).toBe(true);
12
- });
13
-
14
- it("clips to sRGB gamut", () => {
15
- const clipped = mapToGamut(outOfSrgbInP3, "srgb", "clip", false);
16
- expect(isInGamut(clipped, "srgb")).toBe(true);
17
- });
18
-
19
- it("compresses chroma to sRGB gamut", () => {
20
- const compressed = mapToGamut(outOfSrgbInP3, "srgb", "compressChroma", false);
21
- expect(isInGamut(compressed, "srgb")).toBe(true);
22
- expect(compressed.c).toBeLessThanOrEqual(outOfSrgbInP3.c);
23
- });
24
-
25
- it("keeps chroma when already in P3", () => {
26
- const mapped = mapToGamut(outOfSrgbInP3, "p3", "preferP3ThenCompress", false);
27
- expect(isInGamut(mapped, "p3")).toBe(true);
28
- expect(mapped.c).toBeCloseTo(outOfSrgbInP3.c, 4);
29
- });
30
- });
@@ -1,144 +0,0 @@
1
- import { clampChroma, converter } from "culori";
2
-
3
- import type { OutputOptions } from "../types/index.js";
4
- import { clamp } from "../utils/clamp.js";
5
- import type { OkLchColor } from "./generateScale.js";
6
-
7
- export type GamutTarget = "srgb" | "p3";
8
- export type GamutMapping = NonNullable<OutputOptions["gamutMapping"]>;
9
-
10
- type GamutRgb = { r: number; g: number; b: number };
11
- type CuloriOklch = { mode: "oklch"; l: number; c: number; h: number; alpha?: number };
12
-
13
- const toRgb = converter("rgb");
14
- const toP3 = converter("p3");
15
- const toOklch = converter("oklch");
16
-
17
- const normalizeHue = (hue: number) => ((hue % 360) + 360) % 360;
18
-
19
- const toCuloriOklch = (color: OkLchColor): CuloriOklch => ({
20
- mode: "oklch",
21
- l: clamp(color.l, 0, 100) / 100,
22
- c: Math.max(0, color.c),
23
- h: color.h,
24
- alpha: color.alpha ?? 1,
25
- });
26
-
27
- const fromCuloriOklch = (color: { l?: number; c?: number; h?: number; alpha?: number }) => ({
28
- l: clamp(typeof color.l === "number" && Number.isFinite(color.l) ? color.l * 100 : 0, 0, 100),
29
- c: Math.max(0, typeof color.c === "number" && Number.isFinite(color.c) ? color.c : 0),
30
- h: normalizeHue(typeof color.h === "number" && Number.isFinite(color.h) ? color.h : 0),
31
- ...(typeof color.alpha === "number" ? { alpha: color.alpha } : {}),
32
- });
33
-
34
- const clamp01 = (value: number) => Math.min(1, Math.max(0, value));
35
-
36
- const toTargetRgb = (color: OkLchColor, target: GamutTarget): GamutRgb | null => {
37
- const source = toCuloriOklch(color);
38
- const rgb = target === "p3" ? toP3(source) : toRgb(source);
39
-
40
- if (!rgb) {
41
- return null;
42
- }
43
-
44
- const r = typeof rgb.r === "number" && Number.isFinite(rgb.r) ? rgb.r : Number.NaN;
45
- const g = typeof rgb.g === "number" && Number.isFinite(rgb.g) ? rgb.g : Number.NaN;
46
- const b = typeof rgb.b === "number" && Number.isFinite(rgb.b) ? rgb.b : Number.NaN;
47
-
48
- if (!Number.isFinite(r) || !Number.isFinite(g) || !Number.isFinite(b)) {
49
- return null;
50
- }
51
-
52
- return { r, g, b };
53
- };
54
-
55
- const toOklchFromRgb = (rgb: GamutRgb, target: GamutTarget) => {
56
- const converted = toOklch({
57
- mode: (target === "p3" ? "p3" : "rgb") as "p3" | "rgb",
58
- r: clamp01(rgb.r),
59
- g: clamp01(rgb.g),
60
- b: clamp01(rgb.b),
61
- });
62
-
63
- if (!converted) {
64
- return null;
65
- }
66
-
67
- return fromCuloriOklch(converted);
68
- };
69
-
70
- const GAMUT_EPSILON = 1e-6;
71
-
72
- const inGamut = (rgb: GamutRgb | null) =>
73
- !!rgb &&
74
- Number.isFinite(rgb.r) &&
75
- Number.isFinite(rgb.g) &&
76
- Number.isFinite(rgb.b) &&
77
- rgb.r >= -GAMUT_EPSILON &&
78
- rgb.r <= 1 + GAMUT_EPSILON &&
79
- rgb.g >= -GAMUT_EPSILON &&
80
- rgb.g <= 1 + GAMUT_EPSILON &&
81
- rgb.b >= -GAMUT_EPSILON &&
82
- rgb.b <= 1 + GAMUT_EPSILON;
83
-
84
- const fallbackColor = (color: OkLchColor): OkLchColor => ({
85
- l: 0,
86
- c: 0,
87
- h: 0,
88
- alpha: color.alpha ?? 1,
89
- });
90
-
91
- export const isInGamut = (color: OkLchColor, target: GamutTarget): boolean =>
92
- inGamut(toTargetRgb(color, target));
93
-
94
- export const mapToGamut = (
95
- color: OkLchColor,
96
- target: GamutTarget,
97
- mapping: GamutMapping,
98
- strict: boolean,
99
- ): OkLchColor => {
100
- // Note:
101
- // `preferP3ThenCompress` is primarily a caller-level strategy (e.g. serializeColor prefers P3 when possible).
102
- // Inside this mapper, any non-clip mapping uses chroma compression (via clampChroma) when mapping is needed.
103
-
104
- const rgb = toTargetRgb(color, target);
105
-
106
- if (!rgb) {
107
- if (strict) {
108
- throw new Error(`Unable to convert color to ${target}`);
109
- }
110
- return fallbackColor(color);
111
- }
112
-
113
- if (mapping === "clip") {
114
- if (inGamut(rgb)) {
115
- return color;
116
- }
117
-
118
- const clipped = { r: clamp01(rgb.r), g: clamp01(rgb.g), b: clamp01(rgb.b) };
119
- const clippedOklch = toOklchFromRgb(clipped, target);
120
- return clippedOklch
121
- ? {
122
- ...clippedOklch,
123
- ...(typeof color.alpha === "number" ? { alpha: color.alpha } : {}),
124
- }
125
- : fallbackColor(color);
126
- }
127
-
128
- if (inGamut(rgb)) {
129
- return color;
130
- }
131
-
132
- const clamped = clampChroma(toCuloriOklch(color), "oklch", target === "p3" ? "p3" : "rgb");
133
- if (!clamped) {
134
- if (strict) {
135
- throw new Error(`Unable to clamp chroma for ${target}`);
136
- }
137
- return fallbackColor(color);
138
- }
139
-
140
- return fromCuloriOklch(clamped);
141
- };
142
-
143
- export const toGamutRgb = (color: OkLchColor, target: GamutTarget): GamutRgb | null =>
144
- toTargetRgb(color, target);
@@ -1,46 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { generateScale } from "./generateScale.js";
4
-
5
- const seed = { l: 49.5, c: 0.19, h: 264.0 };
6
-
7
- const isMonotonic = (values: number[]) =>
8
- values.every((value, index) => index === 0 || value >= values[index - 1] - 1e-6);
9
-
10
- describe("generateScale", () => {
11
- it("returns 12 steps", () => {
12
- const result = generateScale(seed, { context: "light", surface: "surface" });
13
-
14
- expect(result).toHaveLength(12);
15
- });
16
-
17
- it("keeps lightness monotonic in light context", () => {
18
- const result = generateScale(seed, { context: "light", surface: "surface" });
19
- const lightness = result.map((step) => step.l);
20
-
21
- expect(isMonotonic(lightness)).toBe(true);
22
- });
23
-
24
- it("keeps lightness monotonic in dark context", () => {
25
- const result = generateScale(seed, { context: "dark", surface: "surface" });
26
- const lightness = result.map((step) => step.l);
27
-
28
- expect(isMonotonic(lightness)).toBe(true);
29
- });
30
-
31
- it("keeps hue constant across steps", () => {
32
- const result = generateScale(seed, { context: "light", surface: "surface" });
33
-
34
- result.forEach((step) => {
35
- expect(step.h).toBeCloseTo(seed.h, 6);
36
- });
37
- });
38
-
39
- it("never produces negative chroma", () => {
40
- const result = generateScale(seed, { context: "light", surface: "surface" });
41
-
42
- result.forEach((step) => {
43
- expect(step.c).toBeGreaterThanOrEqual(0);
44
- });
45
- });
46
- });
@@ -1,48 +0,0 @@
1
- import type { CurvePresetName } from "../presets/index.js";
2
- import { curvePresets } from "../presets/index.js";
3
- import type { SurfaceIntent } from "../types/index.js";
4
- import { clamp } from "../utils/clamp.js";
5
- import { lerp } from "../utils/lerp.js";
6
-
7
- export type OkLchColor = {
8
- l: number;
9
- c: number;
10
- h: number;
11
- alpha?: number;
12
- };
13
-
14
- type GenerateScaleOptions = {
15
- context: "light" | "dark";
16
- surface: SurfaceIntent;
17
- preset?: CurvePresetName;
18
- };
19
-
20
- const STEPS = 12;
21
- const OKLCH_L_MIN = 0;
22
- const OKLCH_L_MAX = 100;
23
-
24
- export function generateScale(seed: OkLchColor, options: GenerateScaleOptions): OkLchColor[] {
25
- const presetName = options.preset ?? "modern";
26
- const preset = curvePresets[presetName];
27
- const surfaceCurve = preset.surfaces[options.surface];
28
- const range = surfaceCurve.ranges[options.context];
29
-
30
- return Array.from({ length: STEPS }, (_, index) => {
31
- const t = index / (STEPS - 1);
32
- const lightnessT = surfaceCurve.l(t);
33
- const chromaT = surfaceCurve.c(lightnessT);
34
- const l = clamp(lerp(range.l[0], range.l[1], lightnessT), OKLCH_L_MIN, OKLCH_L_MAX);
35
- // Allow select surfaces to exceed seed chroma slightly while staying within range caps.
36
- const chromaBoost = range.chromaBoost ?? 1;
37
- const boostedSeed = (seed.c ?? range.cMax) * chromaBoost;
38
- const maxChroma = Math.min(range.cMax, Math.max(0, boostedSeed));
39
- const c = clamp(lerp(range.cMin, maxChroma, chromaT), 0, range.cMax);
40
-
41
- return {
42
- l,
43
- c,
44
- h: seed.h,
45
- alpha: seed.alpha,
46
- };
47
- });
48
- }
@@ -1,8 +0,0 @@
1
- export type { EngineContext } from "./context.js";
2
- export { mapColorContextToEngine } from "./context.js";
3
- export type { OkLchColor } from "./generateScale.js";
4
- export { generateScale } from "./generateScale.js";
5
- export type { NormalizedOnSolidQuery, NormalizedQuery } from "./normalize.js";
6
- export { normalizeOnSolidQuery, normalizeQuery } from "./normalize.js";
7
- export type { BaseResolvedColor, ThemeConfig } from "./resolveBaseColor.js";
8
- export { resolveBaseColor } from "./resolveBaseColor.js";