@versatiles/style 5.9.0 → 5.9.2

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 (56) hide show
  1. package/README.md +1 -0
  2. package/dist/index.d.ts +2 -2
  3. package/dist/index.js +4 -11
  4. package/dist/index.js.map +1 -1
  5. package/package.json +2 -3
  6. package/src/color/abstract.ts +0 -202
  7. package/src/color/hsl.test.ts +0 -176
  8. package/src/color/hsl.ts +0 -193
  9. package/src/color/hsv.test.ts +0 -169
  10. package/src/color/hsv.ts +0 -189
  11. package/src/color/index.test.ts +0 -187
  12. package/src/color/index.ts +0 -37
  13. package/src/color/random.test.ts +0 -111
  14. package/src/color/random.ts +0 -269
  15. package/src/color/rgb.test.ts +0 -218
  16. package/src/color/rgb.ts +0 -371
  17. package/src/color/utils.test.ts +0 -85
  18. package/src/color/utils.ts +0 -17
  19. package/src/guess_style/guess_style.test.ts +0 -140
  20. package/src/guess_style/guess_style.ts +0 -251
  21. package/src/guess_style/index.ts +0 -2
  22. package/src/index.test.ts +0 -131
  23. package/src/index.ts +0 -123
  24. package/src/lib/utils.test.ts +0 -281
  25. package/src/lib/utils.ts +0 -123
  26. package/src/shortbread/index.ts +0 -2
  27. package/src/shortbread/layers.test.ts +0 -81
  28. package/src/shortbread/layers.ts +0 -602
  29. package/src/shortbread/properties.test.ts +0 -44
  30. package/src/shortbread/properties.ts +0 -156
  31. package/src/shortbread/template.test.ts +0 -49
  32. package/src/shortbread/template.ts +0 -336
  33. package/src/style_builder/decorator.test.ts +0 -68
  34. package/src/style_builder/decorator.ts +0 -150
  35. package/src/style_builder/recolor.test.ts +0 -328
  36. package/src/style_builder/recolor.ts +0 -244
  37. package/src/style_builder/style_builder.test.ts +0 -148
  38. package/src/style_builder/style_builder.ts +0 -141
  39. package/src/style_builder/types.ts +0 -154
  40. package/src/styles/LICENSE.md +0 -41
  41. package/src/styles/colorful.test.ts +0 -91
  42. package/src/styles/colorful.ts +0 -1177
  43. package/src/styles/eclipse.ts +0 -11
  44. package/src/styles/empty.ts +0 -10
  45. package/src/styles/graybeard.ts +0 -11
  46. package/src/styles/index.ts +0 -34
  47. package/src/styles/neutrino.ts +0 -427
  48. package/src/styles/satellite.test.ts +0 -146
  49. package/src/styles/satellite.ts +0 -106
  50. package/src/styles/shadow.ts +0 -11
  51. package/src/types/index.ts +0 -5
  52. package/src/types/maplibre.ts +0 -25
  53. package/src/types/tilejson.test.ts +0 -96
  54. package/src/types/tilejson.ts +0 -147
  55. package/src/types/vector_layer.test.ts +0 -68
  56. package/src/types/vector_layer.ts +0 -67
@@ -1,169 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { HSV } from './hsv.js';
3
- import { HSL } from './hsl.js';
4
- import { RGB } from './rgb.js';
5
-
6
- describe('HSV Class', () => {
7
- it('constructor initializes values correctly with clamping', () => {
8
- const color = new HSV(400, 120, 120, 2);
9
- expect(color.asArray()).toStrictEqual([40, 100, 100, 1]);
10
-
11
- const colorNegative = new HSV(-60, -10, -10, -1);
12
- expect(colorNegative.asArray()).toStrictEqual([300, 0, 0, 0]);
13
- });
14
-
15
- it('asArray returns correct array representation', () => {
16
- const color = new HSV(120, 50, 50, 0.5);
17
- expect(color.asArray()).toStrictEqual([120, 50, 50, 0.5]);
18
- });
19
-
20
- it('clone returns a new instance with identical values', () => {
21
- const color = new HSV(180, 70, 70, 0.7);
22
- const clone = color.clone();
23
- expect(clone).toBeInstanceOf(HSV);
24
- expect(clone).toEqual(color);
25
- expect(clone).not.toBe(color);
26
- });
27
-
28
- describe('asString', () => {
29
- it('converts fully saturated colors correctly', () => {
30
- expect(new HSV(0, 100, 100).asString()).toBe('hsl(0,100%,50%)');
31
- expect(new HSV(120, 100, 100).asString()).toBe('hsl(120,100%,50%)');
32
- expect(new HSV(240, 100, 100).asString()).toBe('hsl(240,100%,50%)');
33
- });
34
-
35
- it('handles partially saturated colors', () => {
36
- expect(new HSV(60, 50, 100).asString()).toBe('hsl(60,100%,75%)');
37
- expect(new HSV(300, 25, 50).asString()).toBe('hsl(300,14%,44%)');
38
- });
39
-
40
- it('handles achromatic (grey) colors', () => {
41
- expect(new HSV(0, 0, 0).asString()).toBe('hsl(0,0%,0%)');
42
- expect(new HSV(0, 0, 50).asString()).toBe('hsl(0,0%,50%)');
43
- expect(new HSV(0, 0, 100).asString()).toBe('hsl(0,0%,100%)');
44
- });
45
-
46
- it('handles hue wrapping and extreme values', () => {
47
- expect(new HSV(-60, 100, 100).asString()).toBe('hsl(300,100%,50%)');
48
- expect(new HSV(420, 100, 100).asString()).toBe('hsl(60,100%,50%)');
49
- });
50
-
51
- it('handles alpha transparency', () => {
52
- expect(new HSV(0, 100, 100, 0.5).asString()).toBe('hsla(0,100%,50%,0.5)');
53
- expect(new HSV(120, 100, 100, 0.25).asString()).toBe('hsla(120,100%,50%,0.25)');
54
- expect(new HSV(240, 100, 100, 1).asString()).toBe('hsl(240,100%,50%)');
55
- });
56
-
57
- it('produces consistent results for repeated calls', () => {
58
- const color = new HSV(60, 50, 50);
59
- expect(color.asString()).toBe('hsl(60,33%,38%)');
60
- expect(color.asString()).toBe('hsl(60,33%,38%)');
61
- });
62
- });
63
-
64
- describe('color conversion', () => {
65
- it('asHSL converts HSV to HSL correctly', () => {
66
- function check(input: [number, number, number], output: [number, number, number]) {
67
- const hsv = new HSV(...input);
68
- const hsl = hsv.asHSL();
69
- expect(hsl).toBeInstanceOf(HSL);
70
- expect(hsl.asArray().map(Math.round)).toStrictEqual([...output, 1]);
71
-
72
- expect(hsv.asHex()).toStrictEqual(hsl.asHex());
73
- }
74
-
75
- check([10, 0, 0], [10, 0, 0]);
76
- check([11, 0, 50], [11, 0, 50]);
77
- check([12, 0, 100], [12, 0, 100]);
78
- check([13, 50, 0], [13, 0, 0]);
79
- check([14, 50, 50], [14, 33, 38]);
80
- check([15, 50, 100], [15, 100, 75]);
81
- check([16, 100, 0], [16, 0, 0]);
82
- check([17, 100, 50], [17, 100, 25]);
83
- check([18, 100, 100], [18, 100, 50]);
84
- });
85
-
86
- it('asRGB converts HSV to RGB correctly', () => {
87
- const color = new HSV(120, 100, 100);
88
- const rgb = color.asRGB();
89
- expect(rgb).toBeInstanceOf(RGB);
90
- expect(rgb.asArray().map((value) => Math.round(value))).toStrictEqual([0, 255, 0, 1]);
91
- });
92
-
93
- it('asHSV and toHSV return the same instance or clone', () => {
94
- const color = new HSV(240, 100, 50, 1);
95
- expect(color.asHSV()).toStrictEqual(color);
96
- expect(color.toHSV()).toStrictEqual(color);
97
- });
98
-
99
- it('asRGB conversion S and V', () => {
100
- function check(input: [number, number, number], output: [number, number, number]) {
101
- const color = new HSV(...input);
102
- const rgb = color.asRGB();
103
- expect(rgb.asArray().map((value) => Math.round(value))).toStrictEqual([...output, 1]);
104
- }
105
-
106
- check([10, 0, 0], [0, 0, 0]);
107
- check([11, 0, 50], [128, 128, 128]);
108
- check([12, 0, 100], [255, 255, 255]);
109
- check([13, 50, 0], [0, 0, 0]);
110
- check([14, 50, 50], [128, 79, 64]);
111
- check([15, 50, 100], [255, 159, 128]);
112
- check([16, 100, 0], [0, 0, 0]);
113
- check([17, 100, 50], [128, 36, 0]);
114
- check([18, 100, 100], [255, 77, 0]);
115
- });
116
-
117
- it('asRGB conversion H', () => {
118
- function check(hue: number, output: string) {
119
- const color = new HSV(hue, 100, 100);
120
- expect(color.asRGB().asHex()).toBe(output);
121
- }
122
-
123
- check(-1, '#FF0004');
124
- check(0, '#FF0000');
125
- check(1, '#FF0400');
126
- check(30, '#FF8000');
127
- check(60, '#FFFF00');
128
- check(90, '#80FF00');
129
- check(120, '#00FF00');
130
- check(150, '#00FF80');
131
- check(180, '#00FFFF');
132
- check(210, '#0080FF');
133
- check(240, '#0000FF');
134
- check(270, '#8000FF');
135
- check(300, '#FF00FF');
136
- check(330, '#FF0080');
137
- check(359, '#FF0004');
138
- check(360, '#FF0000');
139
- check(361, '#FF0400');
140
- });
141
- });
142
-
143
- describe('parse errors and validations', () => {
144
- it('constructor clamps out-of-bound values', () => {
145
- const color = new HSV(400, 150, 150, 2);
146
- expect(color.asArray()).toStrictEqual([40, 100, 100, 1]);
147
- });
148
-
149
- it('negative values are handled correctly', () => {
150
- const color = new HSV(-360, -50, -50, -1);
151
- expect(color.asArray()).toStrictEqual([0, 0, 0, 0]);
152
- });
153
- });
154
-
155
- describe('fade', () => {
156
- it('reduces alpha correctly', () => {
157
- const color = new HSV(120, 50, 50, 0.8);
158
- expect(color.fade(0.5).asArray()).toStrictEqual([120, 50, 50, 0.4]); // Alpha reduced by 50%
159
- });
160
-
161
- it('handles edge cases for fading', () => {
162
- const opaque = new HSV(0, 50, 50, 1);
163
- expect(opaque.fade(1).asArray()).toStrictEqual([0, 50, 50, 0]); // Fully faded to transparent
164
-
165
- const transparent = new HSV(0, 50, 50, 0);
166
- expect(transparent.fade(0.5).asArray()).toStrictEqual([0, 50, 50, 0]); // Remains fully transparent
167
- });
168
- });
169
- });
package/src/color/hsv.ts DELETED
@@ -1,189 +0,0 @@
1
- import { Color } from './abstract.js';
2
- import { HSL } from './hsl.js';
3
- import randomColor, { RandomColorOptions } from './random.js';
4
- import { RGB } from './rgb.js';
5
- import { clamp, mod } from './utils.js';
6
-
7
- /**
8
- * Represents a color in the HSV (Hue, Saturation, Value) color space.
9
- * Extends the base `Color` class.
10
- */
11
- export class HSV extends Color {
12
- /**
13
- * The hue component of the color, in the range [0, 360].
14
- */
15
- readonly h: number;
16
-
17
- /**
18
- * The saturation component of the color, in the range [0, 100].
19
- */
20
- readonly s: number;
21
-
22
- /**
23
- * The value (brightness) component of the color, in the range [0, 100].
24
- */
25
- readonly v: number;
26
-
27
- /**
28
- * The alpha (opacity) component of the color, in the range [0, 1].
29
- */
30
- readonly a: number;
31
-
32
- /**
33
- * Constructs a new HSV color.
34
- * @param h - The hue component, in the range [0, 360].
35
- * @param s - The saturation component, in the range [0, 100].
36
- * @param v - The value (brightness) component, in the range [0, 100].
37
- * @param a - The alpha (opacity) component, in the range [0, 1]. Defaults to 1.
38
- */
39
- constructor(h: number, s: number, v: number, a: number = 1) {
40
- super();
41
- this.h = mod(h, 360);
42
- this.s = clamp(s, 0, 100);
43
- this.v = clamp(v, 0, 100);
44
- this.a = clamp(a, 0, 1);
45
- }
46
-
47
- /**
48
- * Returns the HSV color as an array of numbers.
49
- * @returns An array containing the hue, saturation, value, and alpha components.
50
- */
51
- asArray(): [number, number, number, number] {
52
- return [this.h, this.s, this.v, this.a];
53
- }
54
-
55
- /**
56
- * Returns a new HSV color with the components rounded to the nearest integer.
57
- * @returns A new HSV color with rounded components.
58
- */
59
- round(): HSV {
60
- return new HSV(Math.round(this.h), Math.round(this.s), Math.round(this.v), Math.round(this.a * 1000) / 1000);
61
- }
62
-
63
- /**
64
- * Returns the color as a string representation.
65
- * @returns A string representation of the color.
66
- */
67
- asString(): string {
68
- return this.asHSL().asString();
69
- }
70
-
71
- /**
72
- * Creates a new HSV color that is a copy of the current color.
73
- * @returns A new HSV color that is a clone of the current color.
74
- */
75
- clone(): HSV {
76
- return new HSV(this.h, this.s, this.v, this.a);
77
- }
78
-
79
- /**
80
- * Converts the HSV color to an HSL color.
81
- * @returns An HSL representation of the color.
82
- */
83
- asHSL(): HSL {
84
- const s = this.s / 100;
85
- const v = this.v / 100;
86
- const k = (2 - s) * v;
87
- const q = k < 1 ? k : 2 - k;
88
- return new HSL(this.h, q === 0 ? 0 : (100 * s * v) / q, (100 * k) / 2, this.a);
89
- }
90
-
91
- /**
92
- * Returns the current HSV color.
93
- * @returns The current HSV color.
94
- */
95
- asHSV(): HSV {
96
- return this.clone();
97
- }
98
-
99
- /**
100
- * Returns the current HSV color.
101
- * @returns The current HSV color.
102
- */
103
- toHSV(): HSV {
104
- return this;
105
- }
106
-
107
- /**
108
- * Converts the HSV color to an RGB color.
109
- * @returns An RGB representation of the color.
110
- */
111
- asRGB(): RGB {
112
- const h = this.h / 360; // Normalize h to range [0, 1]
113
- const s = this.s / 100; // Normalize s to range [0, 1]
114
- const v = this.v / 100; // Normalize v to range [0, 1]
115
-
116
- let r = 0,
117
- g = 0,
118
- b = 0;
119
-
120
- if (s === 0) {
121
- // Achromatic (grey)
122
- r = g = b = v;
123
- } else {
124
- const i = Math.floor(h * 6); // Determine the sector of the color wheel
125
- const f = h * 6 - i; // Fractional part of h * 6
126
- const p = v * (1 - s);
127
- const q = v * (1 - s * f);
128
- const t = v * (1 - s * (1 - f));
129
-
130
- switch (i % 6) {
131
- case 0:
132
- r = v;
133
- g = t;
134
- b = p;
135
- break;
136
- case 1:
137
- r = q;
138
- g = v;
139
- b = p;
140
- break;
141
- case 2:
142
- r = p;
143
- g = v;
144
- b = t;
145
- break;
146
- case 3:
147
- r = p;
148
- g = q;
149
- b = v;
150
- break;
151
- case 4:
152
- r = t;
153
- g = p;
154
- b = v;
155
- break;
156
- case 5:
157
- r = v;
158
- g = p;
159
- b = q;
160
- break;
161
- }
162
- }
163
-
164
- // Convert to RGB in the 0-255 range and return
165
- return new RGB(r * 255, g * 255, b * 255, this.a);
166
- }
167
-
168
- /**
169
- * Fades the color by a given value.
170
- * @param value - The amount to fade the color by, in the range [0, 1].
171
- * @returns A new HSV color with the alpha component faded by the given value.
172
- */
173
- fade(value: number): HSV {
174
- return new HSV(this.h, this.s, this.v, this.a * (1 - value));
175
- }
176
-
177
- /**
178
- * Sets the hue component of the color.
179
- * @param value - The new hue value, in the range [0, 360].
180
- * @returns A new HSV color with the updated hue component.
181
- */
182
- setHue(value: number): HSV {
183
- return new HSV(value, this.s, this.v, this.a);
184
- }
185
-
186
- static randomColor(options?: RandomColorOptions): HSV {
187
- return randomColor(options);
188
- }
189
- }
@@ -1,187 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { Color } from './index.js';
3
- import { HSL } from './hsl.js';
4
- import { HSV } from './hsv.js';
5
- import { RGB } from './rgb.js';
6
-
7
- describe('Color Conversions', () => {
8
- const scenarios: [number, number, number, number][] = [
9
- [-100, 14, 15, 0],
10
- [0, 0, 0, 0.1],
11
- [100, 0, 50, 0.2],
12
- [200, 0, 100, 0.3],
13
- [300, 50, 0, 0.4],
14
- [400, 50, 50, 0.5],
15
- [500, 50, 100, 0.6],
16
- [600, 100, 0, 0.7],
17
- [700, 100, 50, 0.8],
18
- [800, 100, 100, 0.9],
19
- [900, 12, 13, 1.0],
20
- ];
21
- it('test HSV -> HSL -> RGB', () => {
22
- for (const v of scenarios) {
23
- const hsv = new HSV(...v);
24
- expect(hsv.a).toEqual(v[3]);
25
- const hsl = hsv.asHSL();
26
- const a1 = hsv.asRGB().asArray();
27
- const a2 = hsl.asRGB().asArray();
28
- for (let i = 0; i < 4; i++) expect(a1[i]).toBeCloseTo(a2[i]);
29
- }
30
- });
31
-
32
- it('test HSL -> HSV -> RGB', () => {
33
- for (const v of scenarios) {
34
- const hsl = new HSL(...v);
35
- expect(hsl.a).toEqual(v[3]);
36
- const hsv = hsl.asHSV();
37
- const a1 = hsv.asRGB().asArray();
38
- const a2 = hsl.asRGB().asArray();
39
- for (let i = 0; i < 4; i++) expect(a1[i]).toBeCloseTo(a2[i]);
40
- }
41
- });
42
- });
43
-
44
- describe('Color.parse', () => {
45
- it('parses hexadecimal color strings correctly', () => {
46
- const color = Color.parse('#ff8040');
47
- expect(color).toBeInstanceOf(RGB);
48
- expect(color.asArray()).toStrictEqual([255, 128, 64, 1]);
49
-
50
- const colorWithAlpha = Color.parse('#ff80407f');
51
- expect(colorWithAlpha).toBeInstanceOf(RGB);
52
- expect(colorWithAlpha.asHex()).toStrictEqual('#FF80407F');
53
- });
54
-
55
- it('parses RGB strings correctly', () => {
56
- const color = Color.parse('rgb(255, 128, 64)');
57
- expect(color).toBeInstanceOf(RGB);
58
- expect(color.asArray()).toStrictEqual([255, 128, 64, 1]);
59
-
60
- const colorWithAlpha = Color.parse('rgba(255, 128, 64, 0.5)');
61
- expect(colorWithAlpha).toBeInstanceOf(RGB);
62
- expect(colorWithAlpha.asArray()).toStrictEqual([255, 128, 64, 0.5]);
63
- });
64
-
65
- it('parses HSL strings correctly', () => {
66
- const color = Color.parse('hsl(120, 50%, 50%)');
67
- expect(color).toBeInstanceOf(HSL);
68
- expect(color.asArray()).toStrictEqual([120, 50, 50, 1]);
69
-
70
- const colorWithAlpha = Color.parse('hsla(120, 50%, 50%, 0.5)');
71
- expect(colorWithAlpha).toBeInstanceOf(HSL);
72
- expect(colorWithAlpha.asArray()).toStrictEqual([120, 50, 50, 0.5]);
73
- });
74
-
75
- it('throws an error for unsupported formats', () => {
76
- expect(() => Color.parse('invalid color string')).toThrow(
77
- 'Color.parse: Unknown color format "invalid color string"'
78
- );
79
- });
80
- });
81
-
82
- describe('Color Class Properties', () => {
83
- it('Color.HSL is accessible', () => {
84
- expect(Color.HSL).toBe(HSL);
85
- });
86
-
87
- it('Color.HSV is accessible', () => {
88
- expect(Color.HSV).toBe(HSV);
89
- });
90
-
91
- it('Color.RGB is accessible', () => {
92
- expect(Color.RGB).toBe(RGB);
93
- });
94
- });
95
-
96
- describe('Exported Module', () => {
97
- it('named export is Color', async () => {
98
- const module = await import('./index.js');
99
- expect(module.Color).toBe(Color);
100
- });
101
- });
102
-
103
- describe('Color Transformation Methods', () => {
104
- it('gamma() applies gamma correction', () => {
105
- const color = Color.parse('#808080');
106
- expect(color.gamma(2.2).asString()).toBe('rgb(56,56,56)');
107
- });
108
-
109
- it('gamma() works from HSL', () => {
110
- const hsl = new HSL(120, 50, 50, 1);
111
- expect(hsl.gamma(2.2).asString()).toBe('rgb(12,135,12)');
112
- });
113
-
114
- it('contrast() adjusts contrast', () => {
115
- const color = Color.parse('#FF8040');
116
- expect(color.contrast(1.5).asString()).toBe('rgb(255,128,32)');
117
- });
118
-
119
- it('contrast() works from HSV', () => {
120
- const hsv = new HSV(180, 50, 50, 1);
121
- expect(hsv.contrast(1.5).asString()).toBe('rgb(32,128,128)');
122
- });
123
-
124
- it('brightness() works from HSL', () => {
125
- const hsl = new HSL(240, 100, 50, 1);
126
- expect(hsl.brightness(0.3).asString()).toBe('rgb(77,77,255)');
127
- });
128
-
129
- it('lighten() works from HSL', () => {
130
- const hsl = new HSL(0, 100, 30, 1);
131
- expect(hsl.lighten(0.2).asString()).toBe('rgb(173,51,51)');
132
- });
133
-
134
- it('darken() works from HSV', () => {
135
- const hsv = new HSV(60, 100, 80, 1);
136
- expect(hsv.darken(0.3).asString()).toBe('rgb(143,143,0)');
137
- });
138
-
139
- it('tint() blends with a tint color', () => {
140
- const color = Color.parse('#FF0000');
141
- const tintColor = Color.parse('#0000FF');
142
- expect(color.tint(0.5, tintColor).asString()).toBe('rgb(128,0,128)');
143
- });
144
-
145
- it('tint() works from HSL', () => {
146
- const hsl = new HSL(0, 100, 50, 1);
147
- const tintColor = new HSL(120, 100, 50, 1);
148
- expect(hsl.tint(0.5, tintColor).asString()).toBe('rgb(128,128,0)');
149
- });
150
-
151
- it('blend() blends with another color', () => {
152
- const color1 = Color.parse('#FF0000');
153
- const color2 = Color.parse('#0000FF');
154
- expect(color1.blend(0.5, color2).asString()).toBe('rgb(128,0,128)');
155
- });
156
-
157
- it('blend() works from HSV', () => {
158
- const hsv1 = new HSV(0, 100, 100, 1);
159
- const hsv2 = new HSV(240, 100, 100, 1);
160
- expect(hsv1.blend(0.3, hsv2).asString()).toBe('rgb(179,0,77)');
161
- });
162
-
163
- it('setHue() changes the hue', () => {
164
- const color = Color.parse('#FF0000');
165
- expect(color.setHue(180).asString()).toBe('hsl(180,100%,50%)');
166
- });
167
-
168
- it('invertLuminosity() inverts from HSV', () => {
169
- const hsv = new HSV(200, 50, 60, 1);
170
- expect(hsv.invertLuminosity().asString()).toBe('hsl(200,33%,55%)');
171
- });
172
-
173
- it('rotateHue() rotates from RGB', () => {
174
- const rgb = new RGB(255, 0, 0, 1);
175
- expect(rgb.rotateHue(120).asString()).toBe('hsl(120,100%,50%)');
176
- });
177
-
178
- it('saturate() increases saturation from HSV', () => {
179
- const hsv = new HSV(100, 30, 70, 1);
180
- expect(hsv.saturate(1.5).asString()).toBe('hsl(100,65%,60%)');
181
- });
182
-
183
- it('invert() inverts from HSL', () => {
184
- const hsl = new HSL(180, 100, 50, 1);
185
- expect(hsl.invert().asString()).toBe('rgb(255,0,0)');
186
- });
187
- });
@@ -1,37 +0,0 @@
1
- import { Color } from './abstract.js';
2
- import { HSL } from './hsl.js';
3
- import { HSV } from './hsv.js';
4
- import { RGB } from './rgb.js';
5
-
6
- Color.parse = function (input: string | Color): Color {
7
- if (input instanceof Color) return input;
8
-
9
- input = input.trim().toLowerCase();
10
-
11
- if (input.startsWith('#')) return RGB.parse(input);
12
-
13
- const prefix = input.replace(/\d.*/, '').trim().toLowerCase();
14
-
15
- switch (prefix) {
16
- case 'rgb(':
17
- case 'rgba(':
18
- return RGB.parse(input);
19
- case 'hsl(':
20
- case 'hsla(':
21
- return HSL.parse(input);
22
- default:
23
- throw new Error(
24
- `Color.parse: Unknown color format "${input}". Expected formats: "#RRGGBB", "#RGB", "rgb(...)", "rgba(...)", "hsl(...)", or "hsla(...)".`
25
- );
26
- }
27
- };
28
-
29
- Color.HSL = HSL;
30
- Color.HSV = HSV;
31
- Color.RGB = RGB;
32
-
33
- export type { RandomColorOptions } from './random.js';
34
- export type { HSL } from './hsl.js';
35
- export type { HSV } from './hsv.js';
36
- export type { RGB } from './rgb.js';
37
- export { Color };
@@ -1,111 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { HSV } from './hsv.js';
3
- import type { RandomColorOptions } from './random.js';
4
- import randomColor from './random.js';
5
-
6
- describe('RandomColor', () => {
7
- it('constructor initializes without errors', () => {
8
- expect(randomColor).toBeDefined();
9
- });
10
-
11
- describe('Color.random', () => {
12
- it('generates random HSV colors', () => {
13
- const random = randomColor();
14
- expect(random).toBeInstanceOf(HSV);
15
- const array = random.asArray();
16
- expect(array[0]).toBeGreaterThanOrEqual(0);
17
- expect(array[0]).toBeLessThanOrEqual(360);
18
- expect(array[1]).toBeGreaterThanOrEqual(0);
19
- expect(array[1]).toBeLessThanOrEqual(100);
20
- expect(array[2]).toBeGreaterThanOrEqual(0);
21
- expect(array[2]).toBeLessThanOrEqual(100);
22
- });
23
-
24
- it('supports options for generating random colors', () => {
25
- const random = randomColor({ hue: 'red', luminosity: 'bright' });
26
- expect(random).toBeInstanceOf(HSV);
27
- // Additional checks based on the options provided can be added here
28
- });
29
- });
30
-
31
- describe('randomColor method', () => {
32
- it('returns correct color string for some test cases', () => {
33
- function t(options: RandomColorOptions): string {
34
- return randomColor(options).asHSL().asString();
35
- }
36
- expect(t({ seed: 'testSeed', hue: 'red' })).toBe('hsl(356,90%,30%)');
37
- expect(t({ seed: 'testSeed', hue: 120 })).toBe('hsl(120,92%,26%)');
38
- expect(t({ seed: 'testSeed', luminosity: 'dark' })).toBe('hsl(185,98%,19%)');
39
- expect(t({ seed: 'testSeed', luminosity: 12 })).toBe('hsl(185,90%,6%)');
40
- expect(t({ seed: 'testSeed', saturation: 'strong' })).toBe('hsl(185,100%,48%)');
41
- expect(t({ seed: 'testSeed', opacity: 0.5 })).toBe('hsla(185,90%,23%,0.5)');
42
- expect(t({ seed: 'testSeed' })).toBe('hsl(185,90%,23%)');
43
- });
44
-
45
- it('generates light colors with luminosity: "light"', () => {
46
- const color = randomColor({ seed: 'lightSeed', luminosity: 'light' });
47
- const hsv = color.asArray();
48
- // Light colors should have higher brightness values
49
- expect(hsv[2]).toBeGreaterThan(50);
50
- expect(color).toBeInstanceOf(HSV);
51
- });
52
-
53
- it('generates random luminosity colors with luminosity: "random"', () => {
54
- const color = randomColor({ seed: 'randomSeed', luminosity: 'random' });
55
- const hsv = color.asArray();
56
- // Random luminosity can be anywhere from 0-100
57
- expect(hsv[2]).toBeGreaterThanOrEqual(0);
58
- expect(hsv[2]).toBeLessThanOrEqual(100);
59
- expect(color).toBeInstanceOf(HSV);
60
- });
61
-
62
- it('generates light saturation with luminosity: "light"', () => {
63
- // Test light luminosity affects saturation picking
64
- const color1 = randomColor({ seed: 'lightTest1', luminosity: 'light', hue: 'blue' });
65
- const color2 = randomColor({ seed: 'lightTest2', luminosity: 'light', hue: 'green' });
66
- expect(color1).toBeInstanceOf(HSV);
67
- expect(color2).toBeInstanceOf(HSV);
68
- });
69
-
70
- it('generates colors with various saturation options', () => {
71
- const weak = randomColor({ seed: 'satTest', saturation: 'weak' });
72
- const strong = randomColor({ seed: 'satTest', saturation: 'strong' });
73
-
74
- expect(weak).toBeInstanceOf(HSV);
75
- expect(strong).toBeInstanceOf(HSV);
76
- // Strong saturation should have higher saturation values
77
- expect(strong.s).toBeGreaterThan(80);
78
- });
79
-
80
- it('generates colors with all hue name options', () => {
81
- const hues: Array<string | number> = [
82
- 'red',
83
- 'orange',
84
- 'yellow',
85
- 'green',
86
- 'blue',
87
- 'purple',
88
- 'pink',
89
- 'monochrome',
90
- 180,
91
- ];
92
-
93
- hues.forEach((hue) => {
94
- const color = randomColor({ seed: `hue-${hue}`, hue });
95
- expect(color).toBeInstanceOf(HSV);
96
- });
97
- });
98
-
99
- it('consistent color generation with a seed', () => {
100
- const color1 = randomColor({ seed: 'consistentSeed' });
101
- const color2 = randomColor({ seed: 'consistentSeed' });
102
- expect(color1.asHex()).toBe(color2.asHex());
103
- });
104
-
105
- it('different color generation without a seed', () => {
106
- const color1 = randomColor({ seed: 'seed1' });
107
- const color2 = randomColor({ seed: 'seed2' });
108
- expect(color1.asHex()).not.toBe(color2.asHex());
109
- });
110
- });
111
- });