@versatiles/style 5.5.2 → 5.7.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/README.MD +36 -33
- package/dist/index.d.ts +539 -65
- package/dist/index.js +782 -217
- package/dist/index.js.map +1 -1
- package/package.json +26 -25
- package/src/color/abstract.ts +145 -26
- package/src/color/hsl.ts +88 -5
- package/src/color/hsv.ts +73 -5
- package/src/color/index.test.ts +0 -20
- package/src/color/index.ts +0 -3
- package/src/color/random.test.ts +21 -0
- package/src/color/rgb.test.ts +40 -72
- package/src/color/rgb.ts +152 -7
- package/src/color/utils.ts +10 -4
- package/src/guess_style/guess_style.test.ts +1 -1
- package/src/guess_style/guess_style.ts +42 -2
- package/src/index.test.ts +2 -1
- package/src/index.ts +89 -4
- package/src/shortbread/layers.ts +30 -5
- package/src/style_builder/recolor.test.ts +46 -0
- package/src/style_builder/recolor.ts +161 -34
- package/src/style_builder/style_builder.test.ts +2 -0
- package/src/style_builder/style_builder.ts +8 -9
- package/src/style_builder/types.ts +23 -58
- package/src/styles/colorful.ts +36 -7
- package/src/styles/index.ts +2 -0
- package/src/styles/shadow.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versatiles/style",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.7.0",
|
|
4
4
|
"description": "Generate StyleJSON for MapLibre",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -32,44 +32,45 @@
|
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"type": "module",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"
|
|
35
|
+
"@versatiles/style": "^5.6.0",
|
|
36
|
+
"brace-expansion": "^4.0.1"
|
|
36
37
|
},
|
|
37
38
|
"files": [
|
|
38
39
|
"dist/*",
|
|
39
40
|
"src/*"
|
|
40
41
|
],
|
|
41
42
|
"devDependencies": {
|
|
42
|
-
"@maplibre/maplibre-gl-native": "^6.
|
|
43
|
-
"@maplibre/maplibre-gl-style-spec": "^23.
|
|
44
|
-
"@rollup/plugin-commonjs": "^28.0.
|
|
45
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
43
|
+
"@maplibre/maplibre-gl-native": "^6.1.0",
|
|
44
|
+
"@maplibre/maplibre-gl-style-spec": "^23.3.0",
|
|
45
|
+
"@rollup/plugin-commonjs": "^28.0.6",
|
|
46
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
46
47
|
"@rollup/plugin-terser": "^0.4.4",
|
|
47
|
-
"@rollup/plugin-typescript": "^12.1.
|
|
48
|
+
"@rollup/plugin-typescript": "^12.1.3",
|
|
48
49
|
"@types/bin-pack": "^1.0.3",
|
|
49
50
|
"@types/brace-expansion": "^1.1.2",
|
|
50
|
-
"@types/inquirer": "^9.0.
|
|
51
|
+
"@types/inquirer": "^9.0.8",
|
|
51
52
|
"@types/jest": "^29.5.14",
|
|
52
|
-
"@types/node": "^
|
|
53
|
-
"@types/tar-stream": "^3.1.
|
|
54
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
55
|
-
"@typescript-eslint/parser": "^8.
|
|
56
|
-
"@versatiles/release-tool": "^2.
|
|
53
|
+
"@types/node": "^24.0.4",
|
|
54
|
+
"@types/tar-stream": "^3.1.4",
|
|
55
|
+
"@typescript-eslint/eslint-plugin": "^8.35.0",
|
|
56
|
+
"@typescript-eslint/parser": "^8.35.0",
|
|
57
|
+
"@versatiles/release-tool": "^2.4.2",
|
|
57
58
|
"bin-pack": "^1.0.2",
|
|
58
|
-
"esbuild": "^0.25.
|
|
59
|
-
"eslint": "^9.
|
|
60
|
-
"inquirer": "^12.
|
|
59
|
+
"esbuild": "^0.25.5",
|
|
60
|
+
"eslint": "^9.29.0",
|
|
61
|
+
"inquirer": "^12.6.3",
|
|
61
62
|
"jest": "^29.7.0",
|
|
62
63
|
"jest-environment-jsdom": "^29.7.0",
|
|
63
|
-
"jest-ts-webcompat-resolver": "^1.0.
|
|
64
|
-
"rollup": "^4.
|
|
65
|
-
"rollup-plugin-dts": "^6.
|
|
66
|
-
"rollup-plugin-sourcemaps2": "^0.5.
|
|
67
|
-
"sharp": "^0.
|
|
64
|
+
"jest-ts-webcompat-resolver": "^1.0.1",
|
|
65
|
+
"rollup": "^4.44.0",
|
|
66
|
+
"rollup-plugin-dts": "^6.2.1",
|
|
67
|
+
"rollup-plugin-sourcemaps2": "^0.5.2",
|
|
68
|
+
"sharp": "^0.34.2",
|
|
68
69
|
"tar-stream": "^3.1.7",
|
|
69
|
-
"ts-jest": "^29.
|
|
70
|
+
"ts-jest": "^29.4.0",
|
|
70
71
|
"ts-node": "^10.9.2",
|
|
71
|
-
"tsx": "^4.
|
|
72
|
-
"typescript": "^5.8.
|
|
73
|
-
"typescript-eslint": "^8.
|
|
72
|
+
"tsx": "^4.20.3",
|
|
73
|
+
"typescript": "^5.8.3",
|
|
74
|
+
"typescript-eslint": "^8.35.0"
|
|
74
75
|
}
|
|
75
76
|
}
|
package/src/color/abstract.ts
CHANGED
|
@@ -1,83 +1,202 @@
|
|
|
1
1
|
import type { HSL } from './hsl.js';
|
|
2
2
|
import type { HSV } from './hsv.js';
|
|
3
|
-
import { RandomColorOptions } from './random.js';
|
|
4
3
|
import type { RGB } from './rgb.js';
|
|
5
4
|
|
|
5
|
+
/**
|
|
6
|
+
* The abstract `Color` class provides a blueprint for color manipulation and conversion.
|
|
7
|
+
* It includes methods for converting between different color models ({@link HSL}, {@link HSV}, {@link RGB}),
|
|
8
|
+
* as well as various color transformations such as inversion, rotation, saturation, and blending.
|
|
9
|
+
*
|
|
10
|
+
* @abstract
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Abstract class representing a color.
|
|
14
|
+
*/
|
|
6
15
|
export abstract class Color {
|
|
16
|
+
/**
|
|
17
|
+
* Parses a color from a string or another Color instance.
|
|
18
|
+
* @param input - The input color as a string or Color instance.
|
|
19
|
+
* @returns The parsed Color instance.
|
|
20
|
+
*/
|
|
7
21
|
static parse: (input: Color | string) => Color;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The HSL color model.
|
|
25
|
+
*/
|
|
8
26
|
static HSL: typeof HSL;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The HSV color model.
|
|
30
|
+
*/
|
|
9
31
|
static HSV: typeof HSV;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The RGB color model.
|
|
35
|
+
*/
|
|
10
36
|
static RGB: typeof RGB;
|
|
11
|
-
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Creates a clone of the current color instance.
|
|
40
|
+
* @returns A new Color instance that is a clone of the current instance.
|
|
41
|
+
*/
|
|
12
42
|
abstract clone(): Color;
|
|
13
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Converts the color to a hexadecimal string.
|
|
46
|
+
* @returns The hexadecimal representation of the color.
|
|
47
|
+
*/
|
|
14
48
|
asHex(): string {
|
|
15
|
-
return this.
|
|
49
|
+
return this.asRGB().asHex();
|
|
16
50
|
}
|
|
17
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Converts the color to a string representation.
|
|
54
|
+
* @returns The string representation of the color.
|
|
55
|
+
*/
|
|
18
56
|
abstract asString(): string;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Rounds the color values.
|
|
60
|
+
* @returns A new Color instance with rounded values.
|
|
61
|
+
*/
|
|
19
62
|
abstract round(): Color;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Converts the color to an array of numbers.
|
|
66
|
+
* @returns An array representing the color.
|
|
67
|
+
*/
|
|
20
68
|
abstract asArray(): number[];
|
|
21
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Converts the color to the HSL color model.
|
|
72
|
+
* @returns The HSL representation of the color.
|
|
73
|
+
*/
|
|
22
74
|
abstract asHSL(): HSL;
|
|
23
|
-
abstract asHSV(): HSV;
|
|
24
|
-
abstract asRGB(): RGB;
|
|
25
75
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return this.asHSV();
|
|
32
|
-
}
|
|
76
|
+
/**
|
|
77
|
+
* Converts the color to the HSV color model.
|
|
78
|
+
* @returns The HSV representation of the color.
|
|
79
|
+
*/
|
|
80
|
+
abstract asHSV(): HSV;
|
|
33
81
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Converts the color to the RGB color model.
|
|
84
|
+
* @returns The RGB representation of the color.
|
|
85
|
+
*/
|
|
86
|
+
abstract asRGB(): RGB;
|
|
37
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Inverts the luminosity of the color.
|
|
90
|
+
* @returns A new HSL color with inverted luminosity.
|
|
91
|
+
*/
|
|
38
92
|
invertLuminosity(): HSL {
|
|
39
|
-
return this.
|
|
93
|
+
return this.asHSL().invertLuminosity();
|
|
40
94
|
}
|
|
41
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Rotates the hue of the color by a given offset.
|
|
98
|
+
* @param offset - The amount to rotate the hue.
|
|
99
|
+
* @returns A new HSL color with the hue rotated.
|
|
100
|
+
*/
|
|
42
101
|
rotateHue(offset: number): HSL {
|
|
43
|
-
return this.
|
|
102
|
+
return this.asHSL().rotateHue(offset);
|
|
44
103
|
}
|
|
45
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Saturates the color by a given ratio.
|
|
107
|
+
* @param ratio - The ratio to saturate the color.
|
|
108
|
+
* @returns A new HSL color with increased saturation.
|
|
109
|
+
*/
|
|
46
110
|
saturate(ratio: number): HSL {
|
|
47
|
-
return this.
|
|
111
|
+
return this.asHSL().saturate(ratio);
|
|
48
112
|
}
|
|
49
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Applies gamma correction to the color.
|
|
116
|
+
* @param value - The gamma correction value.
|
|
117
|
+
* @returns A new RGB color with gamma correction applied.
|
|
118
|
+
*/
|
|
50
119
|
gamma(value: number): RGB {
|
|
51
|
-
return this.
|
|
120
|
+
return this.asRGB().gamma(value);
|
|
52
121
|
}
|
|
53
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Inverts the color.
|
|
125
|
+
* @returns A new RGB color with inverted values.
|
|
126
|
+
*/
|
|
54
127
|
invert(): RGB {
|
|
55
|
-
return this.
|
|
128
|
+
return this.asRGB().invert();
|
|
56
129
|
}
|
|
57
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Adjusts the contrast of the color.
|
|
133
|
+
* @param value - The contrast adjustment value.
|
|
134
|
+
* @returns A new RGB color with adjusted contrast.
|
|
135
|
+
*/
|
|
58
136
|
contrast(value: number): RGB {
|
|
59
|
-
return this.
|
|
137
|
+
return this.asRGB().contrast(value);
|
|
60
138
|
}
|
|
61
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Adjusts the brightness of the color.
|
|
142
|
+
* @param value - The brightness adjustment value.
|
|
143
|
+
* @returns A new RGB color with adjusted brightness.
|
|
144
|
+
*/
|
|
62
145
|
brightness(value: number): RGB {
|
|
63
|
-
return this.
|
|
146
|
+
return this.asRGB().brightness(value);
|
|
64
147
|
}
|
|
65
148
|
|
|
149
|
+
/**
|
|
150
|
+
* Lightens the color by a given value.
|
|
151
|
+
* @param value - The amount to lighten the color.
|
|
152
|
+
* @returns A new RGB color that is lightened.
|
|
153
|
+
*/
|
|
66
154
|
lighten(value: number): RGB {
|
|
67
|
-
return this.
|
|
155
|
+
return this.asRGB().lighten(value);
|
|
68
156
|
}
|
|
69
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Darkens the color by a given value.
|
|
160
|
+
* @param value - The amount to darken the color.
|
|
161
|
+
* @returns A new RGB color that is darkened.
|
|
162
|
+
*/
|
|
70
163
|
darken(value: number): RGB {
|
|
71
|
-
return this.
|
|
164
|
+
return this.asRGB().darken(value);
|
|
72
165
|
}
|
|
73
166
|
|
|
167
|
+
/**
|
|
168
|
+
* Tints the color by blending it with another color.
|
|
169
|
+
* @param value - The blend ratio.
|
|
170
|
+
* @param tintColor - The color to blend with.
|
|
171
|
+
* @returns A new RGB color that is tinted.
|
|
172
|
+
*/
|
|
74
173
|
tint(value: number, tintColor: Color): RGB {
|
|
75
|
-
return this.
|
|
174
|
+
return this.asRGB().tint(value, tintColor);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Blends the color with another color.
|
|
179
|
+
* @param value - The blend ratio.
|
|
180
|
+
* @param blendColor - The color to blend with.
|
|
181
|
+
* @returns A new RGB color that is blended.
|
|
182
|
+
*/
|
|
183
|
+
blend(value: number, blendColor: Color): RGB {
|
|
184
|
+
return this.asRGB().blend(value, blendColor);
|
|
76
185
|
}
|
|
77
186
|
|
|
187
|
+
/**
|
|
188
|
+
* Sets the hue of the color.
|
|
189
|
+
* @param value - The new hue value.
|
|
190
|
+
* @returns A new HSV color with the hue set.
|
|
191
|
+
*/
|
|
78
192
|
setHue(value: number): HSV {
|
|
79
|
-
return this.
|
|
193
|
+
return this.asHSV().setHue(value);
|
|
80
194
|
}
|
|
81
195
|
|
|
196
|
+
/**
|
|
197
|
+
* Fades the color by a given value.
|
|
198
|
+
* @param value - The fade value.
|
|
199
|
+
* @returns A new Color instance that is faded.
|
|
200
|
+
*/
|
|
82
201
|
abstract fade(value: number): Color;
|
|
83
202
|
}
|
package/src/color/hsl.ts
CHANGED
|
@@ -3,12 +3,38 @@ import { HSV } from './hsv.js';
|
|
|
3
3
|
import { RGB } from './rgb.js';
|
|
4
4
|
import { clamp, formatFloat, mod } from './utils.js';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Represents a color in the HSL (Hue, Saturation, Lightness) color space.
|
|
8
|
+
* Extends the base `Color` class.
|
|
9
|
+
*/
|
|
6
10
|
export class HSL extends Color {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
readonly
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The hue component of the color, in the range [0, 360].
|
|
13
|
+
*/
|
|
14
|
+
readonly h: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The saturation component of the color, in the range [0, 100].
|
|
18
|
+
*/
|
|
19
|
+
readonly s: number;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The lightness component of the color, in the range [0, 100].
|
|
23
|
+
*/
|
|
24
|
+
readonly l: number;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The alpha (opacity) component of the color, in the range [0, 1].
|
|
28
|
+
*/
|
|
29
|
+
readonly a: number;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new HSL color.
|
|
33
|
+
* @param h - The hue component, in the range [0, 360].
|
|
34
|
+
* @param s - The saturation component, in the range [0, 100].
|
|
35
|
+
* @param l - The lightness component, in the range [0, 100].
|
|
36
|
+
* @param a - The alpha (opacity) component, in the range [0, 1]. Defaults to 1.
|
|
37
|
+
*/
|
|
12
38
|
constructor(h: number, s: number, l: number, a: number = 1) {
|
|
13
39
|
super();
|
|
14
40
|
this.h = mod(h, 360);
|
|
@@ -17,10 +43,18 @@ export class HSL extends Color {
|
|
|
17
43
|
this.a = clamp(a, 0, 1);
|
|
18
44
|
}
|
|
19
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Returns the HSL color as an array of numbers.
|
|
48
|
+
* @returns An array containing the hue, saturation, lightness, and alpha components.
|
|
49
|
+
*/
|
|
20
50
|
asArray(): [number, number, number, number] {
|
|
21
51
|
return [this.h, this.s, this.l, this.a];
|
|
22
52
|
}
|
|
23
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Returns a new HSL color with rounded components.
|
|
56
|
+
* @returns A new HSL color with rounded hue, saturation, lightness, and alpha components.
|
|
57
|
+
*/
|
|
24
58
|
round(): HSL {
|
|
25
59
|
return new HSL(
|
|
26
60
|
Math.round(this.h),
|
|
@@ -30,10 +64,18 @@ export class HSL extends Color {
|
|
|
30
64
|
);
|
|
31
65
|
}
|
|
32
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Creates a copy of the current HSL color.
|
|
69
|
+
* @returns A new HSL color with the same components as the current color.
|
|
70
|
+
*/
|
|
33
71
|
clone(): HSL {
|
|
34
72
|
return new HSL(this.h, this.s, this.l, this.a);
|
|
35
73
|
}
|
|
36
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Returns the HSL color as a CSS-compatible string.
|
|
77
|
+
* @returns A string representing the HSL color in CSS format.
|
|
78
|
+
*/
|
|
37
79
|
asString(): string {
|
|
38
80
|
if (this.a === 1) {
|
|
39
81
|
return `hsl(${this.h.toFixed(0)},${this.s.toFixed(0)}%,${this.l.toFixed(0)}%)`;
|
|
@@ -42,14 +84,26 @@ export class HSL extends Color {
|
|
|
42
84
|
}
|
|
43
85
|
}
|
|
44
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Returns the current HSL color.
|
|
89
|
+
* @returns The current HSL color.
|
|
90
|
+
*/
|
|
45
91
|
asHSL(): HSL {
|
|
46
92
|
return this.clone();
|
|
47
93
|
}
|
|
48
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Returns the current HSL color.
|
|
97
|
+
* @returns The current HSL color.
|
|
98
|
+
*/
|
|
49
99
|
toHSL(): HSL {
|
|
50
100
|
return this;
|
|
51
101
|
}
|
|
52
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Converts the HSL color to an HSV color.
|
|
105
|
+
* @returns A new HSV color representing the same color.
|
|
106
|
+
*/
|
|
53
107
|
asHSV(): HSV {
|
|
54
108
|
const s = this.s / 100, l = this.l / 100;
|
|
55
109
|
const v = l + s * Math.min(l, 1 - l);
|
|
@@ -57,6 +111,10 @@ export class HSL extends Color {
|
|
|
57
111
|
return new HSV(this.h, sv * 100, v * 100, this.a);
|
|
58
112
|
}
|
|
59
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Converts the HSL color to an RGB color.
|
|
116
|
+
* @returns A new RGB color representing the same color.
|
|
117
|
+
*/
|
|
60
118
|
asRGB(): RGB {
|
|
61
119
|
const h = this.h / 360;
|
|
62
120
|
const s = this.s / 100;
|
|
@@ -86,6 +144,12 @@ export class HSL extends Color {
|
|
|
86
144
|
);
|
|
87
145
|
}
|
|
88
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Parses a string or Color object into an HSL color.
|
|
149
|
+
* @param input - The input string or Color object to parse.
|
|
150
|
+
* @returns A new HSL color parsed from the input.
|
|
151
|
+
* @throws Will throw an error if the input string is not a valid HSL color string.
|
|
152
|
+
*/
|
|
89
153
|
static parse(input: string | Color): HSL {
|
|
90
154
|
if (input instanceof Color) return input.asHSL();
|
|
91
155
|
|
|
@@ -104,18 +168,37 @@ export class HSL extends Color {
|
|
|
104
168
|
throw new Error(`Invalid HSL color string: "${input}"`);
|
|
105
169
|
}
|
|
106
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Inverts the lightness component of the HSL color.
|
|
173
|
+
* @returns A new HSL color with the lightness component inverted.
|
|
174
|
+
*/
|
|
107
175
|
invertLuminosity(): HSL {
|
|
108
176
|
return new HSL(this.h, this.s, 100 - this.l, this.a);
|
|
109
177
|
}
|
|
110
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Rotates the hue component of the HSL color by a given offset.
|
|
181
|
+
* @param offset - The amount to rotate the hue by, in degrees.
|
|
182
|
+
* @returns A new HSL color with the hue rotated by the given offset.
|
|
183
|
+
*/
|
|
111
184
|
rotateHue(offset: number): HSL {
|
|
112
185
|
return new HSL(mod(this.h + offset, 360), this.s, this.l, this.a);
|
|
113
186
|
}
|
|
114
187
|
|
|
188
|
+
/**
|
|
189
|
+
* Increases the saturation of the HSL color by a given ratio.
|
|
190
|
+
* @param ratio - The ratio by which to increase the saturation.
|
|
191
|
+
* @returns A new HSL color with increased saturation.
|
|
192
|
+
*/
|
|
115
193
|
saturate(ratio: number): HSL {
|
|
116
194
|
return new HSL(this.h, clamp(this.s * (1 + ratio), 0, 100), this.l, this.a);
|
|
117
195
|
}
|
|
118
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Decreases the alpha (opacity) of the HSL color by a given value.
|
|
199
|
+
* @param value - The value by which to decrease the alpha.
|
|
200
|
+
* @returns A new HSL color with decreased alpha.
|
|
201
|
+
*/
|
|
119
202
|
fade(value: number): HSL {
|
|
120
203
|
return new HSL(this.h, this.s, this.l, this.a * (1 - value));
|
|
121
204
|
}
|
package/src/color/hsv.ts
CHANGED
|
@@ -3,12 +3,38 @@ import { HSL } from './hsl.js';
|
|
|
3
3
|
import { RGB } from './rgb.js';
|
|
4
4
|
import { clamp, mod } from './utils.js';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Represents a color in the HSV (Hue, Saturation, Value) color space.
|
|
8
|
+
* Extends the base `Color` class.
|
|
9
|
+
*/
|
|
6
10
|
export class HSV extends Color {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
readonly
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The hue component of the color, in the range [0, 360].
|
|
13
|
+
*/
|
|
14
|
+
readonly h: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The saturation component of the color, in the range [0, 100].
|
|
18
|
+
*/
|
|
19
|
+
readonly s: number;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The value (brightness) component of the color, in the range [0, 100].
|
|
23
|
+
*/
|
|
24
|
+
readonly v: number;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The alpha (opacity) component of the color, in the range [0, 1].
|
|
28
|
+
*/
|
|
29
|
+
readonly a: number;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Constructs a new HSV color.
|
|
33
|
+
* @param h - The hue component, in the range [0, 360].
|
|
34
|
+
* @param s - The saturation component, in the range [0, 100].
|
|
35
|
+
* @param v - The value (brightness) component, in the range [0, 100].
|
|
36
|
+
* @param a - The alpha (opacity) component, in the range [0, 1]. Defaults to 1.
|
|
37
|
+
*/
|
|
12
38
|
constructor(h: number, s: number, v: number, a: number = 1) {
|
|
13
39
|
super();
|
|
14
40
|
this.h = mod(h, 360);
|
|
@@ -17,10 +43,18 @@ export class HSV extends Color {
|
|
|
17
43
|
this.a = clamp(a, 0, 1);
|
|
18
44
|
}
|
|
19
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Returns the HSV color as an array of numbers.
|
|
48
|
+
* @returns An array containing the hue, saturation, value, and alpha components.
|
|
49
|
+
*/
|
|
20
50
|
asArray(): [number, number, number, number] {
|
|
21
51
|
return [this.h, this.s, this.v, this.a];
|
|
22
52
|
}
|
|
23
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Returns a new HSV color with the components rounded to the nearest integer.
|
|
56
|
+
* @returns A new HSV color with rounded components.
|
|
57
|
+
*/
|
|
24
58
|
round(): HSV {
|
|
25
59
|
return new HSV(
|
|
26
60
|
Math.round(this.h),
|
|
@@ -30,14 +64,26 @@ export class HSV extends Color {
|
|
|
30
64
|
);
|
|
31
65
|
}
|
|
32
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Returns the color as a string representation.
|
|
69
|
+
* @returns A string representation of the color.
|
|
70
|
+
*/
|
|
33
71
|
asString(): string {
|
|
34
72
|
return this.asHSL().asString();
|
|
35
73
|
}
|
|
36
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Creates a new HSV color that is a copy of the current color.
|
|
77
|
+
* @returns A new HSV color that is a clone of the current color.
|
|
78
|
+
*/
|
|
37
79
|
clone(): HSV {
|
|
38
80
|
return new HSV(this.h, this.s, this.v, this.a);
|
|
39
81
|
}
|
|
40
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Converts the HSV color to an HSL color.
|
|
85
|
+
* @returns An HSL representation of the color.
|
|
86
|
+
*/
|
|
41
87
|
asHSL(): HSL {
|
|
42
88
|
const s = this.s / 100;
|
|
43
89
|
const v = this.v / 100;
|
|
@@ -51,14 +97,26 @@ export class HSV extends Color {
|
|
|
51
97
|
);
|
|
52
98
|
}
|
|
53
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Returns the current HSV color.
|
|
102
|
+
* @returns The current HSV color.
|
|
103
|
+
*/
|
|
54
104
|
asHSV(): HSV {
|
|
55
105
|
return this.clone();
|
|
56
106
|
}
|
|
57
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Returns the current HSV color.
|
|
110
|
+
* @returns The current HSV color.
|
|
111
|
+
*/
|
|
58
112
|
toHSV(): HSV {
|
|
59
113
|
return this;
|
|
60
114
|
}
|
|
61
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Converts the HSV color to an RGB color.
|
|
118
|
+
* @returns An RGB representation of the color.
|
|
119
|
+
*/
|
|
62
120
|
asRGB(): RGB {
|
|
63
121
|
const h = this.h / 360; // Normalize h to range [0, 1]
|
|
64
122
|
const s = this.s / 100; // Normalize s to range [0, 1]
|
|
@@ -90,10 +148,20 @@ export class HSV extends Color {
|
|
|
90
148
|
return new RGB(r * 255, g * 255, b * 255, this.a);
|
|
91
149
|
}
|
|
92
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Fades the color by a given value.
|
|
153
|
+
* @param value - The amount to fade the color by, in the range [0, 1].
|
|
154
|
+
* @returns A new HSV color with the alpha component faded by the given value.
|
|
155
|
+
*/
|
|
93
156
|
fade(value: number): HSV {
|
|
94
157
|
return new HSV(this.h, this.s, this.v, this.a * (1 - value));
|
|
95
158
|
}
|
|
96
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Sets the hue component of the color.
|
|
162
|
+
* @param value - The new hue value, in the range [0, 360].
|
|
163
|
+
* @returns A new HSV color with the updated hue component.
|
|
164
|
+
*/
|
|
97
165
|
setHue(value: number): HSV {
|
|
98
166
|
return new HSV(value, this.s, this.v, this.a);
|
|
99
167
|
}
|
package/src/color/index.test.ts
CHANGED
|
@@ -77,26 +77,6 @@ describe('Color.parse', () => {
|
|
|
77
77
|
});
|
|
78
78
|
});
|
|
79
79
|
|
|
80
|
-
describe('Color.random', () => {
|
|
81
|
-
test('generates random HSV colors', () => {
|
|
82
|
-
const random = Color.random();
|
|
83
|
-
expect(random).toBeInstanceOf(HSV);
|
|
84
|
-
const array = random.asArray();
|
|
85
|
-
expect(array[0]).toBeGreaterThanOrEqual(0);
|
|
86
|
-
expect(array[0]).toBeLessThanOrEqual(360);
|
|
87
|
-
expect(array[1]).toBeGreaterThanOrEqual(0);
|
|
88
|
-
expect(array[1]).toBeLessThanOrEqual(100);
|
|
89
|
-
expect(array[2]).toBeGreaterThanOrEqual(0);
|
|
90
|
-
expect(array[2]).toBeLessThanOrEqual(100);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
test('supports options for generating random colors', () => {
|
|
94
|
-
const random = Color.random({ hue: 'red', luminosity: 'bright' });
|
|
95
|
-
expect(random).toBeInstanceOf(HSV);
|
|
96
|
-
// Additional checks based on the options provided can be added here
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
80
|
describe('Color Class Properties', () => {
|
|
101
81
|
test('Color.HSL is accessible', () => {
|
|
102
82
|
expect(Color.HSL).toBe(HSL);
|
package/src/color/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Color } from './abstract.js';
|
|
2
2
|
import { HSL } from './hsl.js';
|
|
3
3
|
import { HSV } from './hsv.js';
|
|
4
|
-
import randomColor from './random.js';
|
|
5
4
|
import { RGB } from './rgb.js';
|
|
6
5
|
|
|
7
6
|
Color.parse = function (input: string | Color): Color {
|
|
@@ -29,8 +28,6 @@ Color.HSL = HSL;
|
|
|
29
28
|
Color.HSV = HSV;
|
|
30
29
|
Color.RGB = RGB;
|
|
31
30
|
|
|
32
|
-
Color.random = randomColor;
|
|
33
|
-
|
|
34
31
|
export type { RandomColorOptions } from './random.js';
|
|
35
32
|
export type { HSL } from './hsl.js';
|
|
36
33
|
export type { HSV } from './hsv.js';
|
package/src/color/random.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HSV } from './hsv.js';
|
|
1
2
|
import type { RandomColorOptions } from './random.js';
|
|
2
3
|
import randomColor from './random.js';
|
|
3
4
|
|
|
@@ -6,6 +7,26 @@ describe('RandomColor', () => {
|
|
|
6
7
|
expect(randomColor).toBeDefined();
|
|
7
8
|
});
|
|
8
9
|
|
|
10
|
+
describe('Color.random', () => {
|
|
11
|
+
test('generates random HSV colors', () => {
|
|
12
|
+
const random = randomColor();
|
|
13
|
+
expect(random).toBeInstanceOf(HSV);
|
|
14
|
+
const array = random.asArray();
|
|
15
|
+
expect(array[0]).toBeGreaterThanOrEqual(0);
|
|
16
|
+
expect(array[0]).toBeLessThanOrEqual(360);
|
|
17
|
+
expect(array[1]).toBeGreaterThanOrEqual(0);
|
|
18
|
+
expect(array[1]).toBeLessThanOrEqual(100);
|
|
19
|
+
expect(array[2]).toBeGreaterThanOrEqual(0);
|
|
20
|
+
expect(array[2]).toBeLessThanOrEqual(100);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('supports options for generating random colors', () => {
|
|
24
|
+
const random = randomColor({ hue: 'red', luminosity: 'bright' });
|
|
25
|
+
expect(random).toBeInstanceOf(HSV);
|
|
26
|
+
// Additional checks based on the options provided can be added here
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
9
30
|
describe('randomColor method', () => {
|
|
10
31
|
test('returns correct color string for some test cases', () => {
|
|
11
32
|
function t(options: RandomColorOptions): string {
|