@newtonedev/colors 0.0.1 → 1.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,53 +1,57 @@
1
- import type { Hex, Oklch, Gamut } from "../types.js";
2
- import { type ResolvedColor } from "./resolve-color.js";
1
+ import type { Hex, Oklch } from "../types.js";
3
2
  /**
4
3
  * The result of deriving scale parameters from a key color.
5
4
  *
6
- * Spread `hue` and `chroma` directly into ScaleOptions, then pass
7
- * `resolved.oklch` to findNearest to locate the color in the generated scale.
5
+ * Spread `hue` and `chroma` directly into ScaleOptions, then use
6
+ * `stepIndex` to locate the key color's position in the generated scale.
8
7
  */
9
8
  export interface KeyColorResult {
10
9
  /** Derived hue angle — use directly as ScaleOptions.hue. */
11
10
  readonly hue: number;
12
11
  /**
13
12
  * Derived chroma shape — use directly as ScaleOptions.chroma.
14
- * - `value`: how saturated the scale is (0–1, relative to gamut boundary).
15
- * - `offset`: where in the scale the key color's lightness falls (0 = lightest, 1 = darkest).
13
+ * - `amount`: how saturated the scale is (0–1, relative to gamut boundary).
14
+ * - `balance`: where in the scale the key color's lightness falls (0 = lightest, 1 = darkest).
16
15
  */
17
16
  readonly chroma: {
18
- readonly value: number;
19
- readonly offset: number;
17
+ readonly amount: number;
18
+ readonly balance: number;
20
19
  };
21
- /**
22
- * The resolved color — pass to findNearest to locate the key color
23
- * in the scale after calling generateScale.
24
- *
25
- * @example
26
- * const key = keyColor('#3B82F6', { gamut, lightest, darkest });
27
- * const scale = generateScale({ ...key, steps, grading, gradient });
28
- * const nearest = findNearest(key.resolved.oklch, scale);
29
- */
30
- readonly resolved: ResolvedColor;
20
+ /** The step index in a scale of `steps` length where this color lands. */
21
+ readonly stepIndex: number;
22
+ /** Hex representation of the resolved color (sRGB-clamped if necessary). */
23
+ readonly hex: Hex;
24
+ /** The final OKLCH color, guaranteed in-gamut for the target gamut. */
25
+ readonly oklch: Oklch;
26
+ /** Whether the input was outside the target gamut and was remapped. */
27
+ readonly wasRemapped: boolean;
28
+ /** The original OKLCH before gamut mapping (identical to oklch if in-gamut). */
29
+ readonly original: Oklch;
31
30
  }
32
31
  /**
33
- * Derive scale parameters from a key (brand) color.
32
+ * Derive scale parameters from a known color.
34
33
  *
35
34
  * Given a hex or OKLCH color, returns the `hue` and `chroma` values
36
- * ready to spread into ScaleOptions. The chroma offset is derived from
35
+ * ready to spread into ScaleOptions. The chroma balance is derived from
37
36
  * where the color's lightness falls within the scale's lightness range,
38
- * so the scale's chroma peak aligns with the key color's position.
37
+ * so the chroma distribution is centered around the key color's position.
39
38
  *
40
39
  * @param color A hex string (#RGB or #RRGGBB) or an OKLCH color object.
41
40
  * @param options Scale context — must match the ScaleOptions you will use.
41
+ * `contrast` values are slider values (0–1), same as ScaleOptions.contrast.
42
+ * `steps` is the number of steps in the scale (default: 26).
42
43
  *
43
44
  * @example
44
- * const key = keyColor('#3B82F6', { gamut: 'srgb', lightest: 0.96, darkest: 0.16 });
45
- * const scale = generateScale({ ...key, steps: 26 });
46
- * const { index } = findNearest(key.resolved.oklch, scale);
45
+ * const key = keyColor('#3B82F6');
46
+ * const scale = generateScale({ hue: key.hue, chroma: key.chroma });
47
+ * const matchedHex = oklchToHex(scale[key.stepIndex]);
47
48
  */
48
49
  export declare function keyColor(color: Hex | Oklch, options?: {
49
- readonly gamut?: Gamut;
50
- readonly lightest?: number;
51
- readonly darkest?: number;
50
+ readonly isP3?: boolean;
51
+ readonly contrast?: {
52
+ readonly light?: number;
53
+ readonly dark?: number;
54
+ };
55
+ readonly steps?: number;
52
56
  }): KeyColorResult;
53
57
  //# sourceMappingURL=key-color.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"key-color.d.ts","sourceRoot":"","sources":["../../src/scale/key-color.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAgB,KAAK,aAAa,EAAE,MAAM,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AC;IAC9B,gFAAgF;IAChF,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CACtB,KAAK,EAAE,GAAG,GAAG,KAAK,EAClB,OAAO,CAAC,EAAE;IACR,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,QAAQ,CAAC,EAAE;QAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACxE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB,GACA,cAAc,CAiBhB"}
@@ -1,4 +1,4 @@
1
- import type { Hex, Oklch, Gamut } from "../types.js";
1
+ import type { Hex, Oklch } from "../types.js";
2
2
  /**
3
3
  * Result of resolving a color into scale-ready parameters.
4
4
  */
@@ -13,9 +13,9 @@ export interface ResolvedColor {
13
13
  readonly original: Oklch;
14
14
  /** Chroma lost during gamut mapping (0 if in-gamut). */
15
15
  readonly chromaShift: number;
16
- /** Hue from the resolved color — use directly as ScaleOptions.hue. */
16
+ /** Hue from the resolved color. */
17
17
  readonly hue: number;
18
- /** Chroma as a fraction of the gamut boundary at this L/h (0–1) — use as ScaleOptions.chroma.value. */
18
+ /** Chroma as a fraction of the gamut boundary at this L/h (0–1). */
19
19
  readonly chromaRatio: number;
20
20
  }
21
21
  /**
@@ -23,33 +23,13 @@ export interface ResolvedColor {
23
23
  *
24
24
  * If the color is outside the target gamut, it is perceptually mapped to the
25
25
  * nearest in-gamut color (chroma reduction at fixed L and h). The result
26
- * includes the derived hue and chromaRatio for direct use in ScaleOptions.
26
+ * includes the derived hue and chromaRatio.
27
27
  *
28
28
  * Hex input is always valid sRGB, so `wasRemapped` will be false for hex
29
29
  * with gamut="srgb". OKLCH input may be out of gamut and will be mapped.
30
30
  *
31
31
  * @param input A hex string (#RGB, #RRGGBB) or an OKLCH color.
32
- * @param gamut Target gamut (default: "srgb").
32
+ * @param isP3 Use Display P3 gamut instead of sRGB (default: false).
33
33
  */
34
- export declare function resolveColor(input: Hex | Oklch, gamut?: Gamut): ResolvedColor;
35
- /**
36
- * Result of finding the nearest color in a scale.
37
- */
38
- export interface NearestMatch {
39
- /** Index of the nearest step in the scale (0-based). */
40
- readonly index: number;
41
- /** The nearest OKLCH color from the scale. */
42
- readonly color: Oklch;
43
- /** Perceptual distance (deltaEOK) between the target and nearest step. */
44
- readonly distance: number;
45
- }
46
- /**
47
- * Find the step in a scale perceptually closest to a target color.
48
- *
49
- * Uses deltaEOK (Euclidean distance in OKLAB) for comparison.
50
- *
51
- * @param target The color to match against.
52
- * @param scale An array of OKLCH colors (e.g. from generateScale).
53
- */
54
- export declare function findNearest(target: Oklch, scale: readonly Oklch[]): NearestMatch;
34
+ export declare function resolveColor(input: Hex | Oklch, isP3?: boolean): ResolvedColor;
55
35
  //# sourceMappingURL=resolve-color.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-color.d.ts","sourceRoot":"","sources":["../../src/scale/resolve-color.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAYrD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,uEAAuE;IACvE,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,4EAA4E;IAC5E,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,uEAAuE;IACvE,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,gFAAgF;IAChF,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC;IACzB,wDAAwD;IACxD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,sEAAsE;IACtE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,uGAAuG;IACvG,QAAQ,CAAC,WAAW,EAAE,M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1,6 @@
1
1
  {
2
2
  "name": "@newtonedev/colors",
3
- "version": "0.0.1",
3
+ "version": "1.0.0",
4
4
  "description": "Color scale engine — produces accessible color scales from declarative parameters",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",