@ankhorage/zora 0.16.2 → 1.0.1

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 (114) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/README.md +27 -22
  3. package/dist/components/heading/resolveHeadingRecipe.d.ts +2 -2
  4. package/dist/components/heading/resolveHeadingRecipe.d.ts.map +1 -1
  5. package/dist/components/heading/resolveHeadingRecipe.js.map +1 -1
  6. package/dist/components/text/resolveTextRecipe.d.ts +2 -2
  7. package/dist/components/text/resolveTextRecipe.d.ts.map +1 -1
  8. package/dist/components/text/resolveTextRecipe.js.map +1 -1
  9. package/dist/patterns/theme-composer/ThemeComposer.d.ts.map +1 -1
  10. package/dist/patterns/theme-composer/ThemeComposer.js +101 -86
  11. package/dist/patterns/theme-composer/ThemeComposer.js.map +1 -1
  12. package/dist/patterns/theme-composer/index.d.ts +1 -1
  13. package/dist/patterns/theme-composer/index.d.ts.map +1 -1
  14. package/dist/patterns/theme-composer/index.js.map +1 -1
  15. package/dist/patterns/theme-composer/types.d.ts +3 -13
  16. package/dist/patterns/theme-composer/types.d.ts.map +1 -1
  17. package/dist/patterns/theme-composer/types.js.map +1 -1
  18. package/dist/theme/createZoraThemeConfig.d.ts +1 -1
  19. package/dist/theme/createZoraThemeConfig.d.ts.map +1 -1
  20. package/dist/theme/createZoraThemeConfig.js +5 -6
  21. package/dist/theme/createZoraThemeConfig.js.map +1 -1
  22. package/dist/theme/index.d.ts +1 -1
  23. package/dist/theme/index.d.ts.map +1 -1
  24. package/dist/theme/index.js.map +1 -1
  25. package/dist/theme/types.d.ts +16 -11
  26. package/dist/theme/types.d.ts.map +1 -1
  27. package/dist/theme/types.js +1 -20
  28. package/dist/theme/types.js.map +1 -1
  29. package/dist/theme/useZoraTheme.d.ts +1 -1
  30. package/dist/theme/zoraDefaultTheme.js +1 -1
  31. package/dist/theme/zoraDefaultTheme.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/components/heading/resolveHeadingRecipe.test.ts +30 -5
  34. package/src/components/heading/resolveHeadingRecipe.ts +6 -6
  35. package/src/components/text/resolveTextRecipe.test.ts +30 -5
  36. package/src/components/text/resolveTextRecipe.ts +6 -6
  37. package/src/patterns/theme-composer/ThemeComposer.test.ts +128 -114
  38. package/src/patterns/theme-composer/ThemeComposer.tsx +130 -128
  39. package/src/patterns/theme-composer/index.ts +1 -6
  40. package/src/patterns/theme-composer/types.ts +4 -15
  41. package/src/showcaseCoverage.test.ts +14 -0
  42. package/src/theme/createZoraThemeConfig.test.ts +51 -26
  43. package/src/theme/createZoraThemeConfig.ts +7 -7
  44. package/src/theme/index.ts +1 -3
  45. package/src/theme/types.ts +22 -34
  46. package/src/theme/zoraDefaultTheme.ts +1 -1
  47. package/dist/internal/color/colorToneRecipes.d.ts +0 -23
  48. package/dist/internal/color/colorToneRecipes.d.ts.map +0 -1
  49. package/dist/internal/color/colorToneRecipes.js +0 -139
  50. package/dist/internal/color/colorToneRecipes.js.map +0 -1
  51. package/dist/internal/color/harmony.d.ts +0 -12
  52. package/dist/internal/color/harmony.d.ts.map +0 -1
  53. package/dist/internal/color/harmony.js +0 -69
  54. package/dist/internal/color/harmony.js.map +0 -1
  55. package/dist/internal/color/hue.d.ts +0 -3
  56. package/dist/internal/color/hue.d.ts.map +0 -1
  57. package/dist/internal/color/hue.js +0 -7
  58. package/dist/internal/color/hue.js.map +0 -1
  59. package/dist/internal/color/index.d.ts +0 -10
  60. package/dist/internal/color/index.d.ts.map +0 -1
  61. package/dist/internal/color/index.js +0 -10
  62. package/dist/internal/color/index.js.map +0 -1
  63. package/dist/internal/color/oklch.d.ts +0 -6
  64. package/dist/internal/color/oklch.d.ts.map +0 -1
  65. package/dist/internal/color/oklch.js +0 -50
  66. package/dist/internal/color/oklch.js.map +0 -1
  67. package/dist/internal/color/primary.d.ts +0 -3
  68. package/dist/internal/color/primary.d.ts.map +0 -1
  69. package/dist/internal/color/primary.js +0 -44
  70. package/dist/internal/color/primary.js.map +0 -1
  71. package/dist/internal/color/roleHues.d.ts +0 -15
  72. package/dist/internal/color/roleHues.d.ts.map +0 -1
  73. package/dist/internal/color/roleHues.js +0 -103
  74. package/dist/internal/color/roleHues.js.map +0 -1
  75. package/dist/internal/color/roleScales.d.ts +0 -20
  76. package/dist/internal/color/roleScales.d.ts.map +0 -1
  77. package/dist/internal/color/roleScales.js +0 -79
  78. package/dist/internal/color/roleScales.js.map +0 -1
  79. package/dist/internal/color/scales.d.ts +0 -19
  80. package/dist/internal/color/scales.d.ts.map +0 -1
  81. package/dist/internal/color/scales.js +0 -135
  82. package/dist/internal/color/scales.js.map +0 -1
  83. package/dist/internal/color/semanticTokens.d.ts +0 -28
  84. package/dist/internal/color/semanticTokens.d.ts.map +0 -1
  85. package/dist/internal/color/semanticTokens.js +0 -84
  86. package/dist/internal/color/semanticTokens.js.map +0 -1
  87. package/dist/internal/color/types.d.ts +0 -10
  88. package/dist/internal/color/types.d.ts.map +0 -1
  89. package/dist/internal/color/types.js +0 -4
  90. package/dist/internal/color/types.js.map +0 -1
  91. package/dist/patterns/theme-composer/recommendations.d.ts +0 -14
  92. package/dist/patterns/theme-composer/recommendations.d.ts.map +0 -1
  93. package/dist/patterns/theme-composer/recommendations.js +0 -58
  94. package/dist/patterns/theme-composer/recommendations.js.map +0 -1
  95. package/src/internal/color/colorToneRecipes.test.ts +0 -89
  96. package/src/internal/color/colorToneRecipes.ts +0 -167
  97. package/src/internal/color/harmony.test.ts +0 -145
  98. package/src/internal/color/harmony.ts +0 -96
  99. package/src/internal/color/hue.test.ts +0 -28
  100. package/src/internal/color/hue.ts +0 -7
  101. package/src/internal/color/index.ts +0 -44
  102. package/src/internal/color/oklch.ts +0 -65
  103. package/src/internal/color/primary.test.ts +0 -105
  104. package/src/internal/color/primary.ts +0 -64
  105. package/src/internal/color/roleHues.test.ts +0 -197
  106. package/src/internal/color/roleHues.ts +0 -142
  107. package/src/internal/color/roleScales.test.ts +0 -220
  108. package/src/internal/color/roleScales.ts +0 -127
  109. package/src/internal/color/scales.test.ts +0 -151
  110. package/src/internal/color/scales.ts +0 -194
  111. package/src/internal/color/semanticTokens.test.ts +0 -170
  112. package/src/internal/color/semanticTokens.ts +0 -114
  113. package/src/internal/color/types.ts +0 -15
  114. package/src/patterns/theme-composer/recommendations.ts +0 -85
@@ -1,142 +0,0 @@
1
- import type { ZoraColorHarmony } from '../../theme/types';
2
- import type { ZoraComputedHarmony, ZoraHarmonySlot, ZoraHarmonySlotId } from './harmony';
3
- import { normalizeHueDegrees } from './hue';
4
-
5
- export type ZoraHueRoleId = 'primary' | 'secondary' | 'accent' | 'highlight' | 'surfaceTint';
6
-
7
- export interface ZoraHueRoleAssignment {
8
- role: ZoraHueRoleId;
9
- hue: number;
10
- sourceSlotId: ZoraHarmonySlotId;
11
- }
12
-
13
- export interface ZoraComputedHueRoles {
14
- harmony: ZoraColorHarmony;
15
- assignments: readonly ZoraHueRoleAssignment[];
16
- }
17
-
18
- function findSlot(
19
- harmony: ZoraComputedHarmony,
20
- id: ZoraHarmonySlotId,
21
- ): ZoraHarmonySlot | undefined {
22
- return harmony.orderedSlots.find((slot) => slot.id === id);
23
- }
24
-
25
- function requireBaseSlot(harmony: ZoraComputedHarmony): ZoraHarmonySlot {
26
- const base = findSlot(harmony, 'base');
27
- if (!base) {
28
- throw new Error(`[zora] Expected harmony to include a base slot (kind: ${harmony.kind}).`);
29
- }
30
- return base;
31
- }
32
-
33
- function assignRole(
34
- role: ZoraHueRoleId,
35
- harmony: ZoraComputedHarmony,
36
- preferred: ZoraHarmonySlotId,
37
- fallback: ZoraHarmonySlotId = 'base',
38
- ): ZoraHueRoleAssignment {
39
- const base = requireBaseSlot(harmony);
40
- const preferredSlot = findSlot(harmony, preferred);
41
- const fallbackSlot = findSlot(harmony, fallback);
42
- const chosenSlot = preferredSlot ?? fallbackSlot ?? base;
43
-
44
- return {
45
- role,
46
- hue: normalizeHueDegrees(chosenSlot.hue),
47
- sourceSlotId: chosenSlot.id,
48
- };
49
- }
50
-
51
- export function assignZoraHarmonyRoleHues(harmony: ZoraComputedHarmony): ZoraComputedHueRoles {
52
- const { kind } = harmony;
53
-
54
- if (kind === 'monochromatic') {
55
- return {
56
- harmony: kind,
57
- assignments: [
58
- assignRole('primary', harmony, 'base'),
59
- assignRole('secondary', harmony, 'base'),
60
- assignRole('accent', harmony, 'base'),
61
- assignRole('highlight', harmony, 'base'),
62
- assignRole('surfaceTint', harmony, 'base'),
63
- ],
64
- };
65
- }
66
-
67
- if (kind === 'complementary') {
68
- return {
69
- harmony: kind,
70
- assignments: [
71
- assignRole('primary', harmony, 'base'),
72
- assignRole('secondary', harmony, 'base'),
73
- assignRole('accent', harmony, 'a'),
74
- assignRole('highlight', harmony, 'a'),
75
- assignRole('surfaceTint', harmony, 'base'),
76
- ],
77
- };
78
- }
79
-
80
- if (kind === 'analogous') {
81
- return {
82
- harmony: kind,
83
- assignments: [
84
- assignRole('primary', harmony, 'base'),
85
- assignRole('secondary', harmony, 'a'),
86
- assignRole('accent', harmony, 'b', 'a'),
87
- assignRole('highlight', harmony, 'b', 'a'),
88
- assignRole('surfaceTint', harmony, 'a'),
89
- ],
90
- };
91
- }
92
-
93
- if (kind === 'splitComplementary') {
94
- return {
95
- harmony: kind,
96
- assignments: [
97
- assignRole('primary', harmony, 'base'),
98
- assignRole('secondary', harmony, 'a'),
99
- assignRole('accent', harmony, 'b', 'a'),
100
- assignRole('highlight', harmony, 'b', 'a'),
101
- assignRole('surfaceTint', harmony, 'base'),
102
- ],
103
- };
104
- }
105
-
106
- if (kind === 'triadic') {
107
- return {
108
- harmony: kind,
109
- assignments: [
110
- assignRole('primary', harmony, 'base'),
111
- assignRole('secondary', harmony, 'a'),
112
- assignRole('accent', harmony, 'b', 'a'),
113
- assignRole('highlight', harmony, 'b', 'a'),
114
- assignRole('surfaceTint', harmony, 'base'),
115
- ],
116
- };
117
- }
118
-
119
- return {
120
- harmony: kind,
121
- assignments: [
122
- assignRole('primary', harmony, 'base'),
123
- assignRole('secondary', harmony, 'a'),
124
- assignRole('accent', harmony, 'b', 'a'),
125
- assignRole('highlight', harmony, 'c', 'b'),
126
- assignRole('surfaceTint', harmony, 'base'),
127
- ],
128
- };
129
- }
130
-
131
- export function getZoraHueRoleAssignment(
132
- roles: ZoraComputedHueRoles,
133
- role: ZoraHueRoleId,
134
- ): ZoraHueRoleAssignment {
135
- const found = roles.assignments.find((assignment) => assignment.role === role);
136
- if (!found) {
137
- throw new Error(
138
- `[zora] Expected a hue-role assignment for "${role}" (harmony: ${roles.harmony}).`,
139
- );
140
- }
141
- return found;
142
- }
@@ -1,220 +0,0 @@
1
- import { describe, expect, test } from 'bun:test';
2
-
3
- import type { ZoraColorTone, ZoraHexColor } from '../../theme/types';
4
- import {
5
- assignZoraHarmonyRoleHues,
6
- computeZoraHarmony,
7
- createZoraRoleColorScales,
8
- getZoraRoleColorScale,
9
- parseHexToOklch,
10
- ZORA_COLOR_SCALE_ROLE_ORDER,
11
- ZORA_COLOR_SCALE_STEPS,
12
- type ZoraColorScale,
13
- type ZoraColorScaleStep,
14
- type ZoraComputedHueRoles,
15
- type ZoraRoleColorScale,
16
- } from './index';
17
-
18
- function isSixDigitLowercaseHexColor(value: string): value is ZoraHexColor {
19
- return /^#[0-9a-f]{6}$/.test(value);
20
- }
21
-
22
- function hueDeltaDegrees(a: number, b: number): number {
23
- const raw = Math.abs(a - b) % 360;
24
- return Math.min(raw, 360 - raw);
25
- }
26
-
27
- function getStepValues(scale: ZoraColorScale): { step: ZoraColorScaleStep; hex: ZoraHexColor }[] {
28
- return ZORA_COLOR_SCALE_STEPS.map((step) => ({ step, hex: scale[step] }));
29
- }
30
-
31
- function assertScaleKeys(scale: ZoraColorScale) {
32
- const keys = Object.keys(scale)
33
- .map((key) => Number(key))
34
- .sort((a, b) => a - b);
35
-
36
- expect(keys).toEqual([...ZORA_COLOR_SCALE_STEPS]);
37
- }
38
-
39
- function assertNoPureBlackOrWhite(scale: ZoraColorScale) {
40
- const values = getStepValues(scale).map(({ hex }) => hex);
41
- expect(values).not.toContain('#000000');
42
- expect(values).not.toContain('#ffffff');
43
- }
44
-
45
- function buildHueRoles(assignments: ZoraComputedHueRoles['assignments']): ZoraComputedHueRoles {
46
- return {
47
- harmony: 'tetradic',
48
- assignments,
49
- };
50
- }
51
-
52
- function createCompleteHueRoles(): ZoraComputedHueRoles {
53
- return buildHueRoles([
54
- { role: 'primary', hue: 120, sourceSlotId: 'base' },
55
- { role: 'secondary', hue: 200, sourceSlotId: 'a' },
56
- { role: 'accent', hue: 280, sourceSlotId: 'b' },
57
- { role: 'highlight', hue: 20, sourceSlotId: 'c' },
58
- { role: 'surfaceTint', hue: 160, sourceSlotId: 'a' },
59
- ]);
60
- }
61
-
62
- function createScales(colorTone: ZoraColorTone = 'jewel') {
63
- return createZoraRoleColorScales({
64
- colorTone,
65
- hueRoles: createCompleteHueRoles(),
66
- seed: '#0f766e',
67
- });
68
- }
69
-
70
- function requireHueBackedSourceHue(role: ZoraRoleColorScale): number {
71
- if (typeof role.sourceHue !== 'number') {
72
- throw new Error(`[zora] Expected "${role.role}" role scale to include a sourceHue.`);
73
- }
74
- return role.sourceHue;
75
- }
76
-
77
- describe('createZoraRoleColorScales', () => {
78
- test('returns all roles exactly once in deterministic order', () => {
79
- const scales = createScales();
80
-
81
- expect(scales.roles).toHaveLength(ZORA_COLOR_SCALE_ROLE_ORDER.length);
82
- expect(scales.roles.map((entry) => entry.role)).toEqual([...ZORA_COLOR_SCALE_ROLE_ORDER]);
83
- expect(new Set(scales.roles.map((entry) => entry.role)).size).toBe(
84
- ZORA_COLOR_SCALE_ROLE_ORDER.length,
85
- );
86
- });
87
-
88
- test('every scale contains exactly all 50–950 keys and valid lowercase 6-digit hex values', () => {
89
- const scales = createScales();
90
-
91
- for (const role of scales.roles) {
92
- assertScaleKeys(role.scale);
93
- for (const { hex } of getStepValues(role.scale)) {
94
- expect(isSixDigitLowercaseHexColor(hex)).toBe(true);
95
- }
96
- }
97
- });
98
-
99
- test('output is deterministic for the same input', () => {
100
- expect(createScales()).toEqual(createScales());
101
- });
102
-
103
- test('role hue is preserved approximately for mid steps', () => {
104
- const scales = createZoraRoleColorScales({
105
- colorTone: 'jewel',
106
- hueRoles: buildHueRoles([
107
- { role: 'primary', hue: 115, sourceSlotId: 'base' },
108
- { role: 'secondary', hue: 210, sourceSlotId: 'a' },
109
- { role: 'accent', hue: 300, sourceSlotId: 'b' },
110
- { role: 'highlight', hue: 25, sourceSlotId: 'c' },
111
- { role: 'surfaceTint', hue: 165, sourceSlotId: 'a' },
112
- ]),
113
- seed: '#0f766e',
114
- });
115
-
116
- const midSteps: ZoraColorScaleStep[] = [400, 500, 600, 700];
117
- for (const roleId of ['primary', 'secondary', 'accent', 'highlight', 'surfaceTint'] as const) {
118
- const role = getZoraRoleColorScale(scales, roleId);
119
- const expectedHue = requireHueBackedSourceHue(role);
120
-
121
- for (const step of midSteps) {
122
- const oklch = parseHexToOklch(role.scale[step]);
123
- expect(hueDeltaDegrees(expectedHue, oklch.h)).toBeLessThan(25);
124
- }
125
- }
126
- });
127
-
128
- test('surfaceTint scale has lower chroma than primary/accent/highlight for mid steps', () => {
129
- const scales = createScales('jewel');
130
- const surfaceTint = getZoraRoleColorScale(scales, 'surfaceTint').scale;
131
- const primary = getZoraRoleColorScale(scales, 'primary').scale;
132
- const accent = getZoraRoleColorScale(scales, 'accent').scale;
133
- const highlight = getZoraRoleColorScale(scales, 'highlight').scale;
134
- const step: ZoraColorScaleStep = 500;
135
- const surfaceTintChroma = parseHexToOklch(surfaceTint[step]).c;
136
-
137
- expect(surfaceTintChroma).toBeLessThan(parseHexToOklch(primary[step]).c);
138
- expect(surfaceTintChroma).toBeLessThan(parseHexToOklch(accent[step]).c);
139
- expect(surfaceTintChroma).toBeLessThan(parseHexToOklch(highlight[step]).c);
140
- });
141
-
142
- test('colorTone changes role chroma behavior internally', () => {
143
- const neutral = createScales('neutral');
144
- const fluorescent = createScales('fluorescent');
145
- const step: ZoraColorScaleStep = 500;
146
-
147
- const neutralPrimary = parseHexToOklch(getZoraRoleColorScale(neutral, 'primary').scale[step]).c;
148
- const fluorescentPrimary = parseHexToOklch(
149
- getZoraRoleColorScale(fluorescent, 'primary').scale[step],
150
- ).c;
151
-
152
- expect(fluorescentPrimary).toBeGreaterThan(neutralPrimary);
153
- });
154
-
155
- test('pastel keeps surfaceTint lower chroma than fluorescent foreground roles', () => {
156
- const pastel = createScales('pastel');
157
- const fluorescent = createScales('fluorescent');
158
- const step: ZoraColorScaleStep = 500;
159
-
160
- const pastelSurfaceTint = parseHexToOklch(
161
- getZoraRoleColorScale(pastel, 'surfaceTint').scale[step],
162
- ).c;
163
- const fluorescentAccent = parseHexToOklch(
164
- getZoraRoleColorScale(fluorescent, 'accent').scale[step],
165
- ).c;
166
-
167
- expect(pastelSurfaceTint).toBeLessThan(fluorescentAccent);
168
- });
169
-
170
- test('neutral scale has low chroma', () => {
171
- const neutral = getZoraRoleColorScale(createScales(), 'neutral').scale;
172
-
173
- for (const { hex } of getStepValues(neutral)) {
174
- expect(parseHexToOklch(hex).c).toBeLessThanOrEqual(0.03);
175
- }
176
- });
177
-
178
- test('missing required hue role throws a clear error', () => {
179
- const hueRoles = buildHueRoles([
180
- { role: 'primary', hue: 120, sourceSlotId: 'base' },
181
- { role: 'secondary', hue: 200, sourceSlotId: 'a' },
182
- { role: 'accent', hue: 280, sourceSlotId: 'b' },
183
- { role: 'surfaceTint', hue: 160, sourceSlotId: 'a' },
184
- ]);
185
-
186
- expect(() =>
187
- createZoraRoleColorScales({ colorTone: 'jewel', hueRoles, seed: '#0f766e' }),
188
- ).toThrow('highlight');
189
- });
190
-
191
- test('no role scale contains pure black/white by default', () => {
192
- const scales = createScales();
193
-
194
- for (const role of scales.roles) {
195
- assertNoPureBlackOrWhite(role.scale);
196
- }
197
- });
198
-
199
- test('does not mutate input hueRoles', () => {
200
- const hueRoles = createCompleteHueRoles();
201
- const snapshot = JSON.stringify(hueRoles);
202
-
203
- createZoraRoleColorScales({ colorTone: 'jewel', hueRoles, seed: '#0f766e' });
204
-
205
- expect(JSON.stringify(hueRoles)).toBe(snapshot);
206
- });
207
-
208
- test('integration: harmony -> role hues -> role scales pipeline connects', () => {
209
- const seed: ZoraHexColor = '#0f766e';
210
- const harmony = computeZoraHarmony(seed, 'tetradic');
211
- const hueRoles = assignZoraHarmonyRoleHues(harmony);
212
-
213
- const scales = createZoraRoleColorScales({ colorTone: 'jewel', hueRoles, seed });
214
-
215
- expect(scales.roles.map((entry) => entry.role)).toEqual([...ZORA_COLOR_SCALE_ROLE_ORDER]);
216
- for (const role of scales.roles) {
217
- assertScaleKeys(role.scale);
218
- }
219
- });
220
- });
@@ -1,127 +0,0 @@
1
- import type { ZoraColorTone, ZoraHexColor } from '../../theme/types';
2
- import { getZoraColorToneRecipe } from './colorToneRecipes';
3
- import { parseHexToOklch } from './oklch';
4
- import {
5
- getZoraHueRoleAssignment,
6
- type ZoraComputedHueRoles,
7
- type ZoraHueRoleId,
8
- } from './roleHues';
9
- import {
10
- createZoraHueScale,
11
- type CreateZoraHueScaleOptions,
12
- createZoraNeutralScale,
13
- type ZoraHueScaleRoleId,
14
- } from './scales';
15
- import type { ZoraColorScale } from './types';
16
-
17
- export type ZoraColorScaleRoleId =
18
- | 'primary'
19
- | 'secondary'
20
- | 'accent'
21
- | 'highlight'
22
- | 'surfaceTint'
23
- | 'neutral';
24
-
25
- export const ZORA_COLOR_SCALE_ROLE_ORDER: readonly ZoraColorScaleRoleId[] = [
26
- 'primary',
27
- 'secondary',
28
- 'accent',
29
- 'highlight',
30
- 'surfaceTint',
31
- 'neutral',
32
- ];
33
-
34
- export interface ZoraRoleColorScale {
35
- role: ZoraColorScaleRoleId;
36
- sourceHue?: number;
37
- scale: ZoraColorScale;
38
- }
39
-
40
- export interface ZoraComputedRoleColorScales {
41
- roles: readonly ZoraRoleColorScale[];
42
- }
43
-
44
- export function getZoraRoleColorScale(
45
- scales: ZoraComputedRoleColorScales,
46
- role: ZoraColorScaleRoleId,
47
- ): ZoraRoleColorScale {
48
- const found = scales.roles.find((entry) => entry.role === role);
49
- if (!found) {
50
- throw new Error(`[zora] Expected role color scales to include "${role}".`);
51
- }
52
- return found;
53
- }
54
-
55
- function resolveSeedChroma(seed: ZoraHexColor): number {
56
- return parseHexToOklch(seed).c;
57
- }
58
-
59
- function createHueBackedRoleScale(options: {
60
- colorTone: ZoraColorTone;
61
- hueRoles: ZoraComputedHueRoles;
62
- seedChroma: number;
63
- role: ZoraHueRoleId;
64
- }): ZoraRoleColorScale {
65
- const assignment = getZoraHueRoleAssignment(options.hueRoles, options.role);
66
- const colorToneRecipe = getZoraColorToneRecipe(options.colorTone);
67
- const hueScaleRole: ZoraHueScaleRoleId = options.role;
68
- const hueScaleOptions: CreateZoraHueScaleOptions = {
69
- hue: assignment.hue,
70
- seedChroma: options.seedChroma,
71
- role: hueScaleRole,
72
- colorToneRecipe,
73
- };
74
-
75
- return {
76
- role: options.role,
77
- sourceHue: assignment.hue,
78
- scale: createZoraHueScale(hueScaleOptions),
79
- };
80
- }
81
-
82
- export function createZoraRoleColorScales(options: {
83
- hueRoles: ZoraComputedHueRoles;
84
- seed: ZoraHexColor;
85
- colorTone: ZoraColorTone;
86
- }): ZoraComputedRoleColorScales {
87
- const seedChroma = resolveSeedChroma(options.seed);
88
-
89
- const roles: ZoraRoleColorScale[] = [
90
- createHueBackedRoleScale({
91
- colorTone: options.colorTone,
92
- hueRoles: options.hueRoles,
93
- seedChroma,
94
- role: 'primary',
95
- }),
96
- createHueBackedRoleScale({
97
- colorTone: options.colorTone,
98
- hueRoles: options.hueRoles,
99
- seedChroma,
100
- role: 'secondary',
101
- }),
102
- createHueBackedRoleScale({
103
- colorTone: options.colorTone,
104
- hueRoles: options.hueRoles,
105
- seedChroma,
106
- role: 'accent',
107
- }),
108
- createHueBackedRoleScale({
109
- colorTone: options.colorTone,
110
- hueRoles: options.hueRoles,
111
- seedChroma,
112
- role: 'highlight',
113
- }),
114
- createHueBackedRoleScale({
115
- colorTone: options.colorTone,
116
- hueRoles: options.hueRoles,
117
- seedChroma,
118
- role: 'surfaceTint',
119
- }),
120
- {
121
- role: 'neutral',
122
- scale: createZoraNeutralScale(options.seed),
123
- },
124
- ];
125
-
126
- return { roles };
127
- }
@@ -1,151 +0,0 @@
1
- import { describe, expect, test } from 'bun:test';
2
-
3
- import type { ZoraHexColor } from '../../theme/types';
4
- import {
5
- createZoraColorScale,
6
- type CreateZoraColorScaleOptions,
7
- createZoraNeutralScale,
8
- createZoraPrimaryScale,
9
- parseHexToOklch,
10
- ZORA_COLOR_SCALE_STEPS,
11
- type ZoraColorScale,
12
- type ZoraColorScaleStep,
13
- } from './index';
14
-
15
- function isSixDigitLowercaseHexColor(value: string): value is ZoraHexColor {
16
- return /^#[0-9a-f]{6}$/.test(value);
17
- }
18
-
19
- function hueDeltaDegrees(a: number, b: number): number {
20
- const raw = Math.abs(a - b) % 360;
21
- return Math.min(raw, 360 - raw);
22
- }
23
-
24
- function getStepValues(scale: ZoraColorScale): { step: ZoraColorScaleStep; hex: ZoraHexColor }[] {
25
- return ZORA_COLOR_SCALE_STEPS.map((step) => ({ step, hex: scale[step] }));
26
- }
27
-
28
- function assertScaleKeys(scale: ZoraColorScale) {
29
- const keys = Object.keys(scale)
30
- .map((key) => Number(key))
31
- .sort((a, b) => a - b);
32
-
33
- expect(keys).toEqual([...ZORA_COLOR_SCALE_STEPS]);
34
- }
35
-
36
- function assertDecreasingLightness(scale: ZoraColorScale) {
37
- const lightness = getStepValues(scale).map(({ hex }) => parseHexToOklch(hex).l);
38
-
39
- for (let index = 0; index < lightness.length - 1; index += 1) {
40
- expect(lightness[index]).toBeGreaterThan(lightness[index + 1]);
41
- }
42
- }
43
-
44
- describe('createZoraPrimaryScale', () => {
45
- test('matches createZoraColorScale({ role: primary })', () => {
46
- const seed: ZoraHexColor = '#0f766e';
47
- const options: CreateZoraColorScaleOptions = { seed, role: 'primary' };
48
-
49
- expect(createZoraPrimaryScale(seed)).toEqual(createZoraColorScale(options));
50
- });
51
-
52
- test('returns all 50–950 keys and valid lowercase 6-digit hex values', () => {
53
- const seed: ZoraHexColor = '#0f766e';
54
- const scale = createZoraPrimaryScale(seed);
55
-
56
- assertScaleKeys(scale);
57
-
58
- for (const { hex } of getStepValues(scale)) {
59
- expect(isSixDigitLowercaseHexColor(hex)).toBe(true);
60
- }
61
- });
62
-
63
- test('is deterministic for the same seed', () => {
64
- const seed: ZoraHexColor = '#0f766e';
65
-
66
- expect(createZoraPrimaryScale(seed)).toEqual(createZoraPrimaryScale(seed));
67
- });
68
-
69
- test('lightness decreases from 50 to 950', () => {
70
- const seed: ZoraHexColor = '#0f766e';
71
- const scale = createZoraPrimaryScale(seed);
72
-
73
- assertDecreasingLightness(scale);
74
- });
75
-
76
- test('preserves hue approximately for mid steps', () => {
77
- const seed: ZoraHexColor = '#0f766e';
78
- const seedOklch = parseHexToOklch(seed);
79
- const scale = createZoraPrimaryScale(seed);
80
-
81
- const midSteps: ZoraColorScaleStep[] = [400, 500, 600, 700];
82
- for (const step of midSteps) {
83
- const oklch = parseHexToOklch(scale[step]);
84
- expect(hueDeltaDegrees(seedOklch.h, oklch.h)).toBeLessThan(20);
85
- }
86
- });
87
-
88
- test('mid steps have higher chroma than extremes for a saturated seed', () => {
89
- const seed: ZoraHexColor = '#ff00ff';
90
- const scale = createZoraPrimaryScale(seed);
91
-
92
- const chroma50 = parseHexToOklch(scale[50]).c;
93
- const chroma950 = parseHexToOklch(scale[950]).c;
94
- const chroma500 = parseHexToOklch(scale[500]).c;
95
- const chroma600 = parseHexToOklch(scale[600]).c;
96
-
97
- const extremes = Math.max(chroma50, chroma950);
98
- const mid = Math.max(chroma500, chroma600);
99
-
100
- expect(mid).toBeGreaterThan(extremes);
101
- });
102
-
103
- test('bounds saturated inputs to stay in gamut', () => {
104
- const seed: ZoraHexColor = '#ff00ff';
105
- const scale = createZoraPrimaryScale(seed);
106
-
107
- for (const { hex } of getStepValues(scale)) {
108
- expect(isSixDigitLowercaseHexColor(hex)).toBe(true);
109
- expect(() => parseHexToOklch(hex)).not.toThrow();
110
- }
111
- });
112
-
113
- test('throws on invalid seed in test/development', () => {
114
- const invalid: ZoraHexColor = '#nope';
115
-
116
- expect(() => createZoraPrimaryScale(invalid)).toThrow();
117
- });
118
- });
119
-
120
- describe('createZoraNeutralScale', () => {
121
- test('works with and without a seed', () => {
122
- const seeded = createZoraNeutralScale('#0f766e');
123
- const unseeded = createZoraNeutralScale();
124
-
125
- assertScaleKeys(seeded);
126
- assertScaleKeys(unseeded);
127
- });
128
-
129
- test('returns low-chroma neutrals', () => {
130
- const scale = createZoraNeutralScale('#0f766e');
131
-
132
- for (const { hex } of getStepValues(scale)) {
133
- const oklch = parseHexToOklch(hex);
134
- expect(oklch.c).toBeLessThanOrEqual(0.03);
135
- }
136
- });
137
-
138
- test('lightness decreases from 50 to 950', () => {
139
- const scale = createZoraNeutralScale('#0f766e');
140
-
141
- assertDecreasingLightness(scale);
142
- });
143
-
144
- test('avoids pure black/white in the generated steps', () => {
145
- const scale = createZoraNeutralScale('#0f766e');
146
- const values = getStepValues(scale).map(({ hex }) => hex);
147
-
148
- expect(values).not.toContain('#000000');
149
- expect(values).not.toContain('#ffffff');
150
- });
151
- });