@nightshadeui/palette-generator 2.22.0 → 3.0.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.
@@ -1,6 +1,13 @@
1
- import { PaletteScaleSpec } from './types.js';
2
- export declare const SCALE_STEPS: readonly [0, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
3
- export declare const DEFAULT_LIGHTNESS_SCALE: number[];
4
- export declare const DEFAULT_CHROMA_SCALE: number[];
5
- export declare const DEFAULT_SCALE_SPECS: PaletteScaleSpec[];
1
+ import { PaletteRampSpec } from './types.js';
2
+ export declare const RAMP_STEPS: number[];
3
+ /** Index of step 500: the step that `-contrast` is computed against. */
4
+ export declare const RAMP_ANCHOR_INDEX = 6;
5
+ /**
6
+ * Ramps the styles read by name: `base` for the canvas and the neutral tones,
7
+ * `primary` and `secondary` for brand accents (neutral controls focus in `secondary`).
8
+ */
9
+ export declare const REQUIRED_RAMPS: string[];
10
+ export declare const DEFAULT_LIGHTNESS_CURVE: number[];
11
+ export declare const DEFAULT_CHROMA_CURVE: number[];
12
+ export declare const DEFAULT_RAMPS: PaletteRampSpec[];
6
13
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,eAAO,MAAM,WAAW,oEAEd,CAAC;AAEX,eAAO,MAAM,uBAAuB,UAEnC,CAAC;AAEF,eAAO,MAAM,oBAAoB,UAEhC,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,gBAAgB,EASjD,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,eAAO,MAAM,UAAU,UAEtB,CAAC;AAEF,wEAAwE;AACxE,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAEnC;;;GAGG;AACH,eAAO,MAAM,cAAc,UAAmC,CAAC;AAE/D,eAAO,MAAM,uBAAuB,UAEnC,CAAC;AAEF,eAAO,MAAM,oBAAoB,UAEhC,CAAC;AAEF,eAAO,MAAM,aAAa,EAAE,eAAe,EA2C1C,CAAC"}
package/dist/constants.js CHANGED
@@ -1,20 +1,61 @@
1
- export const SCALE_STEPS = [
1
+ export const RAMP_STEPS = [
2
2
  0, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950,
3
3
  ];
4
- export const DEFAULT_LIGHTNESS_SCALE = [
4
+ /** Index of step 500: the step that `-contrast` is computed against. */
5
+ export const RAMP_ANCHOR_INDEX = 6;
6
+ /**
7
+ * Ramps the styles read by name: `base` for the canvas and the neutral tones,
8
+ * `primary` and `secondary` for brand accents (neutral controls focus in `secondary`).
9
+ */
10
+ export const REQUIRED_RAMPS = ['base', 'primary', 'secondary'];
11
+ export const DEFAULT_LIGHTNESS_CURVE = [
5
12
  1, 0.98, 0.94, 0.88, 0.80, 0.72, 0.64, 0.56, 0.48, 0.42, 0.36, 0.32,
6
13
  ];
7
- export const DEFAULT_CHROMA_SCALE = [
14
+ export const DEFAULT_CHROMA_CURVE = [
8
15
  0.01, 0.02, 0.03, 0.06, 0.09, 0.12, 0.12, 0.09, 0.06, 0.03, 0.02, 0.01,
9
16
  ];
10
- export const DEFAULT_SCALE_SPECS = [
11
- { name: 'base-light', hue: 320, int: 0.2, lum: 1 },
12
- { name: 'base-dark', hue: 340, int: 0.2, lum: 1, reverse: true },
13
- { name: 'primary', hue: 340, int: 1.2, lum: 1 },
14
- { name: 'secondary', hue: 172, int: 0.72, lum: 1 },
15
- { name: 'tertiary', hue: 20, int: 0.25, lum: 1 },
16
- { name: 'success', hue: 140, int: 1.2, lum: 1 },
17
- { name: 'warning', hue: 72, int: 1.33, lum: 1.12 },
18
- { name: 'danger', hue: 24, int: 1.5, lum: 0.9 },
17
+ export const DEFAULT_RAMPS = [
18
+ {
19
+ name: 'base',
20
+ hue: 320,
21
+ chroma: 0.2,
22
+ lightness: 1,
23
+ },
24
+ {
25
+ name: 'primary',
26
+ hue: 340,
27
+ chroma: 1.2,
28
+ lightness: 1,
29
+ },
30
+ {
31
+ name: 'secondary',
32
+ hue: 172,
33
+ chroma: 0.72,
34
+ lightness: 1,
35
+ },
36
+ {
37
+ name: 'tertiary',
38
+ hue: 20,
39
+ chroma: 0.25,
40
+ lightness: 1,
41
+ },
42
+ {
43
+ name: 'success',
44
+ hue: 140,
45
+ chroma: 1.2,
46
+ lightness: 1,
47
+ },
48
+ {
49
+ name: 'warning',
50
+ hue: 72,
51
+ chroma: 1.33,
52
+ lightness: 1.12,
53
+ },
54
+ {
55
+ name: 'danger',
56
+ hue: 24,
57
+ chroma: 1.5,
58
+ lightness: 0.9,
59
+ },
19
60
  ];
20
61
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,WAAW,GAAG;IACvB,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;CACjD,CAAC;AAEX,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACnC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;CACtE,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAChC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;CACzE,CAAC;AAE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ACP,MAAM,EAAE,GAAG;QACX,SAAS,EAAE,GAAG;KACjB;CACJ,CAAC"}
@@ -1,7 +1,25 @@
1
- import { PaletteScaleSpec, PaletteSpec } from './types.js';
1
+ import { PaletteRampSpec, PaletteSpec, RampStep } from './types.js';
2
+ /**
3
+ * Emits every ramp as scheme-aware CSS values: `light-dark(<light>, <dark>)`.
4
+ * The dark ramp is the light ramp mirrored, so a step keeps its meaning in both schemes:
5
+ * `0` is always the canvas end, `950` always the ink end.
6
+ * Throws when the palette lacks a ramp the styles read by name.
7
+ */
2
8
  export declare function generatePaletteOklch(palette?: PaletteSpec): Record<string, string>;
3
- export declare function generateScaleOklch(scale: PaletteScaleSpec, lightnessScale: number[], chromaScale: number[]): Record<string, string>;
4
- export declare function generateTextColor(scale: PaletteScaleSpec, lightnessScale: number[], chromaScale: number[]): "#000" | "#fff";
5
- export declare function generatePaletteCss(palette?: PaletteSpec): string;
6
- export declare function contrastColor(l: number, c: number): "#000" | "#fff";
9
+ export declare function generateRampOklch(ramp: PaletteRampSpec, palette?: PaletteSpec): Record<string, string>;
10
+ /**
11
+ * Samples one ramp along the curves, as the light scheme sees it.
12
+ */
13
+ export declare function generateRampSteps(ramp: PaletteRampSpec, palette?: PaletteSpec): RampStep[];
14
+ /**
15
+ * Emits the tone context of one ramp: `.tone-<name>` points the context ramp
16
+ * (`--tone-0` … `--tone-950`, `--tone-contrast`) at the ramp.
17
+ */
18
+ export declare function generateToneCss(name: string): string;
19
+ /**
20
+ * Emits the palette: the ramps under `selector`, then one tone context per ramp.
21
+ * With `selector` set to `null`, emits bare ramp declarations only.
22
+ */
23
+ export declare function generatePaletteCss(palette?: PaletteSpec, selector?: string | null): string;
24
+ export declare function contrastColor({ lightness, chroma }: RampStep): "#000" | "#fff";
7
25
  //# sourceMappingURL=generate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE3D,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,WAAgB,0BAS7D;AAED,wBAAgB,kBAAkB,CAC9B,KAAK,EAAE,gBAAgB,EACvB,cAAc,EAAE,MAAM,EAAE,EACxB,WAAW,EAAE,MAAM,EAAE,0BAcxB;AAED,wBAAgB,iBAAiB,CAC7B,KAAK,EAAE,gBAAgB,EACvB,cAAc,EAAE,MAAM,EAAE,EACxB,WAAW,EAAE,MAAM,EAAE,mBAKxB;AAED,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,WAAgB,UAY3D;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,mBAEjD"}
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEpE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,WAAgB,0BAW7D;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,GAAE,WAAgB,0BAgBjF;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,GAAE,WAAgB,GAAG,QAAQ,EAAE,CAW9F;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,UAM3C;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,WAAgB,EAAE,QAAQ,GAAE,MAAM,GAAG,IAAc,UAQ9F;AAED,wBAAgB,aAAa,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,QAAQ,mBAE5D"}
package/dist/generate.js CHANGED
@@ -1,47 +1,85 @@
1
- import { DEFAULT_CHROMA_SCALE, DEFAULT_LIGHTNESS_SCALE, DEFAULT_SCALE_SPECS, SCALE_STEPS, } from './constants.js';
1
+ import { DEFAULT_CHROMA_CURVE, DEFAULT_LIGHTNESS_CURVE, DEFAULT_RAMPS, RAMP_ANCHOR_INDEX, RAMP_STEPS, REQUIRED_RAMPS, } from './constants.js';
2
+ /**
3
+ * Emits every ramp as scheme-aware CSS values: `light-dark(<light>, <dark>)`.
4
+ * The dark ramp is the light ramp mirrored, so a step keeps its meaning in both schemes:
5
+ * `0` is always the canvas end, `950` always the ink end.
6
+ * Throws when the palette lacks a ramp the styles read by name.
7
+ */
2
8
  export function generatePaletteOklch(palette = {}) {
3
- const lightnessScale = palette.lightnessScale ?? DEFAULT_LIGHTNESS_SCALE;
4
- const chromaScale = palette.chromaScale ?? DEFAULT_CHROMA_SCALE;
9
+ const ramps = palette.ramps ?? DEFAULT_RAMPS;
10
+ const missing = REQUIRED_RAMPS.filter(name => !ramps.some(ramp => ramp.name === name));
11
+ if (missing.length > 0) {
12
+ throw new Error(`A palette needs these ramps, which the styles read by name: ${missing.join(', ')}.`);
13
+ }
5
14
  const out = {};
6
- for (const scale of palette.scales ?? DEFAULT_SCALE_SPECS) {
7
- const lightness = scale.reverse ? lightnessScale.toReversed() : lightnessScale;
8
- Object.assign(out, generateScaleOklch(scale, lightness, chromaScale));
15
+ for (const ramp of ramps) {
16
+ Object.assign(out, generateRampOklch(ramp, palette));
9
17
  }
10
18
  return out;
11
19
  }
12
- export function generateScaleOklch(scale, lightnessScale, chromaScale) {
13
- const { name } = scale;
14
- const variables = {};
15
- const textColor = generateTextColor(scale, lightnessScale, chromaScale);
16
- variables[`--color-${name}-text`] = textColor;
17
- for (const [index, step] of SCALE_STEPS.entries()) {
18
- const varName = `--color-${name}-${step}`;
19
- const l = scale.lum * lightnessScale[index];
20
- const c = scale.int * chromaScale[index];
21
- const oklch = `oklch(${l} ${c} ${scale.hue})`;
22
- variables[varName] = oklch;
20
+ export function generateRampOklch(ramp, palette = {}) {
21
+ const light = generateRampSteps(ramp, palette);
22
+ const dark = light.toReversed();
23
+ const variables = {
24
+ [`--color-${ramp.name}-contrast`]: schemeValue(contrastColor(light[RAMP_ANCHOR_INDEX]), contrastColor(dark[RAMP_ANCHOR_INDEX])),
25
+ };
26
+ for (const [index, { step }] of light.entries()) {
27
+ variables[`--color-${ramp.name}-${step}`] = schemeValue(formatOklch(light[index]), formatOklch(dark[index]));
23
28
  }
24
29
  return variables;
25
30
  }
26
- export function generateTextColor(scale, lightnessScale, chromaScale) {
27
- const l = scale.lum * lightnessScale[6];
28
- const c = scale.int * chromaScale[6];
29
- return contrastColor(l, c);
30
- }
31
- export function generatePaletteCss(palette = {}) {
32
- const selector = palette.cssSelector === undefined ? ':root' : palette.cssSelector;
33
- const variables = generatePaletteOklch(palette);
34
- const body = Object.entries(variables)
35
- .map(([key, value]) => ` ${key}: ${value};`)
36
- .join('\n');
37
- if (selector === false) {
38
- return Object.entries(variables)
39
- .map(([key, value]) => `${key}: ${value};`)
40
- .join('\n');
31
+ /**
32
+ * Samples one ramp along the curves, as the light scheme sees it.
33
+ */
34
+ export function generateRampSteps(ramp, palette = {}) {
35
+ const lightnessCurve = palette.lightnessCurve ?? DEFAULT_LIGHTNESS_CURVE;
36
+ const chromaCurve = palette.chromaCurve ?? DEFAULT_CHROMA_CURVE;
37
+ return RAMP_STEPS.map((step, index) => {
38
+ return {
39
+ step,
40
+ lightness: round(ramp.lightness * lightnessCurve[index]),
41
+ chroma: round(ramp.chroma * chromaCurve[index]),
42
+ hue: ramp.hue,
43
+ };
44
+ });
45
+ }
46
+ /**
47
+ * Emits the tone context of one ramp: `.tone-<name>` points the context ramp
48
+ * (`--tone-0` … `--tone-950`, `--tone-contrast`) at the ramp.
49
+ */
50
+ export function generateToneCss(name) {
51
+ const lines = [
52
+ ...RAMP_STEPS.map(step => `--tone-${step}: var(--color-${name}-${step});`),
53
+ `--tone-contrast: var(--color-${name}-contrast);`,
54
+ ];
55
+ return wrapDeclarations(`.tone-${name}`, lines);
56
+ }
57
+ /**
58
+ * Emits the palette: the ramps under `selector`, then one tone context per ramp.
59
+ * With `selector` set to `null`, emits bare ramp declarations only.
60
+ */
61
+ export function generatePaletteCss(palette = {}, selector = ':root') {
62
+ const lines = Object.entries(generatePaletteOklch(palette))
63
+ .map(([key, value]) => `${key}: ${value};`);
64
+ if (selector == null) {
65
+ return lines.join('\n');
41
66
  }
42
- return `${selector} {\n${body}\n}`;
67
+ const tones = (palette.ramps ?? DEFAULT_RAMPS).map(ramp => generateToneCss(ramp.name));
68
+ return [wrapDeclarations(selector, lines), ...tones].join('\n\n');
69
+ }
70
+ export function contrastColor({ lightness, chroma }) {
71
+ return lightness > 0.62 + 0.8 * chroma ? '#000' : '#fff';
72
+ }
73
+ function formatOklch({ lightness, chroma, hue }) {
74
+ return `oklch(${lightness} ${chroma} ${hue})`;
75
+ }
76
+ function schemeValue(light, dark) {
77
+ return light === dark ? light : `light-dark(${light}, ${dark})`;
78
+ }
79
+ function wrapDeclarations(selector, lines) {
80
+ return `${selector} {\n${lines.map(line => ` ${line}`).join('\n')}\n}`;
43
81
  }
44
- export function contrastColor(l, c) {
45
- return l > 0.62 + 0.8 * c ? '#000' : '#fff';
82
+ function round(value) {
83
+ return Math.round(value * 10000) / 10000;
46
84
  }
47
85
  //# sourceMappingURL=generate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"generate.js","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,oBAAoB,EACpB,uBAAuB,EACvB,mBAAmB,EACnB,WAAW,GACd,MAAM,gBAAgB,CAAC;AAGxB,MAAM,UAAU,oBAAoB,CAAC,UAAuB,EAAE;IAC1D,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,uBAAuB,CAAC;IACzE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,oBAAoB,CAAC;IAChE,MAAM,GAAG,GAA2B,EAAE,CAAC;IACvC,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,MAAM,IAAI,mBAAmB,EAAE,CAAC;QACxD,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d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './constants.js';
2
2
  export * from './generate.js';
3
+ export * from './schema/PaletteSpecSchema.js';
3
4
  export * from './types.js';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './constants.js';
2
2
  export * from './generate.js';
3
+ export * from './schema/PaletteSpecSchema.js';
3
4
  export * from './types.js';
4
5
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,YAAY,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { Schema } from 'airtight';
2
+ import { PaletteSpec } from '../types.js';
3
+ /**
4
+ * Decodes palette specs from untrusted JSON (files, stdin, browser storage).
5
+ */
6
+ export declare const PaletteSpecSchema: Schema<PaletteSpec>;
7
+ //# sourceMappingURL=PaletteSpecSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PaletteSpecSchema.d.ts","sourceRoot":"","sources":["../../src/schema/PaletteSpecSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C;;GAEG;AACH,eAAO,MAAM,iBAAiB,qBA2B5B,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { Schema } from 'airtight';
2
+ /**
3
+ * Decodes palette specs from untrusted JSON (files, stdin, browser storage).
4
+ */
5
+ export const PaletteSpecSchema = new Schema({
6
+ type: 'object',
7
+ properties: {
8
+ lightnessCurve: {
9
+ type: 'array',
10
+ items: { type: 'number' },
11
+ optional: true,
12
+ },
13
+ chromaCurve: {
14
+ type: 'array',
15
+ items: { type: 'number' },
16
+ optional: true,
17
+ },
18
+ ramps: {
19
+ type: 'array',
20
+ items: {
21
+ type: 'object',
22
+ properties: {
23
+ name: { type: 'string' },
24
+ hue: { type: 'number' },
25
+ chroma: { type: 'number' },
26
+ lightness: { type: 'number' },
27
+ },
28
+ },
29
+ optional: true,
30
+ },
31
+ },
32
+ });
33
+ //# sourceMappingURL=PaletteSpecSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PaletteSpecSchema.js","sourceRoot":"","sources":["../../src/schema/PaletteSpecSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAIlC;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,MAAM,CAAc;IACrD,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACR,cAAc,EAAE;YACZ,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YACzB,QAAQ,EAAE,IAAI;SACjB;QACD,WAAW,EAAE;YACT,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YACzB,QAAQ,EAAE,IAAI;SACjB;QACD,KAAK,EAAE;YACH,IAAI,EAAE,OAAO;YACb,KAAK,EAAE;gBACH,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACR,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;oBACxB,GAAG,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;oBACvB,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;oBAC1B,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;iBAChC;aACJ;YACD,QAAQ,EAAE,IAAI;SACjB;KACJ;CACJ,CAAC,CAAC"}
package/dist/types.d.ts CHANGED
@@ -1,14 +1,24 @@
1
- export interface PaletteScaleSpec {
1
+ /**
2
+ * One color ramp: a hue sampled along the shared lightness and chroma curves.
3
+ * Every ramp is also a tone: the generator emits a `.tone-<name>` context for it.
4
+ */
5
+ export interface PaletteRampSpec {
2
6
  name: string;
3
7
  hue: number;
4
- int: number;
5
- lum: number;
6
- reverse?: boolean;
8
+ /** Multiplier applied to the chroma curve (1 = curve as is). */
9
+ chroma: number;
10
+ /** Multiplier applied to the lightness curve (1 = curve as is). */
11
+ lightness: number;
7
12
  }
8
13
  export interface PaletteSpec {
9
- lightnessScale?: number[];
10
- chromaScale?: number[];
11
- cssSelector?: string | false;
12
- scales?: PaletteScaleSpec[];
14
+ lightnessCurve?: number[];
15
+ chromaCurve?: number[];
16
+ ramps?: PaletteRampSpec[];
17
+ }
18
+ export interface RampStep {
19
+ step: number;
20
+ lightness: number;
21
+ chroma: number;
22
+ hue: number;
13
23
  }
14
24
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,WAAW;IACxB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAC7B,MAAM,CAAC,EAAE,gBAAgB,EAAE,CAAC;CAC/B"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAC;IACf,mEAAmE;IACnE,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,WAAW;IACxB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,KAAK,CAAC,EAAE,eAAe,EAAE,CAAC;CAC7B;AAED,MAAM,WAAW,QAAQ;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;CACf"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nightshadeui/palette-generator",
3
- "version": "2.22.0",
3
+ "version": "3.0.0",
4
4
  "description": "OKLCH palette CSS variable generator for Nightshade",
5
5
  "author": "Boris Okunskiy",
6
6
  "license": "ISC",
@@ -19,5 +19,8 @@
19
19
  ],
20
20
  "scripts": {
21
21
  "build": "tsc"
22
+ },
23
+ "dependencies": {
24
+ "airtight": "^5.7.2"
22
25
  }
23
26
  }
package/src/constants.ts CHANGED
@@ -1,24 +1,67 @@
1
- import { PaletteScaleSpec } from './types.js';
1
+ import { PaletteRampSpec } from './types.js';
2
2
 
3
- export const SCALE_STEPS = [
3
+ export const RAMP_STEPS = [
4
4
  0, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950,
5
- ] as const;
5
+ ];
6
+
7
+ /** Index of step 500: the step that `-contrast` is computed against. */
8
+ export const RAMP_ANCHOR_INDEX = 6;
9
+
10
+ /**
11
+ * Ramps the styles read by name: `base` for the canvas and the neutral tones,
12
+ * `primary` and `secondary` for brand accents (neutral controls focus in `secondary`).
13
+ */
14
+ export const REQUIRED_RAMPS = ['base', 'primary', 'secondary'];
6
15
 
7
- export const DEFAULT_LIGHTNESS_SCALE = [
16
+ export const DEFAULT_LIGHTNESS_CURVE = [
8
17
  1, 0.98, 0.94, 0.88, 0.80, 0.72, 0.64, 0.56, 0.48, 0.42, 0.36, 0.32,
9
18
  ];
10
19
 
11
- export const DEFAULT_CHROMA_SCALE = [
20
+ export const DEFAULT_CHROMA_CURVE = [
12
21
  0.01, 0.02, 0.03, 0.06, 0.09, 0.12, 0.12, 0.09, 0.06, 0.03, 0.02, 0.01,
13
22
  ];
14
23
 
15
- export const DEFAULT_SCALE_SPECS: PaletteScaleSpec[] = [
16
- { name: 'base-light', hue: 320, int: 0.2, lum: 1 },
17
- { name: 'base-dark', hue: 340, int: 0.2, lum: 1, reverse: true },
18
- { name: 'primary', hue: 340, int: 1.2, lum: 1 },
19
- { name: 'secondary', hue: 172, int: 0.72, lum: 1 },
20
- { name: 'tertiary', hue: 20, int: 0.25, lum: 1 },
21
- { name: 'success', hue: 140, int: 1.2, lum: 1 },
22
- { name: 'warning', hue: 72, int: 1.33, lum: 1.12 },
23
- { name: 'danger', hue: 24, int: 1.5, lum: 0.9 },
24
+ export const DEFAULT_RAMPS: PaletteRampSpec[] = [
25
+ {
26
+ name: 'base',
27
+ hue: 320,
28
+ chroma: 0.2,
29
+ lightness: 1,
30
+ },
31
+ {
32
+ name: 'primary',
33
+ hue: 340,
34
+ chroma: 1.2,
35
+ lightness: 1,
36
+ },
37
+ {
38
+ name: 'secondary',
39
+ hue: 172,
40
+ chroma: 0.72,
41
+ lightness: 1,
42
+ },
43
+ {
44
+ name: 'tertiary',
45
+ hue: 20,
46
+ chroma: 0.25,
47
+ lightness: 1,
48
+ },
49
+ {
50
+ name: 'success',
51
+ hue: 140,
52
+ chroma: 1.2,
53
+ lightness: 1,
54
+ },
55
+ {
56
+ name: 'warning',
57
+ hue: 72,
58
+ chroma: 1.33,
59
+ lightness: 1.12,
60
+ },
61
+ {
62
+ name: 'danger',
63
+ hue: 24,
64
+ chroma: 1.5,
65
+ lightness: 0.9,
66
+ },
24
67
  ];
package/src/generate.ts CHANGED
@@ -1,65 +1,108 @@
1
1
  import {
2
- DEFAULT_CHROMA_SCALE,
3
- DEFAULT_LIGHTNESS_SCALE,
4
- DEFAULT_SCALE_SPECS,
5
- SCALE_STEPS,
2
+ DEFAULT_CHROMA_CURVE,
3
+ DEFAULT_LIGHTNESS_CURVE,
4
+ DEFAULT_RAMPS,
5
+ RAMP_ANCHOR_INDEX,
6
+ RAMP_STEPS,
7
+ REQUIRED_RAMPS,
6
8
  } from './constants.js';
7
- import { PaletteScaleSpec, PaletteSpec } from './types.js';
9
+ import { PaletteRampSpec, PaletteSpec, RampStep } from './types.js';
8
10
 
11
+ /**
12
+ * Emits every ramp as scheme-aware CSS values: `light-dark(<light>, <dark>)`.
13
+ * The dark ramp is the light ramp mirrored, so a step keeps its meaning in both schemes:
14
+ * `0` is always the canvas end, `950` always the ink end.
15
+ * Throws when the palette lacks a ramp the styles read by name.
16
+ */
9
17
  export function generatePaletteOklch(palette: PaletteSpec = {}) {
10
- const lightnessScale = palette.lightnessScale ?? DEFAULT_LIGHTNESS_SCALE;
11
- const chromaScale = palette.chromaScale ?? DEFAULT_CHROMA_SCALE;
18
+ const ramps = palette.ramps ?? DEFAULT_RAMPS;
19
+ const missing = REQUIRED_RAMPS.filter(name => !ramps.some(ramp => ramp.name === name));
20
+ if (missing.length > 0) {
21
+ throw new Error(`A palette needs these ramps, which the styles read by name: ${missing.join(', ')}.`);
22
+ }
12
23
  const out: Record<string, string> = {};
13
- for (const scale of palette.scales ?? DEFAULT_SCALE_SPECS) {
14
- const lightness = scale.reverse ? lightnessScale.toReversed() : lightnessScale;
15
- Object.assign(out, generateScaleOklch(scale, lightness, chromaScale));
24
+ for (const ramp of ramps) {
25
+ Object.assign(out, generateRampOklch(ramp, palette));
16
26
  }
17
27
  return out;
18
28
  }
19
29
 
20
- export function generateScaleOklch(
21
- scale: PaletteScaleSpec,
22
- lightnessScale: number[],
23
- chromaScale: number[],
24
- ) {
25
- const { name } = scale;
26
- const variables: Record<string, string> = {};
27
- const textColor = generateTextColor(scale, lightnessScale, chromaScale);
28
- variables[`--color-${name}-text`] = textColor;
29
- for (const [index, step] of SCALE_STEPS.entries()) {
30
- const varName = `--color-${name}-${step}`;
31
- const l = scale.lum * lightnessScale[index]!;
32
- const c = scale.int * chromaScale[index]!;
33
- const oklch = `oklch(${l} ${c} ${scale.hue})`;
34
- variables[varName] = oklch;
30
+ export function generateRampOklch(ramp: PaletteRampSpec, palette: PaletteSpec = {}) {
31
+ const light = generateRampSteps(ramp, palette);
32
+ const dark = light.toReversed();
33
+ const variables: Record<string, string> = {
34
+ [`--color-${ramp.name}-contrast`]: schemeValue(
35
+ contrastColor(light[RAMP_ANCHOR_INDEX]!),
36
+ contrastColor(dark[RAMP_ANCHOR_INDEX]!),
37
+ ),
38
+ };
39
+ for (const [index, { step }] of light.entries()) {
40
+ variables[`--color-${ramp.name}-${step}`] = schemeValue(
41
+ formatOklch(light[index]!),
42
+ formatOklch(dark[index]!),
43
+ );
35
44
  }
36
45
  return variables;
37
46
  }
38
47
 
39
- export function generateTextColor(
40
- scale: PaletteScaleSpec,
41
- lightnessScale: number[],
42
- chromaScale: number[],
43
- ) {
44
- const l = scale.lum * lightnessScale[6]!;
45
- const c = scale.int * chromaScale[6]!;
46
- return contrastColor(l, c);
48
+ /**
49
+ * Samples one ramp along the curves, as the light scheme sees it.
50
+ */
51
+ export function generateRampSteps(ramp: PaletteRampSpec, palette: PaletteSpec = {}): RampStep[] {
52
+ const lightnessCurve = palette.lightnessCurve ?? DEFAULT_LIGHTNESS_CURVE;
53
+ const chromaCurve = palette.chromaCurve ?? DEFAULT_CHROMA_CURVE;
54
+ return RAMP_STEPS.map((step, index) => {
55
+ return {
56
+ step,
57
+ lightness: round(ramp.lightness * lightnessCurve[index]!),
58
+ chroma: round(ramp.chroma * chromaCurve[index]!),
59
+ hue: ramp.hue,
60
+ };
61
+ });
62
+ }
63
+
64
+ /**
65
+ * Emits the tone context of one ramp: `.tone-<name>` points the context ramp
66
+ * (`--tone-0` … `--tone-950`, `--tone-contrast`) at the ramp.
67
+ */
68
+ export function generateToneCss(name: string) {
69
+ const lines = [
70
+ ...RAMP_STEPS.map(step => `--tone-${step}: var(--color-${name}-${step});`),
71
+ `--tone-contrast: var(--color-${name}-contrast);`,
72
+ ];
73
+ return wrapDeclarations(`.tone-${name}`, lines);
47
74
  }
48
75
 
49
- export function generatePaletteCss(palette: PaletteSpec = {}) {
50
- const selector = palette.cssSelector === undefined ? ':root' : palette.cssSelector;
51
- const variables = generatePaletteOklch(palette);
52
- const body = Object.entries(variables)
53
- .map(([key, value]) => ` ${key}: ${value};`)
54
- .join('\n');
55
- if (selector === false) {
56
- return Object.entries(variables)
57
- .map(([key, value]) => `${key}: ${value};`)
58
- .join('\n');
76
+ /**
77
+ * Emits the palette: the ramps under `selector`, then one tone context per ramp.
78
+ * With `selector` set to `null`, emits bare ramp declarations only.
79
+ */
80
+ export function generatePaletteCss(palette: PaletteSpec = {}, selector: string | null = ':root') {
81
+ const lines = Object.entries(generatePaletteOklch(palette))
82
+ .map(([key, value]) => `${key}: ${value};`);
83
+ if (selector == null) {
84
+ return lines.join('\n');
59
85
  }
60
- return `${selector} {\n${body}\n}`;
86
+ const tones = (palette.ramps ?? DEFAULT_RAMPS).map(ramp => generateToneCss(ramp.name));
87
+ return [wrapDeclarations(selector, lines), ...tones].join('\n\n');
88
+ }
89
+
90
+ export function contrastColor({ lightness, chroma }: RampStep) {
91
+ return lightness > 0.62 + 0.8 * chroma ? '#000' : '#fff';
92
+ }
93
+
94
+ function formatOklch({ lightness, chroma, hue }: RampStep) {
95
+ return `oklch(${lightness} ${chroma} ${hue})`;
96
+ }
97
+
98
+ function schemeValue(light: string, dark: string) {
99
+ return light === dark ? light : `light-dark(${light}, ${dark})`;
100
+ }
101
+
102
+ function wrapDeclarations(selector: string, lines: string[]) {
103
+ return `${selector} {\n${lines.map(line => ` ${line}`).join('\n')}\n}`;
61
104
  }
62
105
 
63
- export function contrastColor(l: number, c: number) {
64
- return l > 0.62 + 0.8 * c ? '#000' : '#fff';
106
+ function round(value: number) {
107
+ return Math.round(value * 10000) / 10000;
65
108
  }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './constants.js';
2
2
  export * from './generate.js';
3
+ export * from './schema/PaletteSpecSchema.js';
3
4
  export * from './types.js';
@@ -0,0 +1,35 @@
1
+ import { Schema } from 'airtight';
2
+
3
+ import { PaletteSpec } from '../types.js';
4
+
5
+ /**
6
+ * Decodes palette specs from untrusted JSON (files, stdin, browser storage).
7
+ */
8
+ export const PaletteSpecSchema = new Schema<PaletteSpec>({
9
+ type: 'object',
10
+ properties: {
11
+ lightnessCurve: {
12
+ type: 'array',
13
+ items: { type: 'number' },
14
+ optional: true,
15
+ },
16
+ chromaCurve: {
17
+ type: 'array',
18
+ items: { type: 'number' },
19
+ optional: true,
20
+ },
21
+ ramps: {
22
+ type: 'array',
23
+ items: {
24
+ type: 'object',
25
+ properties: {
26
+ name: { type: 'string' },
27
+ hue: { type: 'number' },
28
+ chroma: { type: 'number' },
29
+ lightness: { type: 'number' },
30
+ },
31
+ },
32
+ optional: true,
33
+ },
34
+ },
35
+ });
package/src/types.ts CHANGED
@@ -1,14 +1,25 @@
1
- export interface PaletteScaleSpec {
1
+ /**
2
+ * One color ramp: a hue sampled along the shared lightness and chroma curves.
3
+ * Every ramp is also a tone: the generator emits a `.tone-<name>` context for it.
4
+ */
5
+ export interface PaletteRampSpec {
2
6
  name: string;
3
7
  hue: number;
4
- int: number;
5
- lum: number;
6
- reverse?: boolean;
8
+ /** Multiplier applied to the chroma curve (1 = curve as is). */
9
+ chroma: number;
10
+ /** Multiplier applied to the lightness curve (1 = curve as is). */
11
+ lightness: number;
7
12
  }
8
13
 
9
14
  export interface PaletteSpec {
10
- lightnessScale?: number[];
11
- chromaScale?: number[];
12
- cssSelector?: string | false;
13
- scales?: PaletteScaleSpec[];
15
+ lightnessCurve?: number[];
16
+ chromaCurve?: number[];
17
+ ramps?: PaletteRampSpec[];
18
+ }
19
+
20
+ export interface RampStep {
21
+ step: number;
22
+ lightness: number;
23
+ chroma: number;
24
+ hue: number;
14
25
  }
package/dist/cli.d.ts DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
3
- //# sourceMappingURL=cli.d.ts.map
package/dist/cli.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
package/dist/cli.js DELETED
@@ -1,31 +0,0 @@
1
- #!/usr/bin/env node
2
- import { readFileSync, writeFileSync } from 'node:fs';
3
- import { Command } from 'commander';
4
- import { generatePaletteCss } from './generate.js';
5
- const program = new Command();
6
- program
7
- .name('palette-generator')
8
- .description('Emit OKLCH palette as CSS custom properties')
9
- .option('-f, --file <path>', 'read JSON palette spec from file (stdin if "-" )')
10
- .option('-o, --output <path>', 'write to file instead of stdout')
11
- .option('--raw', 'emit property lines only (no :root wrapper)')
12
- .action((opts) => {
13
- let palette = {};
14
- if (opts.file) {
15
- const raw = opts.file === '-'
16
- ? readFileSync(0, 'utf8')
17
- : readFileSync(opts.file, 'utf8');
18
- palette = JSON.parse(raw);
19
- }
20
- const css = generatePaletteCss(palette, {
21
- selector: opts.raw ? false : ':root',
22
- });
23
- if (opts.output) {
24
- writeFileSync(opts.output, css, 'utf8');
25
- }
26
- else {
27
- process.stdout.write(`${css}\n`);
28
- }
29
- });
30
- program.parse();
31
- //# sour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