@nightshadeui/palette-generator 2.22.0 → 3.1.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,17 @@
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
+ /** Ramp names the generator won't take: `inverse` is the mirrored `base` tone the styles define. */
11
+ export declare const RESERVED_RAMPS: string[];
12
+ /** Ramp names are lowercase kebab-case, so each one makes a valid `.tone-<name>` class. */
13
+ export declare const RAMP_NAME_PATTERN = "^[a-z][a-z0-9]*(-[a-z0-9]+)*$";
14
+ export declare const DEFAULT_LIGHTNESS_CURVE: number[];
15
+ export declare const DEFAULT_CHROMA_CURVE: number[];
16
+ export declare const DEFAULT_RAMPS: PaletteRampSpec[];
6
17
  //# 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,oGAAoG;AACpG,eAAO,MAAM,cAAc,UAAc,CAAC;AAE1C,2FAA2F;AAC3F,eAAO,MAAM,iBAAiB,kCAAkC,CAAC;AAEjE,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,65 @@
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
+ /** Ramp names the generator won't take: `inverse` is the mirrored `base` tone the styles define. */
12
+ export const RESERVED_RAMPS = ['inverse'];
13
+ /** Ramp names are lowercase kebab-case, so each one makes a valid `.tone-<name>` class. */
14
+ export const RAMP_NAME_PATTERN = '^[a-z][a-z0-9]*(-[a-z0-9]+)*$';
15
+ export const DEFAULT_LIGHTNESS_CURVE = [
5
16
  1, 0.98, 0.94, 0.88, 0.80, 0.72, 0.64, 0.56, 0.48, 0.42, 0.36, 0.32,
6
17
  ];
7
- export const DEFAULT_CHROMA_SCALE = [
18
+ export const DEFAULT_CHROMA_CURVE = [
8
19
  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
20
  ];
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 },
21
+ export const DEFAULT_RAMPS = [
22
+ {
23
+ name: 'base',
24
+ hue: 320,
25
+ chroma: 0.2,
26
+ lightness: 1,
27
+ },
28
+ {
29
+ name: 'primary',
30
+ hue: 340,
31
+ chroma: 1.2,
32
+ lightness: 1,
33
+ },
34
+ {
35
+ name: 'secondary',
36
+ hue: 172,
37
+ chroma: 0.72,
38
+ lightness: 1,
39
+ },
40
+ {
41
+ name: 'tertiary',
42
+ hue: 20,
43
+ chroma: 0.25,
44
+ lightness: 1,
45
+ },
46
+ {
47
+ name: 'success',
48
+ hue: 140,
49
+ chroma: 1.2,
50
+ lightness: 1,
51
+ },
52
+ {
53
+ name: 'warning',
54
+ hue: 72,
55
+ chroma: 1.33,
56
+ lightness: 1.12,
57
+ },
58
+ {
59
+ name: 'danger',
60
+ hue: 24,
61
+ chroma: 1.5,
62
+ lightness: 0.9,
63
+ },
19
64
  ];
20
65
  //# 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,EA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AAM;QACZ,GAAG,EAAE,GAAG;QACR,MAAM,EAAE,GAAG;QACX,SAAS,EAAE,CAAC;KACf;IACD;QACI,IAAI,EAAE,SAAS;QACf,GAAG,EAAE,GAAG;QACR,MAAM,EAAE,GAAG;QACX,SAAS,EAAE,CAAC;KACf;IACD;QACI,IAAI,EAAE,WAAW;QACjB,GAAG,EAAE,GAAG;QACR,MAAM,EAAE,IAAI;QACZ,SAAS,EAAE,CAAC;KACf;IACD;QACI,IAAI,EAAE,UAAU;QAChB,GAAG,EAAE,EAAE;QACP,MAAM,EAAE,IAAI;QACZ,SAAS,EAAE,CAAC;KACf;IACD;QACI,IAAI,EAAE,SAAS;QACf,GAAG,EAAE,GAAG;QACR,MAAM,EAAE,GAAG;QACX,SAAS,EAAE,CAAC;KACf;IACD;QACI,IAAI,EAAE,SAAS;QACf,GAAG,EAAE,EAAE;QACP,MAAM,EAAE,IAAI;QACZ,SAAS,EAAE,IAAI;KAClB;IACD;QACI,IAAI,EAAE,QAAQ;QACd,GAAG,EAAE,EAAE;QACP,MAAM,EAAE,GAAG;QACX,SAAS,EAAE,GAAG;KACjB;CACJ,CAAC"}
@@ -1,7 +1,26 @@
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, names a ramp twice or takes a reserved name,
7
+ * or has a curve without one value per step.
8
+ */
2
9
  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";
10
+ export declare function generateRampOklch(ramp: PaletteRampSpec, palette?: PaletteSpec): Record<string, string>;
11
+ /**
12
+ * Samples one ramp along the curves, as the light scheme sees it.
13
+ */
14
+ export declare function generateRampSteps(ramp: PaletteRampSpec, palette?: PaletteSpec): RampStep[];
15
+ /**
16
+ * Emits the tone context of one ramp: `.tone-<name>` points the context ramp
17
+ * (`--tone-0` … `--tone-950`, `--tone-contrast`) at the ramp.
18
+ */
19
+ export declare function generateToneCss(name: string): string;
20
+ /**
21
+ * Emits the palette: the ramps under `selector`, then one tone context per ramp.
22
+ * With `selector` set to `null`, emits bare ramp declarations only.
23
+ */
24
+ export declare function generatePaletteCss(palette?: PaletteSpec, selector?: string | null): string;
25
+ export declare function contrastColor({ lightness, chroma }: RampStep): "#000" | "#fff";
7
26
  //# 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":"AASA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEpE;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,WAAgB,0BAQ7D;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,101 @@
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, RESERVED_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, names a ramp twice or takes a reserved name,
7
+ * or has a curve without one value per step.
8
+ */
2
9
  export function generatePaletteOklch(palette = {}) {
3
- const lightnessScale = palette.lightnessScale ?? DEFAULT_LIGHTNESS_SCALE;
4
- const chromaScale = palette.chromaScale ?? DEFAULT_CHROMA_SCALE;
10
+ const ramps = palette.ramps ?? DEFAULT_RAMPS;
11
+ checkPalette(palette, ramps.map(ramp => ramp.name));
5
12
  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));
13
+ for (const ramp of ramps) {
14
+ Object.assign(out, generateRampOklch(ramp, palette));
9
15
  }
10
16
  return out;
11
17
  }
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;
18
+ export function generateRampOklch(ramp, palette = {}) {
19
+ const light = generateRampSteps(ramp, palette);
20
+ const dark = light.toReversed();
21
+ const variables = {
22
+ [`--color-${ramp.name}-contrast`]: schemeValue(contrastColor(light[RAMP_ANCHOR_INDEX]), contrastColor(dark[RAMP_ANCHOR_INDEX])),
23
+ };
24
+ for (const [index, { step }] of light.entries()) {
25
+ variables[`--color-${ramp.name}-${step}`] = schemeValue(formatOklch(light[index]), formatOklch(dark[index]));
23
26
  }
24
27
  return variables;
25
28
  }
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');
29
+ /**
30
+ * Samples one ramp along the curves, as the light scheme sees it.
31
+ */
32
+ export function generateRampSteps(ramp, palette = {}) {
33
+ const lightnessCurve = palette.lightnessCurve ?? DEFAULT_LIGHTNESS_CURVE;
34
+ const chromaCurve = palette.chromaCurve ?? DEFAULT_CHROMA_CURVE;
35
+ return RAMP_STEPS.map((step, index) => {
36
+ return {
37
+ step,
38
+ lightness: round(ramp.lightness * lightnessCurve[index]),
39
+ chroma: round(ramp.chroma * chromaCurve[index]),
40
+ hue: ramp.hue,
41
+ };
42
+ });
43
+ }
44
+ /**
45
+ * Emits the tone context of one ramp: `.tone-<name>` points the context ramp
46
+ * (`--tone-0` … `--tone-950`, `--tone-contrast`) at the ramp.
47
+ */
48
+ export function generateToneCss(name) {
49
+ const lines = [
50
+ ...RAMP_STEPS.map(step => `--tone-${step}: var(--color-${name}-${step});`),
51
+ `--tone-contrast: var(--color-${name}-contrast);`,
52
+ ];
53
+ return wrapDeclarations(`.tone-${name}`, lines);
54
+ }
55
+ /**
56
+ * Emits the palette: the ramps under `selector`, then one tone context per ramp.
57
+ * With `selector` set to `null`, emits bare ramp declarations only.
58
+ */
59
+ export function generatePaletteCss(palette = {}, selector = ':root') {
60
+ const lines = Object.entries(generatePaletteOklch(palette))
61
+ .map(([key, value]) => `${key}: ${value};`);
62
+ if (selector == null) {
63
+ return lines.join('\n');
41
64
  }
42
- return `${selector} {\n${body}\n}`;
65
+ const tones = (palette.ramps ?? DEFAULT_RAMPS).map(ramp => generateToneCss(ramp.name));
66
+ return [wrapDeclarations(selector, lines), ...tones].join('\n\n');
67
+ }
68
+ export function contrastColor({ lightness, chroma }) {
69
+ return lightness > 0.62 + 0.8 * chroma ? '#000' : '#fff';
70
+ }
71
+ function formatOklch({ lightness, chroma, hue }) {
72
+ return `oklch(${lightness} ${chroma} ${hue})`;
73
+ }
74
+ function schemeValue(light, dark) {
75
+ return light === dark ? light : `light-dark(${light}, ${dark})`;
76
+ }
77
+ function wrapDeclarations(selector, lines) {
78
+ return `${selector} {\n${lines.map(line => ` ${line}`).join('\n')}\n}`;
43
79
  }
44
- export function contrastColor(l, c) {
45
- return l > 0.62 + 0.8 * c ? '#000' : '#fff';
80
+ function round(value) {
81
+ return Math.round(value * 10000) / 10000;
82
+ }
83
+ function checkPalette(palette, names) {
84
+ const missing = REQUIRED_RAMPS.filter(name => !names.includes(name));
85
+ if (missing.length > 0) {
86
+ throw new Error(`A palette needs these ramps, which the styles read by name: ${missing.join(', ')}.`);
87
+ }
88
+ const repeated = names.filter((name, index) => names.indexOf(name) !== index);
89
+ if (repeated.length > 0) {
90
+ throw new Error(`A palette names each ramp once: ${repeated.join(', ')}.`);
91
+ }
92
+ const reserved = names.filter(name => RESERVED_RAMPS.includes(name));
93
+ if (reserved.length > 0) {
94
+ throw new Error(`These ramp names are reserved: ${reserved.join(', ')}.`);
95
+ }
96
+ const curves = [palette.lightnessCurve, palette.chromaCurve];
97
+ if (curves.some(curve => curve && curve.length !== RAMP_STEPS.length)) {
98
+ throw new Error(`A palette's curves have one value per step: ${RAMP_STEPS.length} values each.`);
99
+ }
46
100
  }
47
101
  //# 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,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,cAAc,CAAC,UAAU,E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,CAAC,GAAG,CAAC,CAAC;IAC1G,CAAC;IACD,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,CAAC;IAC9E,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,mCAAmC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC/E,CAAC;IACD,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;IACrE,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,kCAAkC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC9E,CAAC;IACD,MAAM,MAAM,GAAG,CAAC,OAAO,CAAC,cAAc,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAC7D,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,KAAK,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QACpE,MAAM,IAAI,KAAK,CAAC,+CAA+C,UAAU,CAAC,MAAM,eAAe,CAAC,CAAC;IACrG,CAAC;AACL,CAAC"}
package/dist/index.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,8 @@
1
+ import { Schema } from 'airtight';
2
+ import { PaletteSpec } from '../types.js';
3
+ /**
4
+ * Decodes palette specs from untrusted JSON (files, stdin, browser storage): shapes, ramp names and ranges.
5
+ * What spans the whole spec (curve lengths, unique, required and reserved names) the generator checks.
6
+ */
7
+ export declare const PaletteSpecSchema: Schema<PaletteSpec>;
8
+ //# 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;AAGlC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C;;;GAGG;AACH,eAAO,MAAM,iBAAiB,qBAwC5B,CAAC"}
@@ -0,0 +1,48 @@
1
+ import { Schema } from 'airtight';
2
+ import { RAMP_NAME_PATTERN } from '../constants.js';
3
+ /**
4
+ * Decodes palette specs from untrusted JSON (files, stdin, browser storage): shapes, ramp names and ranges.
5
+ * What spans the whole spec (curve lengths, unique, required and reserved names) the generator checks.
6
+ */
7
+ export const PaletteSpecSchema = new Schema({
8
+ type: 'object',
9
+ properties: {
10
+ lightnessCurve: {
11
+ type: 'array',
12
+ items: { type: 'number' },
13
+ optional: true,
14
+ },
15
+ chromaCurve: {
16
+ type: 'array',
17
+ items: { type: 'number' },
18
+ optional: true,
19
+ },
20
+ ramps: {
21
+ type: 'array',
22
+ items: {
23
+ type: 'object',
24
+ properties: {
25
+ name: {
26
+ type: 'string',
27
+ regex: RAMP_NAME_PATTERN,
28
+ },
29
+ hue: {
30
+ type: 'number',
31
+ minimum: 0,
32
+ maximum: 360,
33
+ },
34
+ chroma: {
35
+ type: 'number',
36
+ minimum: 0,
37
+ },
38
+ lightness: {
39
+ type: 'number',
40
+ minimum: 0,
41
+ },
42
+ },
43
+ },
44
+ optional: true,
45
+ },
46
+ },
47
+ });
48
+ //# 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;AAElC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAGpD;;;GAGG;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;wBACF,IAAI,EAAE,QAAQ;wBACd,KAAK,EAAE,iBAAiB;qBAC3B;oBACD,GAAG,EAAE;wBACD,IAAI,EAAE,QAAQ;wBACd,OAAO,EAAE,CAAC;wBACV,OAAO,EAAE,GAAG;qBACf;oBACD,MAAM,EAAE;wBACJ,IAAI,EAAE,QAAQ;wBACd,OAAO,EAAE,CAAC;qBACb;oBACD,SAAS,EAAE;wBACP,IAAI,EAAE,QAAQ;wBACd,OAAO,EAAE,CAAC;qBACb;iBACJ;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.1.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,73 @@
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'];
15
+
16
+ /** Ramp names the generator won't take: `inverse` is the mirrored `base` tone the styles define. */
17
+ export const RESERVED_RAMPS = ['inverse'];
18
+
19
+ /** Ramp names are lowercase kebab-case, so each one makes a valid `.tone-<name>` class. */
20
+ export const RAMP_NAME_PATTERN = '^[a-z][a-z0-9]*(-[a-z0-9]+)*$';
6
21
 
7
- export const DEFAULT_LIGHTNESS_SCALE = [
22
+ export const DEFAULT_LIGHTNESS_CURVE = [
8
23
  1, 0.98, 0.94, 0.88, 0.80, 0.72, 0.64, 0.56, 0.48, 0.42, 0.36, 0.32,
9
24
  ];
10
25
 
11
- export const DEFAULT_CHROMA_SCALE = [
26
+ export const DEFAULT_CHROMA_CURVE = [
12
27
  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
28
  ];
14
29
 
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 },
30
+ export const DEFAULT_RAMPS: PaletteRampSpec[] = [
31
+ {
32
+ name: 'base',
33
+ hue: 320,
34
+ chroma: 0.2,
35
+ lightness: 1,
36
+ },
37
+ {
38
+ name: 'primary',
39
+ hue: 340,
40
+ chroma: 1.2,
41
+ lightness: 1,
42
+ },
43
+ {
44
+ name: 'secondary',
45
+ hue: 172,
46
+ chroma: 0.72,
47
+ lightness: 1,
48
+ },
49
+ {
50
+ name: 'tertiary',
51
+ hue: 20,
52
+ chroma: 0.25,
53
+ lightness: 1,
54
+ },
55
+ {
56
+ name: 'success',
57
+ hue: 140,
58
+ chroma: 1.2,
59
+ lightness: 1,
60
+ },
61
+ {
62
+ name: 'warning',
63
+ hue: 72,
64
+ chroma: 1.33,
65
+ lightness: 1.12,
66
+ },
67
+ {
68
+ name: 'danger',
69
+ hue: 24,
70
+ chroma: 1.5,
71
+ lightness: 0.9,
72
+ },
24
73
  ];
package/src/generate.ts CHANGED
@@ -1,65 +1,126 @@
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,
8
+ RESERVED_RAMPS,
6
9
  } from './constants.js';
7
- import { PaletteScaleSpec, PaletteSpec } from './types.js';
10
+ import { PaletteRampSpec, PaletteSpec, RampStep } from './types.js';
8
11
 
12
+ /**
13
+ * Emits every ramp as scheme-aware CSS values: `light-dark(<light>, <dark>)`.
14
+ * The dark ramp is the light ramp mirrored, so a step keeps its meaning in both schemes:
15
+ * `0` is always the canvas end, `950` always the ink end.
16
+ * Throws when the palette lacks a ramp the styles read by name, names a ramp twice or takes a reserved name,
17
+ * or has a curve without one value per step.
18
+ */
9
19
  export function generatePaletteOklch(palette: PaletteSpec = {}) {
10
- const lightnessScale = palette.lightnessScale ?? DEFAULT_LIGHTNESS_SCALE;
11
- const chromaScale = palette.chromaScale ?? DEFAULT_CHROMA_SCALE;
20
+ const ramps = palette.ramps ?? DEFAULT_RAMPS;
21
+ checkPalette(palette, ramps.map(ramp => ramp.name));
12
22
  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));
23
+ for (const ramp of ramps) {
24
+ Object.assign(out, generateRampOklch(ramp, palette));
16
25
  }
17
26
  return out;
18
27
  }
19
28
 
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;
29
+ export function generateRampOklch(ramp: PaletteRampSpec, palette: PaletteSpec = {}) {
30
+ const light = generateRampSteps(ramp, palette);
31
+ const dark = light.toReversed();
32
+ const variables: Record<string, string> = {
33
+ [`--color-${ramp.name}-contrast`]: schemeValue(
34
+ contrastColor(light[RAMP_ANCHOR_INDEX]!),
35
+ contrastColor(dark[RAMP_ANCHOR_INDEX]!),
36
+ ),
37
+ };
38
+ for (const [index, { step }] of light.entries()) {
39
+ variables[`--color-${ramp.name}-${step}`] = schemeValue(
40
+ formatOklch(light[index]!),
41
+ formatOklch(dark[index]!),
42
+ );
35
43
  }
36
44
  return variables;
37
45
  }
38
46
 
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);
47
+ /**
48
+ * Samples one ramp along the curves, as the light scheme sees it.
49
+ */
50
+ export function generateRampSteps(ramp: PaletteRampSpec, palette: PaletteSpec = {}): RampStep[] {
51
+ const lightnessCurve = palette.lightnessCurve ?? DEFAULT_LIGHTNESS_CURVE;
52
+ const chromaCurve = palette.chromaCurve ?? DEFAULT_CHROMA_CURVE;
53
+ return RAMP_STEPS.map((step, index) => {
54
+ return {
55
+ step,
56
+ lightness: round(ramp.lightness * lightnessCurve[index]!),
57
+ chroma: round(ramp.chroma * chromaCurve[index]!),
58
+ hue: ramp.hue,
59
+ };
60
+ });
47
61
  }
48
62
 
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');
63
+ /**
64
+ * Emits the tone context of one ramp: `.tone-<name>` points the context ramp
65
+ * (`--tone-0` … `--tone-950`, `--tone-contrast`) at the ramp.
66
+ */
67
+ export function generateToneCss(name: string) {
68
+ const lines = [
69
+ ...RAMP_STEPS.map(step => `--tone-${step}: var(--color-${name}-${step});`),
70
+ `--tone-contrast: var(--color-${name}-contrast);`,
71
+ ];
72
+ return wrapDeclarations(`.tone-${name}`, lines);
73
+ }
74
+
75
+ /**
76
+ * Emits the palette: the ramps under `selector`, then one tone context per ramp.
77
+ * With `selector` set to `null`, emits bare ramp declarations only.
78
+ */
79
+ export function generatePaletteCss(palette: PaletteSpec = {}, selector: string | null = ':root') {
80
+ const lines = Object.entries(generatePaletteOklch(palette))
81
+ .map(([key, value]) => `${key}: ${value};`);
82
+ if (selector == null) {
83
+ return lines.join('\n');
59
84
  }
60
- return `${selector} {\n${body}\n}`;
85
+ const tones = (palette.ramps ?? DEFAULT_RAMPS).map(ramp => generateToneCss(ramp.name));
86
+ return [wrapDeclarations(selector, lines), ...tones].join('\n\n');
87
+ }
88
+
89
+ export function contrastColor({ lightness, chroma }: RampStep) {
90
+ return lightness > 0.62 + 0.8 * chroma ? '#000' : '#fff';
91
+ }
92
+
93
+ function formatOklch({ lightness, chroma, hue }: RampStep) {
94
+ return `oklch(${lightness} ${chroma} ${hue})`;
95
+ }
96
+
97
+ function schemeValue(light: string, dark: string) {
98
+ return light === dark ? light : `light-dark(${light}, ${dark})`;
61
99
  }
62
100
 
63
- export function contrastColor(l: number, c: number) {
64
- return l > 0.62 + 0.8 * c ? '#000' : '#fff';
101
+ function wrapDeclarations(selector: string, lines: string[]) {
102
+ return `${selector} {\n${lines.map(line => ` ${line}`).join('\n')}\n}`;
103
+ }
104
+
105
+ function round(value: number) {
106
+ return Math.round(value * 10000) / 10000;
107
+ }
108
+
109
+ function checkPalette(palette: PaletteSpec, names: string[]) {
110
+ const missing = REQUIRED_RAMPS.filter(name => !names.includes(name));
111
+ if (missing.length > 0) {
112
+ throw new Error(`A palette needs these ramps, which the styles read by name: ${missing.join(', ')}.`);
113
+ }
114
+ const repeated = names.filter((name, index) => names.indexOf(name) !== index);
115
+ if (repeated.length > 0) {
116
+ throw new Error(`A palette names each ramp once: ${repeated.join(', ')}.`);
117
+ }
118
+ const reserved = names.filter(name => RESERVED_RAMPS.includes(name));
119
+ if (reserved.length > 0) {
120
+ throw new Error(`These ramp names are reserved: ${reserved.join(', ')}.`);
121
+ }
122
+ const curves = [palette.lightnessCurve, palette.chromaCurve];
123
+ if (curves.some(curve => curve && curve.length !== RAMP_STEPS.length)) {
124
+ throw new Error(`A palette's curves have one value per step: ${RAMP_STEPS.length} values each.`);
125
+ }
65
126
  }
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,50 @@
1
+ import { Schema } from 'airtight';
2
+
3
+ import { RAMP_NAME_PATTERN } from '../constants.js';
4
+ import { PaletteSpec } from '../types.js';
5
+
6
+ /**
7
+ * Decodes palette specs from untrusted JSON (files, stdin, browser storage): shapes, ramp names and ranges.
8
+ * What spans the whole spec (curve lengths, unique, required and reserved names) the generator checks.
9
+ */
10
+ export const PaletteSpecSchema = new Schema<PaletteSpec>({
11
+ type: 'object',
12
+ properties: {
13
+ lightnessCurve: {
14
+ type: 'array',
15
+ items: { type: 'number' },
16
+ optional: true,
17
+ },
18
+ chromaCurve: {
19
+ type: 'array',
20
+ items: { type: 'number' },
21
+ optional: true,
22
+ },
23
+ ramps: {
24
+ type: 'array',
25
+ items: {
26
+ type: 'object',
27
+ properties: {
28
+ name: {
29
+ type: 'string',
30
+ regex: RAMP_NAME_PATTERN,
31
+ },
32
+ hue: {
33
+ type: 'number',
34
+ minimum: 0,
35
+ maximum: 360,
36
+ },
37
+ chroma: {
38
+ type: 'number',
39
+ minimum: 0,
40
+ },
41
+ lightness: {
42
+ type: 'number',
43
+ minimum: 0,
44
+ },
45
+ },
46
+ },
47
+ optional: true,
48
+ },
49
+ },
50
+ });
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
- //# sourceMappingURL=cli.js.map
package/dist/cli.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],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