saturon 0.2.3 → 0.3.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.
- package/LICENSE +1 -1
- package/README.md +5 -4
- package/dist/Color.d.ts +50 -47
- package/dist/Color.js +122 -229
- package/dist/converters.d.ts +35 -141
- package/dist/converters.js +8 -422
- package/dist/engine.d.ts +148 -0
- package/dist/engine.js +1900 -0
- package/dist/index.umd.js +1774 -1300
- package/dist/index.umd.min.js +143 -1
- package/dist/math.d.ts +2 -12
- package/dist/math.js +1 -11
- package/dist/tests/Color.test.d.ts +17 -0
- package/dist/tests/Color.test.js +1062 -0
- package/dist/tests/wpt.test.js +2129 -0
- package/dist/types.d.ts +62 -72
- package/dist/utils.d.ts +10 -141
- package/dist/utils.js +108 -1052
- package/package.json +10 -11
- package/dist/temp.js +0 -3
- /package/dist/{temp.d.ts → tests/wpt.test.d.ts} +0 -0
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Color } from "./Color.js";
|
|
2
2
|
import { systemColors } from "./config.js";
|
|
3
|
-
import { namedColors,
|
|
4
|
-
import {
|
|
3
|
+
import { namedColors, colorModels, colorSpaces } from "./converters.js";
|
|
4
|
+
import { fitMethods } from "./math.js";
|
|
5
|
+
import { formatters, ParseNode } from "./engine.js";
|
|
5
6
|
/** Represents the config object. */
|
|
6
7
|
export type Config = {
|
|
7
8
|
/** The theme of the application, either "light" or "dark". */
|
|
@@ -18,10 +19,6 @@ export type Config = {
|
|
|
18
19
|
};
|
|
19
20
|
/** Represents a plugin type for the `Color` class. */
|
|
20
21
|
export type Plugin = (colorClass: typeof Color) => void;
|
|
21
|
-
/** Represents the available `<color>`s. */
|
|
22
|
-
export type ColorType = keyof typeof colorTypes;
|
|
23
|
-
/** Represents the available `<color-base>`s. */
|
|
24
|
-
export type ColorBase = keyof typeof colorBases;
|
|
25
22
|
/** Represents the available `<color-function>`s. */
|
|
26
23
|
export type ColorFunction = keyof typeof colorModels;
|
|
27
24
|
/** Represents the available color spaces for `<color()>` function. */
|
|
@@ -32,58 +29,8 @@ export type ColorModel = keyof typeof colorModels;
|
|
|
32
29
|
export type NamedColor = keyof typeof namedColors;
|
|
33
30
|
/** Represents a `<system-color>` identifier. */
|
|
34
31
|
export type SystemColor = keyof typeof systemColors;
|
|
35
|
-
/** Represents the color types that
|
|
36
|
-
export type OutputType =
|
|
37
|
-
[K in ColorType]: (typeof colorTypes)[K] extends {
|
|
38
|
-
fromBridge?: (components: number[]) => number[];
|
|
39
|
-
format?: (coords: number[]) => string | undefined;
|
|
40
|
-
} ? K : never;
|
|
41
|
-
}[ColorType];
|
|
42
|
-
/** Represents a converter for `<color>`s. */
|
|
43
|
-
export type ColorConverter = {
|
|
44
|
-
/**
|
|
45
|
-
* Checks whether a given string is a valid representation of this color type.
|
|
46
|
-
*
|
|
47
|
-
* @param str - The string to validate.
|
|
48
|
-
* @returns `true` if the string is valid for this color type, otherwise `false`.
|
|
49
|
-
*/
|
|
50
|
-
isValid: (str: string) => boolean;
|
|
51
|
-
/** The intermediate "bridge" color space used for conversion. Must be another `<color-function>` identifier (e.g., `"rgb"`, `"xyz"`). */
|
|
52
|
-
bridge: string;
|
|
53
|
-
/**
|
|
54
|
-
* Converts coordinates from the native color function into the bridge color space.
|
|
55
|
-
*
|
|
56
|
-
* @param coords - The coordinates in the native color function's space.
|
|
57
|
-
* @returns The coordinates converted to the bridge color space.
|
|
58
|
-
*/
|
|
59
|
-
toBridge: (coords: number[]) => number[];
|
|
60
|
-
/**
|
|
61
|
-
* Parses a string representation of the color into its numeric coordinates.
|
|
62
|
-
*
|
|
63
|
-
* @param str - The string to parse.
|
|
64
|
-
* @returns The numeric coordinates of the color.
|
|
65
|
-
*/
|
|
66
|
-
parse: (str: string) => number[];
|
|
67
|
-
} & ({
|
|
68
|
-
/**
|
|
69
|
-
* Converts coordinates from the bridge color space back into the native color function's coordinate system.
|
|
70
|
-
*
|
|
71
|
-
* @param coords - The coordinates in the bridge color space.
|
|
72
|
-
* @returns The coordinates converted back to the native color function's space.
|
|
73
|
-
*/
|
|
74
|
-
fromBridge: (coords: number[]) => number[];
|
|
75
|
-
/**
|
|
76
|
-
* Formats numeric component values into a valid CSS color string.
|
|
77
|
-
*
|
|
78
|
-
* @param coords - The numeric component values to format.
|
|
79
|
-
* @param options - Optional formatting options.
|
|
80
|
-
* @returns A formatted CSS color string, or `undefined` if formatting fails.
|
|
81
|
-
*/
|
|
82
|
-
format: (coords: number[], options?: FormattingOptions) => string | undefined;
|
|
83
|
-
} | {
|
|
84
|
-
fromBridge?: undefined;
|
|
85
|
-
format?: undefined;
|
|
86
|
-
});
|
|
32
|
+
/** Represents the color types that can be formatted to string output. */
|
|
33
|
+
export type OutputType = keyof typeof formatters;
|
|
87
34
|
/** Represents a converter for `<color-function>`s. */
|
|
88
35
|
export type ColorModelConverter = {
|
|
89
36
|
/** The target color gamut identifier that the function should be clamped to (e.g., `"srgb"`, `"display-p3"`), or `null` for color spaces without a fixed gamut (e.g., `lab`, `lch`). */
|
|
@@ -138,6 +85,43 @@ export type ColorSpaceConverter = {
|
|
|
138
85
|
/** Matrix to convert from the bridge color space. */
|
|
139
86
|
fromBridgeMatrix: number[][];
|
|
140
87
|
};
|
|
88
|
+
/** Represents an interface for serializing internal color data structures into specific string formats. */
|
|
89
|
+
export type ColorFormatter = {
|
|
90
|
+
/** The intermediate "bridge" color space used for conversion (e.g., `"rgb"`, `"srgb"`). */
|
|
91
|
+
bridge: string;
|
|
92
|
+
/**
|
|
93
|
+
* Translates coordinates from the bridge color space into the format's local coordinate system.
|
|
94
|
+
*
|
|
95
|
+
* @param coords - The coordinates in the bridge color space.
|
|
96
|
+
* @returns The transformed coordinate values.
|
|
97
|
+
*/
|
|
98
|
+
fromBridge: (coords: number[]) => number[];
|
|
99
|
+
/**
|
|
100
|
+
* Converts native channel coordinates into their string representation.
|
|
101
|
+
*
|
|
102
|
+
* @param coords - The coordinate array to format, with optional alpha at the last index.
|
|
103
|
+
* @param options - Options for adjusting output format, legacy syntax, and precision.
|
|
104
|
+
* @returns The formatted color string, or `undefined` if formatting fails.
|
|
105
|
+
*/
|
|
106
|
+
format: (coords: number[], options?: FormattingOptions) => string | undefined;
|
|
107
|
+
};
|
|
108
|
+
/** Defines a grammar rule specification used to extend the CSS color parser engine. */
|
|
109
|
+
export type GrammarRuleSpec = {
|
|
110
|
+
/** The name or names of existing grammar rules to append this rule to (e.g., `"<color>"`). */
|
|
111
|
+
appendTo?: string | string[];
|
|
112
|
+
/** The CSS-style syntax matching pattern for the custom grammar statement. */
|
|
113
|
+
rule: string;
|
|
114
|
+
/**
|
|
115
|
+
* Translates a matched abstract syntax tree node into a color model and coordinate array.
|
|
116
|
+
*
|
|
117
|
+
* @param node - The matched parse tree node.
|
|
118
|
+
* @returns The color model identifier and calculated coordinate values.
|
|
119
|
+
*/
|
|
120
|
+
parse?: (node: ParseNode) => {
|
|
121
|
+
model: ColorModel;
|
|
122
|
+
coords: number[];
|
|
123
|
+
};
|
|
124
|
+
};
|
|
141
125
|
/** Defines the properties of a color component within a converter. */
|
|
142
126
|
export type ComponentDefinition = {
|
|
143
127
|
/** Position of the component in the color array */
|
|
@@ -152,11 +136,9 @@ export type ComponentDefinition = {
|
|
|
152
136
|
*
|
|
153
137
|
* @template M - The color model type.
|
|
154
138
|
*/
|
|
155
|
-
export type Component<M extends ColorModel> = keyof (typeof colorModels)[M]["components"]
|
|
139
|
+
export type Component<M extends ColorModel> = keyof (typeof colorModels)[M]["components"];
|
|
156
140
|
/** Specifies the method used for interpolating hue values during color mixing. */
|
|
157
141
|
export type HueInterpolationMethod = "shorter" | "longer" | "increasing" | "decreasing";
|
|
158
|
-
/** Represents the set of valid easing function names. */
|
|
159
|
-
export type Easing = keyof typeof EASINGS;
|
|
160
142
|
/** Represents a gamut mapping method. */
|
|
161
143
|
export type FitFunction = (coords: number[], model: ColorFunction) => number[];
|
|
162
144
|
/** Describes the available methods for fitting the color into the target gamut. */
|
|
@@ -168,14 +150,19 @@ export type ComponentOptions = {
|
|
|
168
150
|
/** Overrides the auto precision of the output color components, or `null` to disable rounding. */
|
|
169
151
|
precision?: number | null;
|
|
170
152
|
};
|
|
171
|
-
/** Options for formatting color
|
|
153
|
+
/** Options for formatting color components. */
|
|
172
154
|
export type FormattingOptions = ComponentOptions & {
|
|
173
155
|
/** Use legacy syntax (e.g., `"rgba(255, 0, 0, 0.5)"`). */
|
|
174
156
|
legacy?: boolean;
|
|
175
157
|
/** Output components with optional unit suffixes (e.g., `"hsl(250deg 74% 54%)"`). */
|
|
176
158
|
units?: boolean;
|
|
177
159
|
};
|
|
178
|
-
/** Options for
|
|
160
|
+
/** Options for validating color strings with `Color.isValid()`. */
|
|
161
|
+
export type IsValidOptions = {
|
|
162
|
+
/** The specific rule or color type to validate against (e.g., `"<rgb()>"`, `"<hex-color>"`, `"<color-base>"`). If omitted, validates against all supported formats. */
|
|
163
|
+
rule?: string;
|
|
164
|
+
};
|
|
165
|
+
/** Options for generating a random Color instance with `Color.random()`. */
|
|
179
166
|
export type RandomOptions<M extends ColorModel = ColorModel> = {
|
|
180
167
|
/** The color model to use (e.g., "rgb" or "hsl"). */
|
|
181
168
|
model?: M;
|
|
@@ -188,14 +175,17 @@ export type RandomOptions<M extends ColorModel = ColorModel> = {
|
|
|
188
175
|
/** Optional deviation values for each channel, used to control randomness. */
|
|
189
176
|
deviation?: Partial<Record<Component<M>, number>>;
|
|
190
177
|
};
|
|
191
|
-
/**
|
|
192
|
-
export type
|
|
193
|
-
/**
|
|
194
|
-
|
|
195
|
-
/**
|
|
178
|
+
/** Represents an individual color component within a color-mix array in `Color.mix()`. */
|
|
179
|
+
export type MixItem = {
|
|
180
|
+
/** The Color instance to be mixed. */
|
|
181
|
+
color: Color<ColorModel>;
|
|
182
|
+
/** The optional user-specified mixing weight percentage (0 to 100). */
|
|
183
|
+
percentage?: number;
|
|
184
|
+
};
|
|
185
|
+
/** Options configuring color mixing behavior in `Color.mix()`. */
|
|
186
|
+
export type MixOptions<M extends ColorModel = "oklab"> = {
|
|
187
|
+
/** The color model to perform the mix in. If omitted, the mix is performed in OKLAB. */
|
|
188
|
+
in?: M;
|
|
189
|
+
/** The strategy used for hue interpolation in cylindrical spaces (e.g., "shorter", "longer"). */
|
|
196
190
|
hue?: HueInterpolationMethod;
|
|
197
|
-
/** Easing function to apply to the interpolation parameter. */
|
|
198
|
-
easing?: Easing | ((t: number) => number);
|
|
199
|
-
/** Gamma correction value to use during mixing. */
|
|
200
|
-
gamma?: number;
|
|
201
191
|
};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Color } from "./Color.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ColorModel, ColorSpaceConverter, ComponentDefinition, FitMethod, Plugin, Config, HueInterpolationMethod } from "./types.js";
|
|
3
3
|
/** Global cache for internal Color operations. */
|
|
4
4
|
export declare const cache: Map<any, any>;
|
|
5
5
|
/** Registered plugin functions for the Color class. */
|
|
@@ -29,133 +29,14 @@ export declare function configure(options: Partial<Config>): void;
|
|
|
29
29
|
* @throws If no plugins are provided or a plugin is not a function.
|
|
30
30
|
*/
|
|
31
31
|
export declare function use(...pluginFns: Plugin[]): void;
|
|
32
|
-
declare const getterRegistry: {
|
|
33
|
-
readonly "color-types": () => ColorType[];
|
|
34
|
-
readonly "color-bases": () => ColorBase[];
|
|
35
|
-
readonly "color-functions": () => ColorFunction[];
|
|
36
|
-
readonly "color-models": () => ColorModel[];
|
|
37
|
-
readonly "color-spaces": () => ColorSpace[];
|
|
38
|
-
readonly "named-colors": () => NamedColor[];
|
|
39
|
-
readonly "system-colors": () => SystemColor[];
|
|
40
|
-
readonly "output-types": () => OutputType[];
|
|
41
|
-
readonly plugins: () => string[];
|
|
42
|
-
readonly "fit-methods": () => FitMethod[];
|
|
43
|
-
};
|
|
44
|
-
type Getter = keyof typeof getterRegistry;
|
|
45
|
-
/**
|
|
46
|
-
* Retrieve a list of registered items of a specified type.
|
|
47
|
-
*
|
|
48
|
-
* @param type - The getter type, e.g. "color-types".
|
|
49
|
-
* @returns The array returned by the getter.
|
|
50
|
-
*/
|
|
51
|
-
export declare function get<T extends Getter>(type: T): ReturnType<(typeof getterRegistry)[T]>;
|
|
52
|
-
declare const converterRegistry: {
|
|
53
|
-
readonly "color-type": {
|
|
54
|
-
readonly fn: (name: string, value: ColorConverter) => void;
|
|
55
|
-
};
|
|
56
|
-
readonly "color-base": {
|
|
57
|
-
readonly fn: (name: string, value: ColorConverter) => void;
|
|
58
|
-
};
|
|
59
|
-
readonly "color-function": {
|
|
60
|
-
readonly fn: (name: string, value: ColorModelConverter) => void;
|
|
61
|
-
};
|
|
62
|
-
readonly "color-space": {
|
|
63
|
-
readonly fn: (name: string, value: ColorSpaceConverter) => void;
|
|
64
|
-
};
|
|
65
|
-
readonly "named-color": {
|
|
66
|
-
readonly fn: (name: string, value: number[]) => void;
|
|
67
|
-
};
|
|
68
|
-
readonly "fit-method": {
|
|
69
|
-
readonly fn: (name: string, value: FitFunction) => void;
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
|
-
type ConverterType = keyof typeof converterRegistry;
|
|
73
|
-
type ConverterEntry<T extends ConverterType> = {
|
|
74
|
-
name: string;
|
|
75
|
-
value: Parameters<(typeof converterRegistry)[T]["fn"]>[1];
|
|
76
|
-
};
|
|
77
|
-
/**
|
|
78
|
-
* Bulk register multiple converters of a specified type.
|
|
79
|
-
*
|
|
80
|
-
* @param type - The converter type, e.g. "color-function".
|
|
81
|
-
* @param entries - Array of { name, value } objects.
|
|
82
|
-
*/
|
|
83
|
-
export declare function register<T extends ConverterType>(type: T, entries: ConverterEntry<T>[]): void;
|
|
84
|
-
/**
|
|
85
|
-
* Registers a new `<color>` converter under the specified name.
|
|
86
|
-
*
|
|
87
|
-
* @param name - Unique name for the `<color>` converter.
|
|
88
|
-
* @param converter - Converter implementing color conversion logic.
|
|
89
|
-
* @throws If the name is already used or the converter is invalid.
|
|
90
|
-
*/
|
|
91
|
-
export declare function registerColorType(name: string, converter: ColorConverter): void;
|
|
92
|
-
/**
|
|
93
|
-
* Registers a new `<color-base>` converter under the specified name.
|
|
94
|
-
*
|
|
95
|
-
* @param name - The unique name for the `<color-base>` converter.
|
|
96
|
-
* @param converter - Converter implementing color conversion logic.
|
|
97
|
-
* @throws If the name is already used or the converter is invalid.
|
|
98
|
-
*/
|
|
99
|
-
export declare function registerColorBase(name: string, converter: ColorConverter): void;
|
|
100
|
-
/**
|
|
101
|
-
* Registers a new `<color-function>` converter under the specified name.
|
|
102
|
-
*
|
|
103
|
-
* @param name - The unique name for the `<color-function>` converter.
|
|
104
|
-
* @param converter - Converter implementing color conversion logic.
|
|
105
|
-
* @throws If the name is already used or the converter is invalid.
|
|
106
|
-
*/
|
|
107
|
-
export declare function registerColorFunction(name: string, converter: ColorModelConverter): void;
|
|
108
|
-
/**
|
|
109
|
-
* Registers a new color space converter for `<color()>` function under the specified name.
|
|
110
|
-
*
|
|
111
|
-
* @param name - The unique name for the color space converter.
|
|
112
|
-
* @param converter - Converter implementing color space conversion logic.
|
|
113
|
-
* @throws If the name is already used or the converter is invalid.
|
|
114
|
-
*/
|
|
115
|
-
export declare function registerColorSpace(name: string, converter: ColorSpaceConverter): void;
|
|
116
32
|
/**
|
|
117
|
-
*
|
|
33
|
+
* Normalizes a component value to a valid range based on the component definition.
|
|
118
34
|
*
|
|
119
|
-
* @param
|
|
120
|
-
* @param
|
|
121
|
-
* @
|
|
35
|
+
* @param component - The numeric component value to normalize
|
|
36
|
+
* @param value - The component definition that specifies the valid range (either a [min, max] tuple, "hue" for [0, 360], or a default [0, 100] range)
|
|
37
|
+
* @returns The normalized component value, clamped to the valid range or 0/max/min for NaN/Infinity cases
|
|
122
38
|
*/
|
|
123
|
-
export declare function
|
|
124
|
-
/**
|
|
125
|
-
* Registers a new fit method under a specified name.
|
|
126
|
-
*
|
|
127
|
-
* @param name - Name for the fit method (whitespace → hyphens, lowercased).
|
|
128
|
-
* @param method - The fit function.
|
|
129
|
-
* @throws If name exists or method is not a function.
|
|
130
|
-
*/
|
|
131
|
-
export declare function registerFitMethod(name: string, method: FitFunction): void;
|
|
132
|
-
/**
|
|
133
|
-
* Unregisters one or more color types from the registry.
|
|
134
|
-
*
|
|
135
|
-
* @param types - Names of color types to remove.
|
|
136
|
-
*/
|
|
137
|
-
export declare function unregister(...types: string[]): void;
|
|
138
|
-
/**
|
|
139
|
-
* Cleans and normalizes a CSS color string.
|
|
140
|
-
*
|
|
141
|
-
* @param color - The CSS color string.
|
|
142
|
-
* @returns The normalized string.
|
|
143
|
-
*/
|
|
144
|
-
export declare function clean(color: string): string;
|
|
145
|
-
/**
|
|
146
|
-
* Extracts a balanced expression from a string starting at a given index.
|
|
147
|
-
*
|
|
148
|
-
* - If the character at `start` is '(', extracts the full parenthetical expression (including nested parentheses).
|
|
149
|
-
* - Otherwise, extracts a contiguous sequence of alphanumeric characters, hyphens, percent signs, or '#'.
|
|
150
|
-
*
|
|
151
|
-
* @param input - The string to extract from.
|
|
152
|
-
* @param start - Index to start extraction.
|
|
153
|
-
* @returns An object with the extracted `expression` and the index `end` after it.
|
|
154
|
-
*/
|
|
155
|
-
export declare function extractBalancedExpression(input: string, start: number): {
|
|
156
|
-
expression: string;
|
|
157
|
-
end: number;
|
|
158
|
-
};
|
|
39
|
+
export declare function normalize(component: number, value: ComponentDefinition["value"]): number;
|
|
159
40
|
/**
|
|
160
41
|
* Fits or clips color coordinates to a specified model and gamut.
|
|
161
42
|
*
|
|
@@ -169,21 +50,6 @@ export declare function fit(coords: number[], model: ColorModel, options?: {
|
|
|
169
50
|
method?: FitMethod;
|
|
170
51
|
precision?: number | null;
|
|
171
52
|
}): number[];
|
|
172
|
-
/**
|
|
173
|
-
* Converts a color model converter to `<color>` converter.
|
|
174
|
-
*
|
|
175
|
-
* @param name - The name of the color model.
|
|
176
|
-
* @param converter - The color model converter definition.
|
|
177
|
-
* @returns An object of type `ColorConverter`.
|
|
178
|
-
*/
|
|
179
|
-
export declare function modelConverterToColorConverter(name: string, converter: ColorModelConverter): {
|
|
180
|
-
isValid: (str: string) => boolean;
|
|
181
|
-
bridge: string;
|
|
182
|
-
toBridge: (coords: number[]) => number[];
|
|
183
|
-
parse: (str: string) => number[];
|
|
184
|
-
fromBridge: (coords: number[]) => number[];
|
|
185
|
-
format: ([c1, c2, c3, a]: number[], options?: FormattingOptions) => string;
|
|
186
|
-
};
|
|
187
53
|
/**
|
|
188
54
|
* Converts a color space converter to a color model converter.
|
|
189
55
|
*
|
|
@@ -202,4 +68,7 @@ export declare function spaceConverterToModelConverter<const C extends readonly
|
|
|
202
68
|
toBridge: (coords: number[]) => number[];
|
|
203
69
|
fromBridge: (coords: number[]) => number[];
|
|
204
70
|
};
|
|
205
|
-
export
|
|
71
|
+
export declare function hueDelta(a: number, b: number): number;
|
|
72
|
+
export declare function hueDeltaLong(a: number, b: number): number;
|
|
73
|
+
export declare function interpHue(a: number, b: number, t: number, method: string): number;
|
|
74
|
+
export declare function mixTwo<M extends ColorModel = "oklab">(colorA: Color<M>, colorB: Color<M>, t: number, model?: M, hue?: HueInterpolationMethod): Color<M>;
|