saturon 0.2.1 → 0.2.3

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.
package/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
- Copyright © 2025 Ganemede Labs
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
7
- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright © 2025 Ganemede Labs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,119 +1,121 @@
1
- # Saturon
2
-
3
- ![npm](https://img.shields.io/npm/v/saturon)
4
- ![npm](https://img.shields.io/npm/dw/saturon)
5
- ![License](https://img.shields.io/npm/l/saturon)
6
-
7
- A runtime-extensible JavaScript library for parsing, converting, and manipulating colors with full CSS spec support.
8
-
9
- ## 📋 Table of Contents
10
-
11
- - [Features](#-features)
12
- - [Installation](#-installation)
13
- - [Usage](#-usage)
14
- - [Examples](#-examples)
15
- - [Documentation](#-documentation)
16
- - [License](#-license)
17
- - [Contact](#-contact)
18
-
19
- ## ✨ Features
20
-
21
- - **Full CSS Color 4/5 Parsing**
22
- - Infinite nested color functions (e.g. `color-mix(...)` inside `light-dark(...)`)
23
- - Converts between all modern color spaces (OKLab, Display-P3, Rec.2020, etc.)
24
- - High-precision color math for serious colorimetry
25
- - Powerful plugin system for custom color spaces and functions
26
- - Supports complex color syntaxes like `color(from hsl(240 none calc(-infinity) / 0.5) display-p3 r calc(g + b) 100 / alpha)`
27
-
28
- ## 🔧 Installation
29
-
30
- ```bash
31
- npm install saturon
32
- ```
33
-
34
- ## 🚀 Usage
35
-
36
- ```js
37
- import { Color } from "saturon";
38
-
39
- // Parse any CSS color string
40
- const color = Color.from("#1481b8ff");
41
-
42
- // Access coordinates
43
- console.log(color.toArray()); // → [20, 129, 184, 1]
44
-
45
- // Convert to another format
46
- console.log(color.to("oklch", { units: true })); // → "oklch(0.57368 0.12258 238.41345deg)"
47
-
48
- // Access values in another color space
49
- console.log(color.in("lab").toObject({ precision: 1 })); // → { l: 50.5, a: -13.9, b: -37.6, alpha: 1 }
50
-
51
- // Modify components
52
- const modified = color.in("hsl").with({ l: (l) => l * 1.2 });
53
- console.log(modified.toString({ legacy: true })); // "hsl(200, 80%, 48%)"
54
- ```
55
-
56
- ## 💡 Examples
57
-
58
- ### Converting Colors
59
-
60
- ```js
61
- const color = Color.from("hsl(337 100% 60%)");
62
- console.log(color.to("rgb")); // → rgb(255 51 129)
63
- console.log(color.to("hex-color")); // → #ff3381
64
- ```
65
-
66
- ### Manipulating Components
67
-
68
- ```js
69
- const color = Color.from("hwb(255 7% 1%)");
70
- const hwb = color.with({ h: 100, b: (b) => b * 20 });
71
- console.log(hwb.toString()); // hwb(100 7 20)
72
- ```
73
-
74
- ### Mixing Colors
75
-
76
- ```js
77
- const red = Color.from("hsl(0, 100%, 50%)");
78
- const mixed = red.mix("hsl(120, 100%, 50%)");
79
- console.log(mixed.toString()); // hsl(60 100 50)
80
- ```
81
-
82
- ### New Named Color Registration
83
-
84
- ```js
85
- registerNamedColor("sunsetblush", [255, 94, 77]);
86
- const rgb = Color.from("rgb(255, 94, 77)");
87
- console.log(rgb.to("named-color")); // sunsetblush
88
- ```
89
-
90
- ### New Color Function Registration
91
-
92
- ```js
93
- const converter = {
94
- components: {
95
- i: { index: 0, value: [0, 1] },
96
- ct: { index: 1, value: [-1, 1] },
97
- cp: { index: 2, value: [-1, 1] },
98
- },
99
- bridge: "rgb",
100
- toBridge: (ictcp: number[]) => [/* r, g, b */],
101
- fromBridge: (rgb: number[]) => [/* i, ct, cp */],
102
- };
103
-
104
- registerColorFunction("ictcp", converter);
105
- const ictcp = Color.from("ictcp(0.2 0.2 -0.1)");
106
- console.log(ictcp.to("rgb")); // → rgb(6 7 90)
107
- ```
108
-
109
- ## 📚 Documentation
110
-
111
- Full documentation is available at [saturon.js.org](https://saturon.js.org).
112
-
113
- ## 📜 License
114
-
115
- This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
116
-
117
- ## 📧 Contact
118
-
119
- For inquiries or more information, you can reach out to us at [ganemedelabs@gmail.com](mailto:ganemedelabs@gmail.com).
1
+ # Saturon
2
+
3
+ ![Version](https://img.shields.io/npm/v/saturon)
4
+ ![Downloads](https://img.shields.io/npm/dw/saturon)
5
+ ![License](https://img.shields.io/npm/l/saturon)
6
+
7
+ A runtime-extensible JavaScript library for parsing, converting, and manipulating colors with full CSS spec support.
8
+
9
+ ## 📋 Table of Contents
10
+
11
+ - [Features](#-features)
12
+ - [Installation](#-installation)
13
+ - [Usage](#-usage)
14
+ - [Examples](#-examples)
15
+ - [Documentation](#-documentation)
16
+ - [License](#-license)
17
+ - [Contact](#-contact)
18
+
19
+ ## ✨ Features
20
+
21
+ - **Full CSS Color 4/5 Parsing**
22
+ - Infinite nested color functions (e.g. `color-mix(...)` inside `light-dark(...)`)
23
+ - Converts between all modern color spaces (OKLab, Display-P3, Rec.2020, etc.)
24
+ - High-precision color math for serious colorimetry
25
+ - Powerful plugin system for custom color spaces and functions
26
+ - Supports complex color syntaxes like `color(from hsl(240 none calc(-infinity) / 0.5) display-p3 r calc(g + b) 100 / alpha)`
27
+
28
+ ## 🔧 Installation
29
+
30
+ ```bash
31
+ npm install saturon
32
+ # or
33
+ yarn add saturon
34
+ ```
35
+
36
+ ## 🚀 Usage
37
+
38
+ ```js
39
+ import { Color } from "saturon";
40
+
41
+ // Parse any CSS color string
42
+ const color = Color.from("#1481b8ff");
43
+
44
+ // Access coordinates
45
+ console.log(color.toArray()); // [20, 129, 184, 1]
46
+
47
+ // Convert to another format
48
+ console.log(color.to("oklch", { units: true })); // → "oklch(0.57368 0.12258 238.41345deg)"
49
+
50
+ // Access values in another color space
51
+ console.log(color.in("lab").toObject({ precision: 1 })); // { l: 50.5, a: -13.9, b: -37.6, alpha: 1 }
52
+
53
+ // Modify components
54
+ const modified = color.in("hsl").with({ l: (l) => l * 1.2 });
55
+ console.log(modified.toString({ legacy: true })); // → "hsl(200, 80%, 48%)"
56
+ ```
57
+
58
+ ## 💡 Examples
59
+
60
+ ### Converting Colors
61
+
62
+ ```js
63
+ const color = Color.from("hsl(337 100% 60%)");
64
+ console.log(color.to("rgb")); // → rgb(255 51 129)
65
+ console.log(color.to("hex-color")); // → #ff3381
66
+ ```
67
+
68
+ ### Manipulating Components
69
+
70
+ ```js
71
+ const color = Color.from("hwb(255 7% 1%)");
72
+ const hwb = color.with({ h: 100, b: (b) => b * 20 });
73
+ console.log(hwb.toString()); // → hwb(100 7 20)
74
+ ```
75
+
76
+ ### Mixing Colors
77
+
78
+ ```js
79
+ const red = Color.from("hsl(0, 100%, 50%)");
80
+ const mixed = red.mix("hsl(120, 100%, 50%)");
81
+ console.log(mixed.toString()); // → hsl(60 100 50)
82
+ ```
83
+
84
+ ### New Named Color Registration
85
+
86
+ ```js
87
+ registerNamedColor("sunsetblush", [255, 94, 77]);
88
+ const rgb = Color.from("rgb(255, 94, 77)");
89
+ console.log(rgb.to("named-color")); // → sunsetblush
90
+ ```
91
+
92
+ ### New Color Function Registration
93
+
94
+ ```js
95
+ const converter = {
96
+ components: {
97
+ i: { index: 0, value: [0, 1] },
98
+ ct: { index: 1, value: [-1, 1] },
99
+ cp: { index: 2, value: [-1, 1] },
100
+ },
101
+ bridge: "rgb",
102
+ toBridge: (ictcp: number[]) => [/* r, g, b */],
103
+ fromBridge: (rgb: number[]) => [/* i, ct, cp */],
104
+ };
105
+
106
+ registerColorFunction("ictcp", converter);
107
+ const ictcp = Color.from("ictcp(0.2 0.2 -0.1)");
108
+ console.log(ictcp.to("rgb")); // → rgb(6 7 90)
109
+ ```
110
+
111
+ ## 📚 Documentation
112
+
113
+ Full documentation is available at [saturon.js.org](https://saturon.js.org).
114
+
115
+ ## 📜 License
116
+
117
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
118
+
119
+ ## 📧 Contact
120
+
121
+ For inquiries or more information, you can reach out to us at [ganemedelabs@gmail.com](mailto:ganemedelabs@gmail.com).
package/dist/Color.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Component, MixOptions, ColorType, OutputType, FormattingOptions, NamedColor, ColorSpace, FitMethod, ColorModel, RandomOptions, GetOptions } from "./types.js";
1
+ import type { Component, MixOptions, ColorType, OutputType, FormattingOptions, NamedColor, ColorSpace, FitMethod, ColorModel, RandomOptions, ComponentOptions } from "./types.js";
2
2
  /**
3
3
  * The `Color` class represents a dynamic CSS color object, allowing for the manipulation
4
4
  * and retrieval of colors in various formats (e.g., RGB, HEX, HSL).
@@ -82,7 +82,7 @@ export declare class Color<M extends ColorModel = ColorModel> {
82
82
  * @returns An object mapping each component (and alpha) to its numeric value.
83
83
  * @throws If the model has no defined components.
84
84
  */
85
- toObject(options?: GetOptions): { [key in Component<M>]: number; };
85
+ toObject(options?: ComponentOptions): { [key in Component<M>]: number; };
86
86
  /**
87
87
  * Returns the color as an array of component values, optionally normalized and fitted.
88
88
  *
@@ -90,9 +90,9 @@ export declare class Color<M extends ColorModel = ColorModel> {
90
90
  * @returns An array of normalized color components and alpha.
91
91
  * @throws If the model has no defined components.
92
92
  */
93
- toArray(options?: GetOptions): number[];
93
+ toArray(options?: ComponentOptions): number[];
94
94
  /**
95
- * Returns a new `Color` instance with updated or replaced component values.
95
+ * Creates a new Color instance with modified component values.
96
96
  *
97
97
  * This method supports several flexible update styles:
98
98
  *
@@ -131,12 +131,14 @@ export declare class Color<M extends ColorModel = ColorModel> {
131
131
  * color.with(({ r, g, b }) => [r * 0.5, g * 0.5, b * 0.5, 1]);
132
132
  * ```
133
133
  *
134
- * @param values - Either:
135
- * - a partial object of component values,
136
- * - an updater function returning an object or array,
137
- * - or an array of new coordinates.
138
- * @returns A new `Color` instance with updated values.
139
- * @throws If the color model has no defined components.
134
+ * @template M - The color model type
135
+ * @param values - The new component values to apply. Can be:
136
+ * - A partial object mapping component names to numbers or update functions
137
+ * - A function that receives current components and returns partial updates or an array of values
138
+ * - An array of new values corresponding to component indices
139
+ * @param normalize - Whether to normalize component values to their valid ranges. Defaults to `true`.
140
+ * When `false`, values are not clamped or validated against their ranges.
141
+ * @returns A new Color instance with the updated component values
140
142
  */
141
143
  with(values: Partial<{
142
144
  [K in Component<M> | "alpha"]: number | ((prev: number) => number);
@@ -144,7 +146,7 @@ export declare class Color<M extends ColorModel = ColorModel> {
144
146
  [K in Component<M> | "alpha"]: number;
145
147
  }) => Partial<{
146
148
  [K in Component<M> | "alpha"]: number;
147
- }> | (number | undefined)[]) | (number | undefined)[]): Color<M>;
149
+ }> | (number | undefined)[]) | (number | undefined)[], normalize?: boolean): Color<M>;
148
150
  /**
149
151
  * Mixes this color with another by a specified amount.
150
152
  *
@@ -163,6 +165,13 @@ export declare class Color<M extends ColorModel = ColorModel> {
163
165
  * @throws If the gamut is unsupported.
164
166
  */
165
167
  within(gamut: ColorSpace, method?: FitMethod): Color<M>;
168
+ /**
169
+ * Creates a new Color instance with values fitted to the color model's gamut.
170
+ *
171
+ * @param options - Configuration options for fitting
172
+ * @returns A new Color instance with fitted values
173
+ */
174
+ fit(options?: ComponentOptions): Color<M>;
166
175
  /**
167
176
  * Calculates the WCAG 2.1 contrast ratio between this color and another.
168
177
  *