toosoon-utils 4.2.3 → 4.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.
Files changed (42) hide show
  1. package/README.md +499 -575
  2. package/lib/colors.d.ts +147 -66
  3. package/lib/colors.js +149 -63
  4. package/lib/constants.js +1 -1
  5. package/lib/dom.d.ts +1 -1
  6. package/lib/dom.js +1 -1
  7. package/lib/extras/colors/Color.d.ts +406 -0
  8. package/lib/extras/colors/Color.js +546 -0
  9. package/lib/extras/colors/ColorPalette.d.ts +105 -0
  10. package/lib/extras/colors/ColorPalette.js +124 -0
  11. package/lib/extras/colors/ColorScale.d.ts +257 -0
  12. package/lib/extras/colors/ColorScale.js +347 -0
  13. package/lib/extras/colors/_ColorScale.d.ts +62 -0
  14. package/lib/extras/colors/_ColorScale.js +156 -0
  15. package/lib/extras/colors/index.d.ts +3 -0
  16. package/lib/extras/colors/index.js +3 -0
  17. package/lib/extras/frame-rate/FrameRate.d.ts +1 -1
  18. package/lib/extras/frame-rate/FrameRate.js +2 -2
  19. package/lib/extras/geometry/Vector.d.ts +1 -1
  20. package/lib/extras/geometry/Vector2.d.ts +17 -11
  21. package/lib/extras/geometry/Vector2.js +29 -23
  22. package/lib/extras/geometry/Vector3.d.ts +5 -5
  23. package/lib/extras/geometry/Vector3.js +10 -10
  24. package/lib/extras/paths/Path.d.ts +3 -3
  25. package/lib/extras/paths/Path.js +10 -10
  26. package/lib/extras/paths/PathContext.d.ts +5 -10
  27. package/lib/extras/paths/PathContext.js +70 -99
  28. package/lib/extras/paths/PathSVG.d.ts +31 -25
  29. package/lib/extras/paths/PathSVG.js +36 -39
  30. package/lib/extras/paths/index.d.ts +1 -1
  31. package/lib/geometry.js +1 -1
  32. package/lib/maths.d.ts +19 -13
  33. package/lib/maths.js +23 -17
  34. package/lib/prng.d.ts +4 -4
  35. package/lib/prng.js +4 -4
  36. package/lib/random.d.ts +4 -4
  37. package/lib/random.js +4 -4
  38. package/lib/strings.d.ts +14 -8
  39. package/lib/strings.js +14 -8
  40. package/lib/tsconfig.tsbuildinfo +1 -1
  41. package/lib/types.d.ts +15 -8
  42. package/package.json +14 -14
@@ -0,0 +1,124 @@
1
+ import Color from './Color';
2
+ import ColorScale from './ColorScale';
3
+ /**
4
+ * Utility class for generating color palettes
5
+ *
6
+ * @exports
7
+ * @class ColorPalette
8
+ */
9
+ export default class ColorPalette {
10
+ isColorPalette = true;
11
+ type = 'ColorPalette';
12
+ _base = new Color();
13
+ /**
14
+ * Object containing this color palette color scales
15
+ */
16
+ scales = {};
17
+ /**
18
+ * Object containing this color palette generators
19
+ */
20
+ generators = {};
21
+ /**
22
+ * @param {Color|ColorRepresentation} color Base color
23
+ * @param {object} generators Object containing generators
24
+ */
25
+ constructor(color, generators = {}) {
26
+ this.base = color;
27
+ this.generators = generators;
28
+ this.update();
29
+ }
30
+ /**
31
+ * Pick an interpolated color from one of this color palette color scale
32
+ *
33
+ * @param {string} key Name of the color scale
34
+ * @param {number} t Normalized time value to interpolate
35
+ * @returns {Color} Interpolated color
36
+ */
37
+ getColor(key, t) {
38
+ return this.scales[key]?.getColor(t) ?? this.base.clone();
39
+ }
40
+ /**
41
+ * Add a given generator to this color palette
42
+ *
43
+ * @param {string} key Name of the generator
44
+ * @param {ColorPaletteGenerator} generator Generator to add
45
+ */
46
+ subscribe(key, generator) {
47
+ this.generators[key] = generator;
48
+ this.scales[key] = this.generate(generator);
49
+ }
50
+ /**
51
+ * Generate a color scale
52
+ *
53
+ * @param {ColorPaletteGenerator} generator Generator interface to use for generation
54
+ * @param {ColorInterpolation} generator.interpolation Type of interpolation used for generation
55
+ * @param {number} generator.length Amount of colors to generate
56
+ * @param {Color} [generator.target] Target color to interpolate towards
57
+ * @param {ColorScaleProcessingParameters} [generator.processing] Target color processing parameters, for processing the target color before generation
58
+ * @param {object} [generator.params] Interpolation parameters
59
+ * @returns {ColorScale} Generated color scale
60
+ */
61
+ generate(generator) {
62
+ const { interpolation, length, target: color, processing, params } = generator;
63
+ const base = ColorScale.processColor(this.base.clone(), params?.preprocessing);
64
+ const target = ColorScale.processColor(color ?? this.base.clone(), processing);
65
+ return new ColorScale().generate(interpolation, length, base, target, params).process(params?.postprocessing);
66
+ }
67
+ /**
68
+ * Update this color palette color scales
69
+ */
70
+ update() {
71
+ for (let key in this.generators) {
72
+ const generator = this.generators[key];
73
+ this.scales[key] = this.generate(generator);
74
+ }
75
+ }
76
+ /**
77
+ * Base color of this color palette
78
+ */
79
+ set base(color) {
80
+ this._base.set(color);
81
+ this.update();
82
+ }
83
+ get base() {
84
+ return this._base;
85
+ }
86
+ }
87
+ /** Generators */
88
+ const length = 9;
89
+ export const LightScaleGenerator = {
90
+ interpolation: 'hsl',
91
+ length,
92
+ processing: { hsl: { lightness: 1 - 1 / length } },
93
+ params: {
94
+ preprocessing: { hsl: { lightness: 0.5 } },
95
+ postprocessing: {}
96
+ }
97
+ };
98
+ export const DarkScaleGenerator = {
99
+ interpolation: 'hsl',
100
+ length,
101
+ processing: { hsl: { lightness: 1 / length } },
102
+ params: {
103
+ preprocessing: { hsl: { lightness: 0.5 } },
104
+ postprocessing: {}
105
+ }
106
+ };
107
+ export const SequentialLightGenerator = {
108
+ interpolation: 'sequential',
109
+ length,
110
+ processing: { hcl: { luminance: 100 } },
111
+ params: {
112
+ preprocessing: { hcl: { luminance: 50 } },
113
+ postprocessing: {}
114
+ }
115
+ };
116
+ export const SequentialDarkGenerator = {
117
+ interpolation: 'sequential',
118
+ length,
119
+ processing: { hcl: { luminance: 0 } },
120
+ params: {
121
+ preprocessing: { hcl: { luminance: 50 } },
122
+ postprocessing: {}
123
+ }
124
+ };
@@ -0,0 +1,257 @@
1
+ import type { Range } from '../../types';
2
+ import Color, { type ColorInterpolationsParameters, type ColorInterpolation } from './Color';
3
+ /**
4
+ * Processing parameter used to modify an input value into a target.
5
+ * - If the target is a number, the input will be set to its value
6
+ * - If the target is an array of numbers, the input will be clamped inside the range [min, max]
7
+ * - If the target is undefined, the input will stay the same
8
+ */
9
+ export type ColorScaleProcessingTarget = number | Range | undefined;
10
+ /**
11
+ * Parameters used for color scale processing
12
+ */
13
+ export type ColorScaleProcessingParameters = {
14
+ /**
15
+ * Processing HSL modifiers
16
+ */
17
+ hsl?: {
18
+ hue?: ColorScaleProcessingTarget;
19
+ saturation?: ColorScaleProcessingTarget;
20
+ lightness?: ColorScaleProcessingTarget;
21
+ };
22
+ /**
23
+ * Processing HSB modifiers
24
+ */
25
+ hsb?: {
26
+ hue?: ColorScaleProcessingTarget;
27
+ saturation?: ColorScaleProcessingTarget;
28
+ brightness?: ColorScaleProcessingTarget;
29
+ };
30
+ /**
31
+ * Processing HCL modifiers
32
+ */
33
+ hcl?: {
34
+ hue?: ColorScaleProcessingTarget;
35
+ chroma?: ColorScaleProcessingTarget;
36
+ luminance?: ColorScaleProcessingTarget;
37
+ };
38
+ };
39
+ /**
40
+ * Utility class for generating and processing color scales
41
+ *
42
+ * @exports
43
+ * @class ColorScale
44
+ */
45
+ export default class ColorScale {
46
+ readonly isColorScale = true;
47
+ readonly type: string;
48
+ /**
49
+ * Array of colors composing this color scale
50
+ */
51
+ colors: Color[];
52
+ [Symbol.iterator](): Iterator<Color>;
53
+ /**
54
+ * Pick an interpolated color from this color scale
55
+ *
56
+ * @param {number} t Normalized time value to interpolate
57
+ * @returns {Color} Interpolated color on this scale
58
+ */
59
+ getColor(t: number): Color;
60
+ /**
61
+ * Set this color scale colors to an array of interpolated colors
62
+ *
63
+ * @param {ColorInterpolation} interpolation Type of interpolation used for generation
64
+ * @param {number} length Amount of colors to generate
65
+ * @param {Color} color1 Start color
66
+ * @param {Color} color2 End color
67
+ * @param {object} [params] Interpolation parameters
68
+ * @returns {this}
69
+ */
70
+ generate<I extends ColorInterpolation>(interpolation: I, length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters[I]): this;
71
+ /**
72
+ * Set this color scale colors to an array of interpolated colors in the RGB color space
73
+ *
74
+ * @param {number} length Amount of colors to generate
75
+ * @param {Color} color1 Start color
76
+ * @param {Color} color2 End color
77
+ * @param {object} [params] Interpolation parameters
78
+ * @param {number} [params.power] Interpolation exponent
79
+ * @returns {this}
80
+ */
81
+ generateRgb(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['rgb']): this;
82
+ /**
83
+ * Set this color scale colors to an array of interpolated colors in the HSL color space
84
+ *
85
+ * @param {number} length Amount of colors to generate
86
+ * @param {Color} color1 Start color
87
+ * @param {Color} color2 End color
88
+ * @param {object} [params] Interpolation parameters
89
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [h, s, l]
90
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
91
+ * @returns {this}
92
+ */
93
+ generateHsl(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['hsl']): this;
94
+ /**
95
+ * Set this color scale colors to an array of interpolated colors in the HSB color space
96
+ *
97
+ * @param {number} length Amount of colors to generate
98
+ * @param {Color} color1 Start color
99
+ * @param {Color} color2 End color
100
+ * @param {object} [params] Interpolation parameters
101
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [h, s, b]
102
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
103
+ * @returns {this}
104
+ */
105
+ generateHsb(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['hsb']): this;
106
+ /**
107
+ * Set this color scale colors to an array of interpolated colors following HCL Qualitative color palettes algorithm
108
+ *
109
+ * @param {number} length Amount of colors to generate
110
+ * @param {Color} color1 Start color
111
+ * @param {Color} color2 End color
112
+ * @param {object} [params] Interpolation parameters
113
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
114
+ * @returns {this}
115
+ */
116
+ generateQualitative(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['qualitative']): this;
117
+ /**
118
+ * Set this color scale colors to an array of interpolated colors following HCL Sequential color palettes algorithm
119
+ *
120
+ * @param {number} length Amount of colors to generate
121
+ * @param {Color} color1 Start color
122
+ * @param {Color} color2 End color
123
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [c, l]
124
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
125
+ * @param {number} [params.chromaMax] Maximum chroma value
126
+ * @returns {this}
127
+ */
128
+ generateSequential(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['sequential']): this;
129
+ /**
130
+ * Set this color scale colors to an array of interpolated colors following HCL Diverging color palettes algorithm
131
+ *
132
+ * @param {number} length Amount of colors to generate
133
+ * @param {Color} color1 Start color
134
+ * @param {Color} color2 End color
135
+ * @param {object} [params] Interpolation parameters
136
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : ([c, l])
137
+ * @returns {this}
138
+ */
139
+ generateDiverging(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['diverging']): this;
140
+ /**
141
+ * Process all colors composing this scale
142
+ *
143
+ * @param {ColorScaleProcessingParameters} [params] Processing parameters
144
+ * @returns {this}
145
+ */
146
+ process(params?: ColorScaleProcessingParameters): this;
147
+ /**
148
+ * Amount of colors composing this color scale
149
+ */
150
+ get length(): number;
151
+ /**
152
+ * Generate an array of interpolated colors
153
+ *
154
+ * @param {ColorInterpolation} interpolation Type of interpolation used for generation
155
+ * @param {number} length Amount of colors to generate
156
+ * @param {Color} color1 Start color
157
+ * @param {Color} color2 End color
158
+ * @param {object} [params] Interpolation parameters
159
+ * @returns {Color[]} Generated color scale
160
+ */
161
+ static generate<I extends ColorInterpolation>(interpolation: I, length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters[I]): Color[];
162
+ /**
163
+ * Generate an array of interpolated colors in the RGB color space
164
+ *
165
+ * @param {number} length Amount of colors to generate
166
+ * @param {Color} color1 Start color
167
+ * @param {Color} color2 End color
168
+ * @param {object} [params] Interpolation parameters
169
+ * @param {number} [params.power] Interpolation exponent
170
+ * @returns {Color[]} Generated RGB color scale
171
+ */
172
+ static generateRgb(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['rgb']): Color[];
173
+ /**
174
+ * Generate an array of interpolated colors in the HSL color space
175
+ *
176
+ * @param {number} length Amount of colors to generate
177
+ * @param {Color} color1 Start color
178
+ * @param {Color} color2 End color
179
+ * @param {object} [params] Interpolation parameters
180
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [h, s, l]
181
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
182
+ * @returns {Color[]} Generated HSL color scale
183
+ */
184
+ static generateHsl(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['hsl']): Color[];
185
+ /**
186
+ * Generate an array of interpolated colors in the HSB color space
187
+ *
188
+ * @param {number} length Amount of colors to generate
189
+ * @param {Color} color1 Start color
190
+ * @param {Color} color2 End color
191
+ * @param {object} [params] Interpolation parameters
192
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [h, s, b]
193
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
194
+ * @returns {Color[]} Generated HSB color scale
195
+ */
196
+ static generateHsb(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['hsb']): Color[];
197
+ /**
198
+ * Generate an array of interpolated colors following HCL Qualitative color palettes algorithm
199
+ *
200
+ * @param {number} length Amount of colors to generate
201
+ * @param {Color} color1 Start color
202
+ * @param {Color} color2 End color
203
+ * @param {object} [params] Interpolation parameters
204
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
205
+ * @returns {Color[]} Generated qualitative color scale
206
+ */
207
+ static generateQualitative(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['qualitative']): Color[];
208
+ /**
209
+ * Generate an array of interpolated colors following HCL Sequential color palettes algorithm
210
+ *
211
+ * @param {number} length Amount of colors to generate
212
+ * @param {Color} color1 Start color
213
+ * @param {Color} color2 End color
214
+ * @param {object} [params] Interpolation parameters
215
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [c, l]
216
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
217
+ * @param {number} [params.chromaMax] Maximum chroma value
218
+ * @returns {Color[]} Generated sequential color scale
219
+ */
220
+ static generateSequential(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['sequential']): Color[];
221
+ /**
222
+ * Generate an array of interpolated colors following HCL Diverging color palettes algorithm
223
+ *
224
+ * @param {number} length Amount of colors to generate
225
+ * @param {Color} color1 Start color
226
+ * @param {Color} color2 End color
227
+ * @param {object} [params] Interpolation parameters
228
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : ([c, l])
229
+ * @returns {Color[]} Generated diverging color scale
230
+ */
231
+ static generateDiverging(length: number, color1: Color, color2: Color, params?: ColorInterpolationsParameters['diverging']): Color[];
232
+ /**
233
+ * Process a given value
234
+ *
235
+ * @param {number} value Value to process
236
+ * @param {ColorScaleProcessingTarget} [target] Processing target
237
+ * @returns {number} Processed value
238
+ */
239
+ static processValue(value: number, target: ColorScaleProcessingTarget): number;
240
+ /**
241
+ * Process a given color
242
+ *
243
+ * @param {Color} color Color to process
244
+ * @param {ColorScaleProcessingParameters} [params] Processing parameters
245
+ * @returns {Color} Processed color
246
+ */
247
+ static processColor(color: Color, params?: ColorScaleProcessingParameters): Color;
248
+ /**
249
+ * Process all colors composing a given scale
250
+ *
251
+ * @param {ColorScale} scale Color scale to process
252
+ * @param {ColorScaleProcessingParameters} [params] Processing parameters
253
+ * @returns {ColorScale} Processed color scale
254
+ */
255
+ static process(scale: ColorScale, params?: ColorScaleProcessingParameters): ColorScale;
256
+ private static _getInterpolateFunction;
257
+ }