@ankhorage/zora 0.12.0 → 0.12.2

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 (40) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/internal/color/harmony.d.ts +12 -0
  3. package/dist/internal/color/harmony.d.ts.map +1 -0
  4. package/dist/internal/color/harmony.js +69 -0
  5. package/dist/internal/color/harmony.js.map +1 -0
  6. package/dist/internal/color/hue.d.ts +3 -0
  7. package/dist/internal/color/hue.d.ts.map +1 -0
  8. package/dist/internal/color/hue.js +7 -0
  9. package/dist/internal/color/hue.js.map +1 -0
  10. package/dist/internal/color/index.d.ts +4 -0
  11. package/dist/internal/color/index.d.ts.map +1 -1
  12. package/dist/internal/color/index.js +4 -0
  13. package/dist/internal/color/index.js.map +1 -1
  14. package/dist/internal/color/oklch.d.ts.map +1 -1
  15. package/dist/internal/color/oklch.js +1 -4
  16. package/dist/internal/color/oklch.js.map +1 -1
  17. package/dist/internal/color/roleHues.d.ts +15 -0
  18. package/dist/internal/color/roleHues.d.ts.map +1 -0
  19. package/dist/internal/color/roleHues.js +103 -0
  20. package/dist/internal/color/roleHues.js.map +1 -0
  21. package/dist/internal/color/scales.d.ts +10 -0
  22. package/dist/internal/color/scales.d.ts.map +1 -0
  23. package/dist/internal/color/scales.js +110 -0
  24. package/dist/internal/color/scales.js.map +1 -0
  25. package/dist/internal/color/types.d.ts +4 -0
  26. package/dist/internal/color/types.d.ts.map +1 -1
  27. package/dist/internal/color/types.js +3 -1
  28. package/dist/internal/color/types.js.map +1 -1
  29. package/package.json +1 -1
  30. package/src/internal/color/harmony.test.ts +145 -0
  31. package/src/internal/color/harmony.ts +96 -0
  32. package/src/internal/color/hue.test.ts +28 -0
  33. package/src/internal/color/hue.ts +7 -0
  34. package/src/internal/color/index.ts +20 -0
  35. package/src/internal/color/oklch.ts +1 -5
  36. package/src/internal/color/roleHues.test.ts +197 -0
  37. package/src/internal/color/roleHues.ts +142 -0
  38. package/src/internal/color/scales.test.ts +151 -0
  39. package/src/internal/color/scales.ts +145 -0
  40. package/src/internal/color/types.ts +10 -0
@@ -0,0 +1,151 @@
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
+ });
@@ -0,0 +1,145 @@
1
+ import type { ZoraHexColor } from '../../theme/types';
2
+ import { clampOklchToGamut, formatOklchAsHex, parseHexToOklch } from './oklch';
3
+ import { type ZoraColorScale, type ZoraColorScaleStep } from './types';
4
+
5
+ export interface CreateZoraColorScaleOptions {
6
+ seed: ZoraHexColor;
7
+ role?: 'primary' | 'neutral';
8
+ }
9
+
10
+ const PRIMARY_LIGHTNESS_BY_STEP: Record<ZoraColorScaleStep, number> = {
11
+ 50: 0.97,
12
+ 100: 0.93,
13
+ 200: 0.86,
14
+ 300: 0.78,
15
+ 400: 0.68,
16
+ 500: 0.58,
17
+ 600: 0.5,
18
+ 700: 0.42,
19
+ 800: 0.34,
20
+ 900: 0.27,
21
+ 950: 0.2,
22
+ };
23
+
24
+ const NEUTRAL_LIGHTNESS_BY_STEP: Record<ZoraColorScaleStep, number> = {
25
+ 50: 0.98,
26
+ 100: 0.95,
27
+ 200: 0.89,
28
+ 300: 0.8,
29
+ 400: 0.68,
30
+ 500: 0.55,
31
+ 600: 0.44,
32
+ 700: 0.34,
33
+ 800: 0.25,
34
+ 900: 0.18,
35
+ 950: 0.12,
36
+ };
37
+
38
+ const PRIMARY_CHROMA_MULTIPLIER_BY_STEP: Record<ZoraColorScaleStep, number> = {
39
+ 50: 0.2,
40
+ 100: 0.3,
41
+ 200: 0.45,
42
+ 300: 0.7,
43
+ 400: 0.95,
44
+ 500: 1,
45
+ 600: 0.95,
46
+ 700: 0.85,
47
+ 800: 0.65,
48
+ 900: 0.45,
49
+ 950: 0.3,
50
+ };
51
+
52
+ const MAX_PRIMARY_SCALE_CHROMA = 0.2;
53
+ const MIN_PRIMARY_SCALE_CHROMA = 0.04;
54
+ const NEUTRAL_CHROMA = 0.012;
55
+ const DEFAULT_NEUTRAL_HUE_DEGREES = 260;
56
+
57
+ function clampNumber(value: number, min: number, max: number): number {
58
+ return Math.max(min, Math.min(value, max));
59
+ }
60
+
61
+ function resolvePrimaryScaleChroma(seedChroma: number, step: ZoraColorScaleStep): number {
62
+ const cappedSeedChroma = clampNumber(seedChroma, 0, MAX_PRIMARY_SCALE_CHROMA);
63
+ const multiplier = PRIMARY_CHROMA_MULTIPLIER_BY_STEP[step];
64
+ const scaled = cappedSeedChroma * multiplier;
65
+
66
+ const bounded = clampNumber(scaled, 0, MAX_PRIMARY_SCALE_CHROMA);
67
+ const shouldEnforceMin = step >= 300 && step <= 700 && seedChroma >= MIN_PRIMARY_SCALE_CHROMA;
68
+
69
+ return shouldEnforceMin ? Math.max(bounded, MIN_PRIMARY_SCALE_CHROMA) : bounded;
70
+ }
71
+
72
+ function createScaleEntries(options: CreateZoraColorScaleOptions): ZoraColorScale {
73
+ const seed = parseHexToOklch(options.seed);
74
+
75
+ if (options.role === 'neutral') {
76
+ const hue = typeof seed.h === 'number' ? seed.h : DEFAULT_NEUTRAL_HUE_DEGREES;
77
+
78
+ return createScaleFromRamp({
79
+ hue,
80
+ chroma: NEUTRAL_CHROMA,
81
+ lightnessByStep: NEUTRAL_LIGHTNESS_BY_STEP,
82
+ });
83
+ }
84
+
85
+ return createScaleFromRamp({
86
+ hue: seed.h,
87
+ chromaByStep: (step) => resolvePrimaryScaleChroma(seed.c, step),
88
+ lightnessByStep: PRIMARY_LIGHTNESS_BY_STEP,
89
+ });
90
+ }
91
+
92
+ interface CreateScaleFromRampOptions {
93
+ hue: number;
94
+ chroma?: number;
95
+ chromaByStep?: (step: ZoraColorScaleStep) => number;
96
+ lightnessByStep: Record<ZoraColorScaleStep, number>;
97
+ }
98
+
99
+ function createScaleColor(
100
+ options: CreateScaleFromRampOptions,
101
+ step: ZoraColorScaleStep,
102
+ ): ZoraHexColor {
103
+ const lightness = options.lightnessByStep[step];
104
+ const chroma =
105
+ typeof options.chromaByStep === 'function' ? options.chromaByStep(step) : (options.chroma ?? 0);
106
+
107
+ const clamped = clampOklchToGamut({
108
+ l: clampNumber(lightness, 0, 1),
109
+ c: clampNumber(chroma, 0, 1),
110
+ h: options.hue,
111
+ });
112
+
113
+ return formatOklchAsHex(clamped);
114
+ }
115
+
116
+ function createScaleFromRamp(options: CreateScaleFromRampOptions): ZoraColorScale {
117
+ return {
118
+ 50: createScaleColor(options, 50),
119
+ 100: createScaleColor(options, 100),
120
+ 200: createScaleColor(options, 200),
121
+ 300: createScaleColor(options, 300),
122
+ 400: createScaleColor(options, 400),
123
+ 500: createScaleColor(options, 500),
124
+ 600: createScaleColor(options, 600),
125
+ 700: createScaleColor(options, 700),
126
+ 800: createScaleColor(options, 800),
127
+ 900: createScaleColor(options, 900),
128
+ 950: createScaleColor(options, 950),
129
+ };
130
+ }
131
+
132
+ export function createZoraColorScale(options: CreateZoraColorScaleOptions): ZoraColorScale {
133
+ return createScaleEntries({
134
+ seed: options.seed,
135
+ role: options.role,
136
+ });
137
+ }
138
+
139
+ export function createZoraPrimaryScale(seed: ZoraHexColor): ZoraColorScale {
140
+ return createZoraColorScale({ seed, role: 'primary' });
141
+ }
142
+
143
+ export function createZoraNeutralScale(seed: ZoraHexColor = '#94a3b8'): ZoraColorScale {
144
+ return createZoraColorScale({ seed, role: 'neutral' });
145
+ }
@@ -1,5 +1,15 @@
1
+ import type { ZoraHexColor } from '../../theme/types';
2
+
1
3
  export interface ZoraOklchColor {
2
4
  l: number;
3
5
  c: number;
4
6
  h: number;
5
7
  }
8
+
9
+ export const ZORA_COLOR_SCALE_STEPS = [
10
+ 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950,
11
+ ] as const;
12
+
13
+ export type ZoraColorScaleStep = (typeof ZORA_COLOR_SCALE_STEPS)[number];
14
+
15
+ export type ZoraColorScale = Record<ZoraColorScaleStep, ZoraHexColor>;