@versatiles/style 5.9.0 → 5.9.1
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/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -3
- package/src/color/abstract.ts +0 -202
- package/src/color/hsl.test.ts +0 -176
- package/src/color/hsl.ts +0 -193
- package/src/color/hsv.test.ts +0 -169
- package/src/color/hsv.ts +0 -189
- package/src/color/index.test.ts +0 -187
- package/src/color/index.ts +0 -37
- package/src/color/random.test.ts +0 -111
- package/src/color/random.ts +0 -269
- package/src/color/rgb.test.ts +0 -218
- package/src/color/rgb.ts +0 -371
- package/src/color/utils.test.ts +0 -85
- package/src/color/utils.ts +0 -17
- package/src/guess_style/guess_style.test.ts +0 -140
- package/src/guess_style/guess_style.ts +0 -251
- package/src/guess_style/index.ts +0 -2
- package/src/index.test.ts +0 -131
- package/src/index.ts +0 -123
- package/src/lib/utils.test.ts +0 -281
- package/src/lib/utils.ts +0 -123
- package/src/shortbread/index.ts +0 -2
- package/src/shortbread/layers.test.ts +0 -81
- package/src/shortbread/layers.ts +0 -602
- package/src/shortbread/properties.test.ts +0 -44
- package/src/shortbread/properties.ts +0 -156
- package/src/shortbread/template.test.ts +0 -49
- package/src/shortbread/template.ts +0 -336
- package/src/style_builder/decorator.test.ts +0 -68
- package/src/style_builder/decorator.ts +0 -150
- package/src/style_builder/recolor.test.ts +0 -328
- package/src/style_builder/recolor.ts +0 -244
- package/src/style_builder/style_builder.test.ts +0 -148
- package/src/style_builder/style_builder.ts +0 -141
- package/src/style_builder/types.ts +0 -154
- package/src/styles/LICENSE.md +0 -41
- package/src/styles/colorful.test.ts +0 -91
- package/src/styles/colorful.ts +0 -1177
- package/src/styles/eclipse.ts +0 -11
- package/src/styles/empty.ts +0 -10
- package/src/styles/graybeard.ts +0 -11
- package/src/styles/index.ts +0 -34
- package/src/styles/neutrino.ts +0 -427
- package/src/styles/satellite.test.ts +0 -146
- package/src/styles/satellite.ts +0 -106
- package/src/styles/shadow.ts +0 -11
- package/src/types/index.ts +0 -5
- package/src/types/maplibre.ts +0 -25
- package/src/types/tilejson.test.ts +0 -96
- package/src/types/tilejson.ts +0 -147
- package/src/types/vector_layer.test.ts +0 -68
- package/src/types/vector_layer.ts +0 -67
package/src/color/random.ts
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
import { HSV } from './hsv.js';
|
|
2
|
-
import { mod } from './utils.js';
|
|
3
|
-
|
|
4
|
-
type Range = [number, number];
|
|
5
|
-
interface ColorInfo {
|
|
6
|
-
hueRange: Range | null;
|
|
7
|
-
lowerBounds: Range[];
|
|
8
|
-
saturationRange: Range;
|
|
9
|
-
brightnessRange: Range;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface RandomColorOptions {
|
|
13
|
-
seed?: string;
|
|
14
|
-
hue?: number | string;
|
|
15
|
-
opacity?: number;
|
|
16
|
-
luminosity?: number | string;
|
|
17
|
-
saturation?: number | string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let colorDictionary = new Map<string, ColorInfo>();
|
|
21
|
-
|
|
22
|
-
export default function randomColor(options?: RandomColorOptions): HSV {
|
|
23
|
-
if (colorDictionary.size === 0) colorDictionary = initColorDictionary();
|
|
24
|
-
|
|
25
|
-
options ??= {};
|
|
26
|
-
|
|
27
|
-
let seed = inputToSeed(options.seed);
|
|
28
|
-
|
|
29
|
-
const H = pickHue(options);
|
|
30
|
-
const S = pickSaturation(H, options);
|
|
31
|
-
const V = pickBrightness(H, S, options);
|
|
32
|
-
|
|
33
|
-
return new HSV(H, S, V, options.opacity ?? 1);
|
|
34
|
-
|
|
35
|
-
function pickHue(options: RandomColorOptions): number {
|
|
36
|
-
return mod(randomWithin(getHueRange(options.hue)), 360);
|
|
37
|
-
|
|
38
|
-
function getHueRange(hue?: number | string): Range {
|
|
39
|
-
if (typeof hue === 'number') {
|
|
40
|
-
hue = mod(hue, 360);
|
|
41
|
-
return [hue, hue];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (typeof hue === 'string') {
|
|
45
|
-
const color = colorDictionary.get(hue);
|
|
46
|
-
if (color?.hueRange) return color.hueRange;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return [0, 360];
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function pickSaturation(hue: number, options: RandomColorOptions): number {
|
|
54
|
-
if (options.hue === 'monochrome') return 0;
|
|
55
|
-
if (options.luminosity === 'random') return randomWithin([0, 100]);
|
|
56
|
-
|
|
57
|
-
let [sMin, sMax] = getColorInfo(hue).saturationRange;
|
|
58
|
-
|
|
59
|
-
if (options.saturation === 'strong') return sMax;
|
|
60
|
-
|
|
61
|
-
switch (options.luminosity) {
|
|
62
|
-
case 'bright':
|
|
63
|
-
sMin = 55;
|
|
64
|
-
break;
|
|
65
|
-
case 'dark':
|
|
66
|
-
sMin = sMax - 10;
|
|
67
|
-
break;
|
|
68
|
-
case 'light':
|
|
69
|
-
sMax = 55;
|
|
70
|
-
break;
|
|
71
|
-
default:
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return randomWithin([sMin, sMax]);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function pickBrightness(h: number, s: number, options: RandomColorOptions): number {
|
|
78
|
-
let bMin = getMinimumBrightness(h, s),
|
|
79
|
-
bMax = 100;
|
|
80
|
-
|
|
81
|
-
if (typeof options.luminosity === 'number') {
|
|
82
|
-
bMin = options.luminosity;
|
|
83
|
-
bMax = options.luminosity;
|
|
84
|
-
} else {
|
|
85
|
-
switch (options.luminosity) {
|
|
86
|
-
case 'dark':
|
|
87
|
-
bMax = Math.min(100, bMin + 20);
|
|
88
|
-
break;
|
|
89
|
-
case 'light':
|
|
90
|
-
bMin = (bMax + bMin) / 2;
|
|
91
|
-
break;
|
|
92
|
-
case 'random':
|
|
93
|
-
bMin = 0;
|
|
94
|
-
bMax = 100;
|
|
95
|
-
break;
|
|
96
|
-
default:
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return randomWithin([bMin, bMax]);
|
|
101
|
-
|
|
102
|
-
function getMinimumBrightness(h: number, s: number): number {
|
|
103
|
-
const { lowerBounds } = getColorInfo(h);
|
|
104
|
-
|
|
105
|
-
for (let i = 0; i < lowerBounds.length - 1; i++) {
|
|
106
|
-
const [s1, v1] = lowerBounds[i];
|
|
107
|
-
const [s2, v2] = lowerBounds[i + 1];
|
|
108
|
-
if (s >= s1 && s <= s2) {
|
|
109
|
-
const m = (v2 - v1) / (s2 - s1),
|
|
110
|
-
b = v1 - m * s1;
|
|
111
|
-
return m * s + b;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return 0;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function randomWithin(range: Range): number {
|
|
120
|
-
//Seeded random algorithm from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
|
|
121
|
-
seed = (seed * 9301 + 49297) % 233280;
|
|
122
|
-
return Math.floor(range[0] + (seed / 233280.0) * (range[1] - range[0]));
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function inputToSeed(input: number | string | null | undefined): number {
|
|
127
|
-
if (input == null) return 0;
|
|
128
|
-
if (typeof input === 'number') return input;
|
|
129
|
-
|
|
130
|
-
let i = 0;
|
|
131
|
-
for (let p = 0; p < input.length; p++) i = (i * 0x101 + input.charCodeAt(p)) % 0x100000000;
|
|
132
|
-
return i;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function initColorDictionary(): Map<string, ColorInfo> {
|
|
136
|
-
const dict = new Map<string, ColorInfo>();
|
|
137
|
-
|
|
138
|
-
const defineColor = (name: string, hueRange: [number, number] | null, lowerBounds: [number, number][]): void => {
|
|
139
|
-
const [greyest] = lowerBounds;
|
|
140
|
-
const colorful = lowerBounds[lowerBounds.length - 1];
|
|
141
|
-
|
|
142
|
-
dict.set(name, {
|
|
143
|
-
hueRange,
|
|
144
|
-
lowerBounds,
|
|
145
|
-
saturationRange: [greyest[0], colorful[0]],
|
|
146
|
-
brightnessRange: [colorful[1], greyest[1]],
|
|
147
|
-
});
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
defineColor('monochrome', null, [
|
|
151
|
-
[0, 0],
|
|
152
|
-
[100, 0],
|
|
153
|
-
]);
|
|
154
|
-
defineColor(
|
|
155
|
-
'red',
|
|
156
|
-
[-26, 18],
|
|
157
|
-
[
|
|
158
|
-
[20, 100],
|
|
159
|
-
[30, 92],
|
|
160
|
-
[40, 89],
|
|
161
|
-
[50, 85],
|
|
162
|
-
[60, 78],
|
|
163
|
-
[70, 70],
|
|
164
|
-
[80, 60],
|
|
165
|
-
[90, 55],
|
|
166
|
-
[100, 50],
|
|
167
|
-
]
|
|
168
|
-
);
|
|
169
|
-
defineColor(
|
|
170
|
-
'orange',
|
|
171
|
-
[18, 46],
|
|
172
|
-
[
|
|
173
|
-
[20, 100],
|
|
174
|
-
[30, 93],
|
|
175
|
-
[40, 88],
|
|
176
|
-
[50, 86],
|
|
177
|
-
[60, 85],
|
|
178
|
-
[70, 70],
|
|
179
|
-
[100, 70],
|
|
180
|
-
]
|
|
181
|
-
);
|
|
182
|
-
defineColor(
|
|
183
|
-
'yellow',
|
|
184
|
-
[46, 62],
|
|
185
|
-
[
|
|
186
|
-
[25, 100],
|
|
187
|
-
[40, 94],
|
|
188
|
-
[50, 89],
|
|
189
|
-
[60, 86],
|
|
190
|
-
[70, 84],
|
|
191
|
-
[80, 82],
|
|
192
|
-
[90, 80],
|
|
193
|
-
[100, 75],
|
|
194
|
-
]
|
|
195
|
-
);
|
|
196
|
-
defineColor(
|
|
197
|
-
'green',
|
|
198
|
-
[62, 178],
|
|
199
|
-
[
|
|
200
|
-
[30, 100],
|
|
201
|
-
[40, 90],
|
|
202
|
-
[50, 85],
|
|
203
|
-
[60, 81],
|
|
204
|
-
[70, 74],
|
|
205
|
-
[80, 64],
|
|
206
|
-
[90, 50],
|
|
207
|
-
[100, 40],
|
|
208
|
-
]
|
|
209
|
-
);
|
|
210
|
-
defineColor(
|
|
211
|
-
'blue',
|
|
212
|
-
[178, 257],
|
|
213
|
-
[
|
|
214
|
-
[20, 100],
|
|
215
|
-
[30, 86],
|
|
216
|
-
[40, 80],
|
|
217
|
-
[50, 74],
|
|
218
|
-
[60, 60],
|
|
219
|
-
[70, 52],
|
|
220
|
-
[80, 44],
|
|
221
|
-
[90, 39],
|
|
222
|
-
[100, 35],
|
|
223
|
-
]
|
|
224
|
-
);
|
|
225
|
-
defineColor(
|
|
226
|
-
'purple',
|
|
227
|
-
[257, 282],
|
|
228
|
-
[
|
|
229
|
-
[20, 100],
|
|
230
|
-
[30, 87],
|
|
231
|
-
[40, 79],
|
|
232
|
-
[50, 70],
|
|
233
|
-
[60, 65],
|
|
234
|
-
[70, 59],
|
|
235
|
-
[80, 52],
|
|
236
|
-
[90, 45],
|
|
237
|
-
[100, 42],
|
|
238
|
-
]
|
|
239
|
-
);
|
|
240
|
-
defineColor(
|
|
241
|
-
'pink',
|
|
242
|
-
[282, 334],
|
|
243
|
-
[
|
|
244
|
-
[20, 100],
|
|
245
|
-
[30, 90],
|
|
246
|
-
[40, 86],
|
|
247
|
-
[60, 84],
|
|
248
|
-
[80, 80],
|
|
249
|
-
[90, 75],
|
|
250
|
-
[100, 73],
|
|
251
|
-
]
|
|
252
|
-
);
|
|
253
|
-
|
|
254
|
-
return dict;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function getColorInfo(hue: number): ColorInfo {
|
|
258
|
-
hue = mod(hue, 360);
|
|
259
|
-
if (hue >= 334) hue -= 360;
|
|
260
|
-
|
|
261
|
-
for (const color of colorDictionary.values()) {
|
|
262
|
-
if (color.hueRange && hue >= color.hueRange[0] && hue <= color.hueRange[1]) {
|
|
263
|
-
return color;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
throw new Error(
|
|
267
|
-
`getColorInfo: No color info found for hue value ${hue}. This indicates a gap in the color dictionary hue ranges.`
|
|
268
|
-
);
|
|
269
|
-
}
|
package/src/color/rgb.test.ts
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { RGB } from './rgb.js';
|
|
3
|
-
import { HSL } from './hsl.js';
|
|
4
|
-
import { HSV } from './hsv.js';
|
|
5
|
-
|
|
6
|
-
describe('RGB Class', () => {
|
|
7
|
-
it('constructor initializes values correctly with clamping', () => {
|
|
8
|
-
const color = new RGB(300, -50, 500, 2);
|
|
9
|
-
expect(color.asArray()).toStrictEqual([255, 0, 255, 1]);
|
|
10
|
-
|
|
11
|
-
const colorNegative = new RGB(-10, -20, -30, -1);
|
|
12
|
-
expect(colorNegative.asArray()).toStrictEqual([0, 0, 0, 0]);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it('asArray returns the correct array representation', () => {
|
|
16
|
-
const color = new RGB(128, 64, 255, 0.5);
|
|
17
|
-
expect(color.asArray()).toStrictEqual([128, 64, 255, 0.5]);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('clone creates a new instance with identical values', () => {
|
|
21
|
-
const color = new RGB(128, 255, 64, 0.8);
|
|
22
|
-
const clone = color.clone();
|
|
23
|
-
expect(clone).toBeInstanceOf(RGB);
|
|
24
|
-
expect(clone).toEqual(color);
|
|
25
|
-
expect(clone).not.toBe(color);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('asString returns correct RGB/RGBA string', () => {
|
|
29
|
-
const color1 = new RGB(255, 128, 64);
|
|
30
|
-
expect(color1.asString()).toBe('rgb(255,128,64)');
|
|
31
|
-
|
|
32
|
-
const color2 = new RGB(255, 128, 64, 0.5);
|
|
33
|
-
expect(color2.asString()).toBe('rgba(255,128,64,0.5)');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('asHex returns correct hexadecimal representation', () => {
|
|
37
|
-
const color1 = new RGB(255, 128, 64);
|
|
38
|
-
expect(color1.asHex()).toBe('#FF8040');
|
|
39
|
-
|
|
40
|
-
const color2 = new RGB(255, 128, 64, 0.5);
|
|
41
|
-
expect(color2.asHex()).toBe('#FF804080');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
describe('color conversion', () => {
|
|
45
|
-
it('asHSL converts RGB to HSL correctly', () => {
|
|
46
|
-
const hsl = new RGB(255, 0, 0).asHSL();
|
|
47
|
-
expect(hsl).toBeInstanceOf(HSL);
|
|
48
|
-
expect(hsl.asArray().map((value) => Math.round(value))).toStrictEqual([0, 100, 50, 1]);
|
|
49
|
-
|
|
50
|
-
expect(RGB.parse('#000000').asHSL().round().asArray()).toStrictEqual([0, 0, 0, 1]);
|
|
51
|
-
expect(RGB.parse('#FFFFFF').asHSL().round().asArray()).toStrictEqual([0, 0, 100, 1]);
|
|
52
|
-
expect(RGB.parse('#FF0000').asHSL().round().asArray()).toStrictEqual([0, 100, 50, 1]);
|
|
53
|
-
expect(RGB.parse('#FF8000').asHSL().round().asArray()).toStrictEqual([30, 100, 50, 1]);
|
|
54
|
-
expect(RGB.parse('#FFFF00').asHSL().round().asArray()).toStrictEqual([60, 100, 50, 1]);
|
|
55
|
-
expect(RGB.parse('#80FF00').asHSL().round().asArray()).toStrictEqual([90, 100, 50, 1]);
|
|
56
|
-
expect(RGB.parse('#00FF00').asHSL().round().asArray()).toStrictEqual([120, 100, 50, 1]);
|
|
57
|
-
expect(RGB.parse('#00FF80').asHSL().round().asArray()).toStrictEqual([150, 100, 50, 1]);
|
|
58
|
-
expect(RGB.parse('#00FFFF').asHSL().round().asArray()).toStrictEqual([180, 100, 50, 1]);
|
|
59
|
-
expect(RGB.parse('#0080FF').asHSL().round().asArray()).toStrictEqual([210, 100, 50, 1]);
|
|
60
|
-
expect(RGB.parse('#0000FF').asHSL().round().asArray()).toStrictEqual([240, 100, 50, 1]);
|
|
61
|
-
expect(RGB.parse('#8000FF').asHSL().round().asArray()).toStrictEqual([270, 100, 50, 1]);
|
|
62
|
-
expect(RGB.parse('#FF00FF').asHSL().round().asArray()).toStrictEqual([300, 100, 50, 1]);
|
|
63
|
-
expect(RGB.parse('#FF0080').asHSL().round().asArray()).toStrictEqual([330, 100, 50, 1]);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('asHSV converts RGB to HSV correctly', () => {
|
|
67
|
-
const hsv = new RGB(255, 0, 0).asHSV();
|
|
68
|
-
expect(hsv).toBeInstanceOf(HSV);
|
|
69
|
-
expect(hsv.asArray().map((value) => Math.round(value))).toStrictEqual([0, 100, 100, 1]);
|
|
70
|
-
|
|
71
|
-
expect(RGB.parse('#000000').asHSV().round().asArray()).toStrictEqual([0, 0, 0, 1]);
|
|
72
|
-
expect(RGB.parse('#FFFFFF').asHSV().round().asArray()).toStrictEqual([0, 0, 100, 1]);
|
|
73
|
-
expect(RGB.parse('#FF0000').asHSV().round().asArray()).toStrictEqual([0, 100, 100, 1]);
|
|
74
|
-
expect(RGB.parse('#FF8000').asHSV().round().asArray()).toStrictEqual([30, 100, 100, 1]);
|
|
75
|
-
expect(RGB.parse('#FFFF00').asHSV().round().asArray()).toStrictEqual([60, 100, 100, 1]);
|
|
76
|
-
expect(RGB.parse('#80FF00').asHSV().round().asArray()).toStrictEqual([90, 100, 100, 1]);
|
|
77
|
-
expect(RGB.parse('#00FF00').asHSV().round().asArray()).toStrictEqual([120, 100, 100, 1]);
|
|
78
|
-
expect(RGB.parse('#00FF80').asHSV().round().asArray()).toStrictEqual([150, 100, 100, 1]);
|
|
79
|
-
expect(RGB.parse('#00FFFF').asHSV().round().asArray()).toStrictEqual([180, 100, 100, 1]);
|
|
80
|
-
expect(RGB.parse('#0080FF').asHSV().round().asArray()).toStrictEqual([210, 100, 100, 1]);
|
|
81
|
-
expect(RGB.parse('#0000FF').asHSV().round().asArray()).toStrictEqual([240, 100, 100, 1]);
|
|
82
|
-
expect(RGB.parse('#8000FF').asHSV().round().asArray()).toStrictEqual([270, 100, 100, 1]);
|
|
83
|
-
expect(RGB.parse('#FF00FF').asHSV().round().asArray()).toStrictEqual([300, 100, 100, 1]);
|
|
84
|
-
expect(RGB.parse('#FF0080').asHSV().round().asArray()).toStrictEqual([330, 100, 100, 1]);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('asRGB returns a clone', () => {
|
|
88
|
-
const color = new RGB(255, 128, 64, 0.5);
|
|
89
|
-
expect(color.asRGB()).toStrictEqual(color);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('handles black correctly', () => {
|
|
93
|
-
const color = new RGB(0, 0, 0);
|
|
94
|
-
expect(
|
|
95
|
-
color
|
|
96
|
-
.asHSL()
|
|
97
|
-
.asArray()
|
|
98
|
-
.map((value) => Math.round(value))
|
|
99
|
-
).toStrictEqual([0, 0, 0, 1]);
|
|
100
|
-
expect(
|
|
101
|
-
color
|
|
102
|
-
.asHSV()
|
|
103
|
-
.asArray()
|
|
104
|
-
.map((value) => Math.round(value))
|
|
105
|
-
).toStrictEqual([0, 0, 0, 1]);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('handles white correctly', () => {
|
|
109
|
-
const color = new RGB(255, 255, 255);
|
|
110
|
-
expect(
|
|
111
|
-
color
|
|
112
|
-
.asHSL()
|
|
113
|
-
.asArray()
|
|
114
|
-
.map((value) => Math.round(value))
|
|
115
|
-
).toStrictEqual([0, 0, 100, 1]);
|
|
116
|
-
expect(
|
|
117
|
-
color
|
|
118
|
-
.asHSV()
|
|
119
|
-
.asArray()
|
|
120
|
-
.map((value) => Math.round(value))
|
|
121
|
-
).toStrictEqual([0, 0, 100, 1]);
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
describe('parse', () => {
|
|
126
|
-
it('parses hexadecimal color strings correctly', () => {
|
|
127
|
-
expect(RGB.parse('#ff8040').asArray()).toStrictEqual([255, 128, 64, 1]);
|
|
128
|
-
expect(RGB.parse('#ff804066').asArray()).toStrictEqual([255, 128, 64, 0.4]);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('parses shorthand hexadecimal strings correctly', () => {
|
|
132
|
-
expect(RGB.parse('#fa4').asArray()).toStrictEqual([255, 170, 68, 1]);
|
|
133
|
-
expect(RGB.parse('#fa43').asArray()).toStrictEqual([255, 170, 68, 0.2]);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('parses RGB strings correctly', () => {
|
|
137
|
-
expect(RGB.parse('rgb(255, 128, 64)').asArray()).toStrictEqual([255, 128, 64, 1]);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('parses RGBA strings correctly', () => {
|
|
141
|
-
expect(RGB.parse('rgba(255, 128, 64, 0.5)').asArray()).toStrictEqual([255, 128, 64, 0.5]);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('throws an error for invalid strings', () => {
|
|
145
|
-
expect(() => RGB.parse('invalid')).toThrow('Invalid RGB color string: "invalid"');
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
describe('filter methods', () => {
|
|
150
|
-
function pc(cb: (c: RGB) => RGB): [number, number, number, number] {
|
|
151
|
-
return cb(new RGB(50, 150, 200, 0.8))
|
|
152
|
-
.round()
|
|
153
|
-
.asArray();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
it('adjusts gamma correctly', () => {
|
|
157
|
-
expect(pc((c) => c.gamma(2.2))).toStrictEqual([7, 79, 149, 0.8]);
|
|
158
|
-
expect(pc((c) => c.gamma(0.001))).toStrictEqual([255, 255, 255, 0.8]);
|
|
159
|
-
expect(pc((c) => c.gamma(1000))).toStrictEqual([0, 0, 0, 0.8]);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('inverts RGB values correctly', () => {
|
|
163
|
-
expect(pc((c) => c.invert())).toStrictEqual([205, 105, 55, 0.8]);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('adjusts contrast correctly', () => {
|
|
167
|
-
expect(pc((c) => c.contrast(1.5))).toStrictEqual([11, 161, 236, 0.8]);
|
|
168
|
-
expect(pc((c) => c.contrast(1e6))).toStrictEqual([0, 255, 255, 0.8]);
|
|
169
|
-
expect(pc((c) => c.contrast(0))).toStrictEqual([128, 128, 128, 0.8]);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('increases brightness correctly', () => {
|
|
173
|
-
expect(pc((c) => c.brightness(0.5))).toStrictEqual([153, 203, 228, 0.8]);
|
|
174
|
-
expect(pc((c) => c.brightness(-0.5))).toStrictEqual([25, 75, 100, 0.8]);
|
|
175
|
-
expect(pc((c) => c.brightness(2))).toStrictEqual([255, 255, 255, 0.8]);
|
|
176
|
-
expect(pc((c) => c.brightness(-2))).toStrictEqual([0, 0, 0, 0.8]);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it('tints color correctly', () => {
|
|
180
|
-
const tintColor = new RGB(255, 0, 0);
|
|
181
|
-
expect(pc((c) => c.tint(0.5, tintColor))).toStrictEqual([125, 100, 125, 0.8]);
|
|
182
|
-
expect(pc((c) => c.tint(1, tintColor))).toStrictEqual([200, 50, 50, 0.8]);
|
|
183
|
-
expect(pc((c) => c.tint(0, tintColor))).toStrictEqual([50, 150, 200, 0.8]);
|
|
184
|
-
expect(pc((c) => c.tint(-1, tintColor))).toStrictEqual([50, 150, 200, 0.8]);
|
|
185
|
-
expect(pc((c) => c.tint(2, tintColor))).toStrictEqual([200, 50, 50, 0.8]);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it('blends color correctly', () => {
|
|
189
|
-
const blendColor = new RGB(255, 0, 0);
|
|
190
|
-
expect(pc((c) => c.blend(0.2, blendColor))).toStrictEqual([91, 120, 160, 0.8]);
|
|
191
|
-
expect(pc((c) => c.blend(0.5, blendColor))).toStrictEqual([153, 75, 100, 0.8]);
|
|
192
|
-
expect(pc((c) => c.blend(0.8, blendColor))).toStrictEqual([214, 30, 40, 0.8]);
|
|
193
|
-
expect(pc((c) => c.blend(1, blendColor))).toStrictEqual([255, 0, 0, 0.8]);
|
|
194
|
-
expect(pc((c) => c.blend(0, blendColor))).toStrictEqual([50, 150, 200, 0.8]);
|
|
195
|
-
expect(pc((c) => c.blend(-1, blendColor))).toStrictEqual([50, 150, 200, 0.8]);
|
|
196
|
-
expect(pc((c) => c.blend(2, blendColor))).toStrictEqual([255, 0, 0, 0.8]);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it('lightens the color correctly', () => {
|
|
200
|
-
expect(pc((c) => c.lighten(0.5))).toStrictEqual([153, 203, 228, 0.8]);
|
|
201
|
-
expect(pc((c) => c.lighten(2))).toStrictEqual([255, 255, 255, 0.8]);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('darkens the color correctly', () => {
|
|
205
|
-
expect(pc((c) => c.darken(0.5))).toStrictEqual([25, 75, 100, 0.8]);
|
|
206
|
-
expect(pc((c) => c.darken(1))).toStrictEqual([0, 0, 0, 0.8]);
|
|
207
|
-
expect(pc((c) => c.darken(2))).toStrictEqual([0, 0, 0, 0.8]);
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
it('fades color correctly', () => {
|
|
211
|
-
expect(pc((c) => c.fade(0.5))).toStrictEqual([50, 150, 200, 0.4]);
|
|
212
|
-
expect(pc((c) => c.fade(1))).toStrictEqual([50, 150, 200, 0]);
|
|
213
|
-
|
|
214
|
-
const fullyOpaque = new RGB(50, 150, 200, 1);
|
|
215
|
-
expect(fullyOpaque.fade(0).asArray()).toStrictEqual([50, 150, 200, 1]);
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
});
|