nhb-toolbox 4.0.36 → 4.0.40
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/cjs/colors/Color.js +189 -20
- package/dist/cjs/colors/helpers.js +64 -27
- package/dist/dts/colors/Color.d.ts +93 -18
- package/dist/dts/colors/Color.d.ts.map +1 -1
- package/dist/dts/colors/helpers.d.ts +26 -22
- package/dist/dts/colors/helpers.d.ts.map +1 -1
- package/dist/dts/colors/initials.d.ts +3 -3
- package/dist/dts/colors/initials.d.ts.map +1 -1
- package/dist/dts/colors/types.d.ts +17 -2
- package/dist/dts/colors/types.d.ts.map +1 -1
- package/dist/dts/date/constants.d.ts +1 -1
- package/dist/esm/colors/Color.js +190 -21
- package/dist/esm/colors/helpers.js +59 -25
- package/package.json +1 -1
package/dist/cjs/colors/Color.js
CHANGED
|
@@ -8,7 +8,7 @@ const hsl = (0, random_1.generateRandomHSLColor)();
|
|
|
8
8
|
const { hex, rgb } = (0, convert_1.convertColorCode)(hsl);
|
|
9
9
|
/**
|
|
10
10
|
* * Class representing a color and its conversions among `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` and `HSLA` formats.
|
|
11
|
-
* * It has
|
|
11
|
+
* * It has 13 instance methods to manipulate and play with the color values.
|
|
12
12
|
* * It has 6 static methods that can be used to check if a color is in `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` format.
|
|
13
13
|
*
|
|
14
14
|
* @property hex - The color in `Hex` format.
|
|
@@ -41,7 +41,7 @@ class Color {
|
|
|
41
41
|
* If no color is passed, a random color will be generated.
|
|
42
42
|
*
|
|
43
43
|
* Additionally:
|
|
44
|
-
* -
|
|
44
|
+
* - It has 13 instance methods to manipulate and play with the color values.
|
|
45
45
|
* - Use static methods like `Color.isHex6(color)` to validate color strings.
|
|
46
46
|
*
|
|
47
47
|
* @param toConvert - An optional input color string in any supported format (`Hex`, `Hex8`, `RGB`, `RGBA`, `HSL`, or `HSLA`) to convert in all other (includes the current format) formats.
|
|
@@ -118,12 +118,12 @@ class Color {
|
|
|
118
118
|
yield this.hsla;
|
|
119
119
|
}
|
|
120
120
|
/**
|
|
121
|
-
*
|
|
122
|
-
* - For solid colors (Hex6/RGB/HSL): Adds an alpha channel with the specified opacity
|
|
123
|
-
* - For alpha colors (Hex8/RGBA/HSLA): Updates the existing alpha channel
|
|
121
|
+
* @instance Applies or modifies the opacity of a color. Mutate the original instance.
|
|
122
|
+
* - For solid colors (Hex6/RGB/HSL): Adds an alpha channel with the specified opacity.
|
|
123
|
+
* - For alpha colors (Hex8/RGBA/HSLA): Updates the existing alpha channel.
|
|
124
124
|
*
|
|
125
|
-
* @param opacity - A number between 0-100 representing the opacity percentage
|
|
126
|
-
* @returns
|
|
125
|
+
* @param opacity - A number between 0-100 representing the opacity percentage.
|
|
126
|
+
* @returns A new instance of `Color` containing all color formats with the applied opacity.
|
|
127
127
|
*
|
|
128
128
|
* @example
|
|
129
129
|
* const color = new Color("#ff0000");
|
|
@@ -141,14 +141,168 @@ class Color {
|
|
|
141
141
|
const alphaDecimal = validOpacity / 100;
|
|
142
142
|
const rgbValues = (0, helpers_1._extractSolidColorValues)(this.rgb);
|
|
143
143
|
const hslValues = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
144
|
-
return {
|
|
144
|
+
return Color.#fromParts({
|
|
145
145
|
hex: this.hex.slice(0, 7).toUpperCase(),
|
|
146
146
|
hex8: `${this.hex.slice(0, 7)}${alphaHex}`.toUpperCase(),
|
|
147
147
|
rgb: `rgb(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]})`,
|
|
148
148
|
rgba: `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, ${alphaDecimal})`,
|
|
149
149
|
hsl: `hsl(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%)`,
|
|
150
150
|
hsla: `hsla(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%, ${alphaDecimal})`,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* @instance Darkens the color by reducing the lightness by the given percentage.
|
|
155
|
+
* @param percent - The percentage to darken (0–100).
|
|
156
|
+
* @returns A new `Color` instance with the modified darkness.
|
|
157
|
+
*/
|
|
158
|
+
applyDarkness(percent) {
|
|
159
|
+
const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
160
|
+
const newL = Math.max(0, l - percent);
|
|
161
|
+
const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
|
|
162
|
+
return new Color(newHSL);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @instance Lightens the color by increasing the lightness by the given percentage.
|
|
166
|
+
* @param percent - The percentage to brighten (0–100).
|
|
167
|
+
* @returns A new `Color` instance with the modified lightness.
|
|
168
|
+
*/
|
|
169
|
+
applyBrightness(percent) {
|
|
170
|
+
const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
171
|
+
const newL = Math.min(100, l + percent);
|
|
172
|
+
const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
|
|
173
|
+
return new Color(newHSL);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @instance Reduces the saturation of the color to make it appear duller.
|
|
177
|
+
* @param percent - The percentage to reduce saturation (0–100).
|
|
178
|
+
* @returns A new `Color` instance with the modified saturation.
|
|
179
|
+
*/
|
|
180
|
+
applyDullness(percent) {
|
|
181
|
+
const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
182
|
+
const newS = Math.max(0, s - percent);
|
|
183
|
+
const newHSL = `hsl(${h}, ${newS}%, ${l}%)`;
|
|
184
|
+
return new Color(newHSL);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* @instance Softens the color toward white by reducing saturation and increasing lightness based on a percentage.
|
|
188
|
+
* - *This creates a soft UI-like white shade effect (similar to some UI libraries' light color scale).*
|
|
189
|
+
* @param percent - Value from 0 to 100 representing how far to push the color toward white.
|
|
190
|
+
* @returns A new `Color` instance shifted toward white.
|
|
191
|
+
*/
|
|
192
|
+
applyWhiteShade(percent) {
|
|
193
|
+
const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
194
|
+
// Cap values to avoid overshooting
|
|
195
|
+
const newS = Math.max(0, s - (s * percent) / 100);
|
|
196
|
+
const newL = Math.min(100, l + ((100 - l) * percent) / 100);
|
|
197
|
+
const newHSL = `hsl(${h}, ${newS}%, ${newL}%)`;
|
|
198
|
+
return new Color(newHSL);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @instance Blends the current color with another color based on the given weight.
|
|
202
|
+
* @param other - The color in any 6 `(Hex, Hex8 RGB, RGBA, HSL or HSLA)` to blend with.
|
|
203
|
+
* @param weight - A number from 0 to 1 indicating the weight of the other color. Defaults to `0.5`.
|
|
204
|
+
* - `weight = 0` → only the original color (this)
|
|
205
|
+
* - `weight = 1` → only the other color
|
|
206
|
+
* - `weight = 0.5` → equal blend between the two
|
|
207
|
+
* @returns A new `Color` instance representing the blended result.
|
|
208
|
+
*/
|
|
209
|
+
blendWith(other, weight = 0.5) {
|
|
210
|
+
const w = Math.max(0, Math.min(1, weight));
|
|
211
|
+
const converted = new Color(other);
|
|
212
|
+
const rgb1 = (0, helpers_1._extractSolidColorValues)(this.rgb);
|
|
213
|
+
const rgb2 = (0, helpers_1._extractSolidColorValues)(converted.rgb);
|
|
214
|
+
const blended = rgb1.map((c, i) => Math.round(c * (1 - w) + rgb2[i] * w));
|
|
215
|
+
const blendedRGB = `rgb(${blended[0]}, ${blended[1]}, ${blended[2]})`;
|
|
216
|
+
return new Color(blendedRGB);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @instance Calculates the contrast ratio between this color and another color (WCAG).
|
|
220
|
+
* @param other - The other color to compare against.
|
|
221
|
+
* @returns A number representing the contrast ratio (rounded to 2 decimal places).
|
|
222
|
+
*/
|
|
223
|
+
contrastRatio(other) {
|
|
224
|
+
const newColor = new Color(other);
|
|
225
|
+
const luminance = (rgb) => {
|
|
226
|
+
const [r, g, b] = (0, helpers_1._extractSolidColorValues)(rgb).map((v) => {
|
|
227
|
+
const c = v / 255;
|
|
228
|
+
return c <= 0.03928 ?
|
|
229
|
+
c / 12.92
|
|
230
|
+
: Math.pow((c + 0.055) / 1.055, 2.4);
|
|
231
|
+
});
|
|
232
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
151
233
|
};
|
|
234
|
+
const lum1 = luminance(this.rgb);
|
|
235
|
+
const lum2 = luminance(newColor.rgb);
|
|
236
|
+
const brighter = Math.max(lum1, lum2);
|
|
237
|
+
const darker = Math.min(lum1, lum2);
|
|
238
|
+
const ratio = (brighter + 0.05) / (darker + 0.05);
|
|
239
|
+
return Math.round(ratio * 100) / 100;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* @instance Returns the complementary color by rotating the hue 180 degrees.
|
|
243
|
+
* @returns A new Color that is the complement of the current color.
|
|
244
|
+
*/
|
|
245
|
+
getComplementaryColor() {
|
|
246
|
+
const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
247
|
+
const newHue = (h + 180) % 360;
|
|
248
|
+
const newHSL = `hsl(${newHue}, ${s}%, ${l}%)`;
|
|
249
|
+
return new Color(newHSL);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @instance Generates a color scheme of analogous colors, including the base color.
|
|
253
|
+
* Analogous colors are next to each other on the color wheel (±30°).
|
|
254
|
+
* @returns An array of three Color instances: [base, left, right].
|
|
255
|
+
*/
|
|
256
|
+
getAnalogousColors() {
|
|
257
|
+
const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
258
|
+
const left = `hsl(${(h + 330) % 360}, ${s}%, ${l}%)`;
|
|
259
|
+
const right = `hsl(${(h + 30) % 360}, ${s}%, ${l}%)`;
|
|
260
|
+
return [this, new Color(left), new Color(right)];
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* @instance Generates a color triad scheme including the base color.
|
|
264
|
+
* Triadic colors are evenly spaced (120° apart) on the color wheel.
|
|
265
|
+
* @returns An array of three Color instances: [base, triad1, triad2].
|
|
266
|
+
*/
|
|
267
|
+
getTriadColors() {
|
|
268
|
+
const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
269
|
+
const c1 = `hsl(${(h + 120) % 360}, ${s}%, ${l}%)`;
|
|
270
|
+
const c2 = `hsl(${(h + 240) % 360}, ${s}%, ${l}%)`;
|
|
271
|
+
return [this, new Color(c1), new Color(c2)];
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* @instance Generates a tetradic color scheme including the base color.
|
|
275
|
+
* Tetradic colors form a rectangle on the color wheel (90° apart).
|
|
276
|
+
* @returns An array of four Color instances: [base, tetrad1, tetrad2, tetrad3].
|
|
277
|
+
*/
|
|
278
|
+
getTetradColors() {
|
|
279
|
+
const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
|
|
280
|
+
const c1 = `hsl(${(h + 90) % 360}, ${s}%, ${l}%)`;
|
|
281
|
+
const c2 = `hsl(${(h + 180) % 360}, ${s}%, ${l}%)`;
|
|
282
|
+
const c3 = `hsl(${(h + 270) % 360}, ${s}%, ${l}%)`;
|
|
283
|
+
return [this, new Color(c1), new Color(c2), new Color(c3)];
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* @instance Gets the `WCAG` accessibility rating between this and another color.
|
|
287
|
+
* @param other - The other color to test contrast against.
|
|
288
|
+
* @returns 'Fail', 'AA', or 'AAA' based on `WCAG 2.1` contrast standards.
|
|
289
|
+
*/
|
|
290
|
+
getWCAGRating(other) {
|
|
291
|
+
const ratio = this.contrastRatio(other);
|
|
292
|
+
if (ratio >= 7)
|
|
293
|
+
return 'AAA';
|
|
294
|
+
if (ratio >= 4.5)
|
|
295
|
+
return 'AA';
|
|
296
|
+
return 'Fail';
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* @instance Determines if the color is light based on its perceived brightness.
|
|
300
|
+
* @returns `true` if light, `false` if dark.
|
|
301
|
+
*/
|
|
302
|
+
isLightColor() {
|
|
303
|
+
const [r, g, b] = (0, helpers_1._extractSolidColorValues)(this.rgb);
|
|
304
|
+
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
|
|
305
|
+
return brightness > 127.5;
|
|
152
306
|
}
|
|
153
307
|
/**
|
|
154
308
|
* @static Checks if a color is in `Hex6` format.
|
|
@@ -169,40 +323,40 @@ class Color {
|
|
|
169
323
|
return /^#[0-9A-Fa-f]{8}$/.test(color);
|
|
170
324
|
}
|
|
171
325
|
/**
|
|
172
|
-
* @static Checks if a color is in `RGB` format.
|
|
326
|
+
* @static Checks if a color is in `RGB` format and within valid ranges.
|
|
173
327
|
*
|
|
174
328
|
* @param color Color to check.
|
|
175
|
-
* @returns
|
|
329
|
+
* @returns `true` if it's a `RGB` color, `false` if not.
|
|
176
330
|
*/
|
|
177
331
|
static isRGB(color) {
|
|
178
|
-
return
|
|
332
|
+
return (0, helpers_1._isRGB)(color);
|
|
179
333
|
}
|
|
180
334
|
/**
|
|
181
|
-
* @static Checks if a color is in `RGBA` format.
|
|
335
|
+
* @static Checks if a color is in `RGBA` format and within valid ranges.
|
|
182
336
|
*
|
|
183
337
|
* @param color Color to check.
|
|
184
|
-
* @returns
|
|
338
|
+
* @returns `true` if it's a `RGBA` color, `false` if not.
|
|
185
339
|
*/
|
|
186
340
|
static isRGBA(color) {
|
|
187
|
-
return
|
|
341
|
+
return (0, helpers_1._isRGBA)(color);
|
|
188
342
|
}
|
|
189
343
|
/**
|
|
190
|
-
* @static Checks if a color is in `HSL` format.
|
|
344
|
+
* @static Checks if a color is in `HSL` format and within valid ranges.
|
|
191
345
|
*
|
|
192
346
|
* @param color Color to check.
|
|
193
|
-
* @returns
|
|
347
|
+
* @returns `true` if it's a `HSL` color, `false` if not.
|
|
194
348
|
*/
|
|
195
349
|
static isHSL(color) {
|
|
196
|
-
return
|
|
350
|
+
return (0, helpers_1._isHSL)(color);
|
|
197
351
|
}
|
|
198
352
|
/**
|
|
199
|
-
* @static Checks if a color is in `HSLA` format.
|
|
353
|
+
* @static Checks if a color is in `HSLA` format and within valid ranges.
|
|
200
354
|
*
|
|
201
355
|
* @param color Color to check.
|
|
202
|
-
* @returns
|
|
356
|
+
* @returns `true` if it's a `HSLA` color, `false` if not.
|
|
203
357
|
*/
|
|
204
358
|
static isHSLA(color) {
|
|
205
|
-
return
|
|
359
|
+
return (0, helpers_1._isHSLA)(color);
|
|
206
360
|
}
|
|
207
361
|
/**
|
|
208
362
|
* @private Converts the given color to all other formats while preserving the original.
|
|
@@ -237,5 +391,20 @@ class Color {
|
|
|
237
391
|
}
|
|
238
392
|
throw new Error(`Unrecognized color format: ${color}`);
|
|
239
393
|
}
|
|
394
|
+
/**
|
|
395
|
+
* @private @static Internal factory to create a Color instance from parsed parts.
|
|
396
|
+
* @param parts All the color parts as object.
|
|
397
|
+
* @returns An instance of `Color`.
|
|
398
|
+
*/
|
|
399
|
+
static #fromParts(parts) {
|
|
400
|
+
const color = Object.create(Color.prototype);
|
|
401
|
+
color.hex = parts.hex;
|
|
402
|
+
color.hex8 = parts.hex8;
|
|
403
|
+
color.rgb = parts.rgb;
|
|
404
|
+
color.rgba = parts.rgba;
|
|
405
|
+
color.hsl = parts.hsl;
|
|
406
|
+
color.hsla = parts.hsla;
|
|
407
|
+
return color;
|
|
408
|
+
}
|
|
240
409
|
}
|
|
241
410
|
exports.Color = Color;
|
|
@@ -2,12 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports._extractAlphaColorValues = exports._extractSolidColorValues = exports._isSimilarToLast = exports._generateRandomHSL = exports._applyOpacity = exports._convertOpacityToHex = void 0;
|
|
4
4
|
exports._isHex6 = _isHex6;
|
|
5
|
-
exports._isRGB = _isRGB;
|
|
6
|
-
exports._isHSL = _isHSL;
|
|
7
5
|
exports._isHex8 = _isHex8;
|
|
6
|
+
exports._isRGB = _isRGB;
|
|
8
7
|
exports._isRGBA = _isRGBA;
|
|
8
|
+
exports._isHSL = _isHSL;
|
|
9
9
|
exports._isHSLA = _isHSLA;
|
|
10
10
|
exports._isValidAlpha = _isValidAlpha;
|
|
11
|
+
exports._isValidRGBComponent = _isValidRGBComponent;
|
|
12
|
+
exports._isValidHue = _isValidHue;
|
|
13
|
+
exports._isValidPercentage = _isValidPercentage;
|
|
11
14
|
/**
|
|
12
15
|
* * Converts opacity percentage (0-100) to a 2-digit hex string.
|
|
13
16
|
*
|
|
@@ -97,7 +100,7 @@ const _extractAlphaColorValues = (colorString) => {
|
|
|
97
100
|
};
|
|
98
101
|
exports._extractAlphaColorValues = _extractAlphaColorValues;
|
|
99
102
|
/**
|
|
100
|
-
*
|
|
103
|
+
* @private Checks if a color is in `Hex` format.
|
|
101
104
|
*
|
|
102
105
|
* @param color Color to check.
|
|
103
106
|
* @returns Boolean: `true` if it's a `Hex` color, `false` if not.
|
|
@@ -106,58 +109,92 @@ function _isHex6(color) {
|
|
|
106
109
|
return /^#[0-9A-Fa-f]{6}$/.test(color);
|
|
107
110
|
}
|
|
108
111
|
/**
|
|
109
|
-
*
|
|
112
|
+
* @private Checks if a color is in `Hex8` format.
|
|
110
113
|
*
|
|
111
114
|
* @param color Color to check.
|
|
112
|
-
* @returns Boolean: `true` if it's
|
|
115
|
+
* @returns Boolean: `true` if it's a `Hex8` color, `false` if not.
|
|
113
116
|
*/
|
|
114
|
-
function
|
|
115
|
-
return
|
|
117
|
+
function _isHex8(color) {
|
|
118
|
+
return /^#[0-9A-Fa-f]{8}$/.test(color);
|
|
116
119
|
}
|
|
117
120
|
/**
|
|
118
|
-
*
|
|
121
|
+
* @private Checks if a color is in `RGB` format and within valid ranges.
|
|
119
122
|
*
|
|
120
123
|
* @param color Color to check.
|
|
121
|
-
* @returns
|
|
124
|
+
* @returns `true` if it's a `RGB` color, `false` if not.
|
|
122
125
|
*/
|
|
123
|
-
function
|
|
124
|
-
|
|
126
|
+
function _isRGB(color) {
|
|
127
|
+
const match = color.match(/^rgb\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)\s*\)$/);
|
|
128
|
+
if (!match)
|
|
129
|
+
return false;
|
|
130
|
+
const [r, g, b] = match.slice(1).map(Number);
|
|
131
|
+
return (_isValidRGBComponent(r) &&
|
|
132
|
+
_isValidRGBComponent(g) &&
|
|
133
|
+
_isValidRGBComponent(b));
|
|
125
134
|
}
|
|
126
135
|
/**
|
|
127
|
-
* @
|
|
128
|
-
* Checks if a color is in `Hex8` format.
|
|
136
|
+
* @private Checks if a color is in `RGBA` format and within valid ranges.
|
|
129
137
|
*
|
|
130
138
|
* @param color Color to check.
|
|
131
|
-
* @returns
|
|
139
|
+
* @returns `true` if it's a `RGBA` color, `false` if not.
|
|
132
140
|
*/
|
|
133
|
-
function
|
|
134
|
-
|
|
141
|
+
function _isRGBA(color) {
|
|
142
|
+
const match = color.match(/^rgba\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(0|1|0?\.\d+)\s*\)$/);
|
|
143
|
+
if (!match)
|
|
144
|
+
return false;
|
|
145
|
+
const [r, g, b, a] = match.slice(1).map(Number);
|
|
146
|
+
return (_isValidRGBComponent(r) &&
|
|
147
|
+
_isValidRGBComponent(g) &&
|
|
148
|
+
_isValidRGBComponent(b) &&
|
|
149
|
+
_isValidAlpha(a));
|
|
135
150
|
}
|
|
136
151
|
/**
|
|
137
|
-
* @
|
|
138
|
-
* Checks if a color is in `RGBA` format.
|
|
152
|
+
* @private Checks if a color is in `HSL` format and within valid ranges.
|
|
139
153
|
*
|
|
140
154
|
* @param color Color to check.
|
|
141
|
-
* @returns
|
|
155
|
+
* @returns `true` if it's a `HSL` color, `false` if not.
|
|
142
156
|
*/
|
|
143
|
-
function
|
|
144
|
-
|
|
157
|
+
function _isHSL(color) {
|
|
158
|
+
const match = color.match(/^hsl\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)%,\s*(\d{1,3}(?:\.\d+)?)%\s*\)$/);
|
|
159
|
+
if (!match)
|
|
160
|
+
return false;
|
|
161
|
+
const [h, s, l] = match.slice(1).map(Number);
|
|
162
|
+
return _isValidHue(h) && _isValidPercentage(s) && _isValidPercentage(l);
|
|
145
163
|
}
|
|
146
164
|
/**
|
|
147
|
-
* @
|
|
148
|
-
* Checks if a color is in `HSLA` format.
|
|
165
|
+
* @private Checks if a color is in `HSLA` format and within valid ranges.
|
|
149
166
|
*
|
|
150
167
|
* @param color Color to check.
|
|
151
|
-
* @returns
|
|
168
|
+
* @returns `true` if it's a `HSLA` color, `false` if not.
|
|
152
169
|
*/
|
|
153
170
|
function _isHSLA(color) {
|
|
154
|
-
|
|
171
|
+
const match = color.match(/^hsla\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)%,\s*(\d{1,3}(?:\.\d+)?)%,\s*(0|1|0?\.\d+)\s*\)$/);
|
|
172
|
+
if (!match)
|
|
173
|
+
return false;
|
|
174
|
+
const [h, s, l, a] = match.slice(1).map(Number);
|
|
175
|
+
return (_isValidHue(h) &&
|
|
176
|
+
_isValidPercentage(s) &&
|
|
177
|
+
_isValidPercentage(l) &&
|
|
178
|
+
_isValidAlpha(a));
|
|
155
179
|
}
|
|
156
180
|
/**
|
|
157
|
-
*
|
|
181
|
+
* @private Checks if a number is valid alpha value.
|
|
182
|
+
*
|
|
158
183
|
* @param value Alpha value to check.
|
|
159
|
-
* @returns
|
|
184
|
+
* @returns `true` if it's a valid alpha value, `false` if not.
|
|
160
185
|
*/
|
|
161
186
|
function _isValidAlpha(value) {
|
|
162
187
|
return value >= 0 && value <= 1 && !isNaN(value);
|
|
163
188
|
}
|
|
189
|
+
/** * @private Validates RGB component (0–255). */
|
|
190
|
+
function _isValidRGBComponent(value) {
|
|
191
|
+
return value >= 0 && value <= 255;
|
|
192
|
+
}
|
|
193
|
+
/** * @private Validates HSL hue (0–360). */
|
|
194
|
+
function _isValidHue(value) {
|
|
195
|
+
return value >= 0 && value <= 360;
|
|
196
|
+
}
|
|
197
|
+
/** * @private Validates HSL percentage components (0–100). */
|
|
198
|
+
function _isValidPercentage(value) {
|
|
199
|
+
return value >= 0 && value <= 100;
|
|
200
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ColorType, Hex6, Hex8, HSL, HSLA, Percent, RGB, RGBA } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Class representing a color and its conversions among `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` and `HSLA` formats.
|
|
4
|
-
* * It has
|
|
4
|
+
* * It has 13 instance methods to manipulate and play with the color values.
|
|
5
5
|
* * It has 6 static methods that can be used to check if a color is in `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` format.
|
|
6
6
|
*
|
|
7
7
|
* @property hex - The color in `Hex` format.
|
|
@@ -32,7 +32,7 @@ export declare class Color {
|
|
|
32
32
|
* - `HSLA` (e.g., `hsla(14, 100%, 60%, 1)`)
|
|
33
33
|
*
|
|
34
34
|
* Additionally:
|
|
35
|
-
* -
|
|
35
|
+
* - It has 13 instance methods to manipulate and play with the color values.
|
|
36
36
|
* - Use static methods like `Color.isHex6(color)` to validate color strings.
|
|
37
37
|
*
|
|
38
38
|
* @example
|
|
@@ -58,7 +58,7 @@ export declare class Color {
|
|
|
58
58
|
* You can create a color from any of these formats, and the class will populate the rest.
|
|
59
59
|
*
|
|
60
60
|
* Additionally:
|
|
61
|
-
* -
|
|
61
|
+
* - It has 13 instance methods to manipulate and play with the color values.
|
|
62
62
|
* - Use available 6 static methods like `Color.isHex6(color)` to validate color strings.
|
|
63
63
|
*
|
|
64
64
|
* @param toConvert - A color string in any supported format (`Hex`, `Hex8`, `RGB`, `RGBA`, `HSL`, or `HSLA`) to convert in all other formats (includes the current format).
|
|
@@ -84,12 +84,12 @@ export declare class Color {
|
|
|
84
84
|
/** - Iterates over the color representations (Hex, RGB, HSL). */
|
|
85
85
|
[Symbol.iterator](): Generator<Hex6 | RGB | HSL | Hex8 | RGBA | HSLA, void, unknown>;
|
|
86
86
|
/**
|
|
87
|
-
*
|
|
88
|
-
* - For solid colors (Hex6/RGB/HSL): Adds an alpha channel with the specified opacity
|
|
89
|
-
* - For alpha colors (Hex8/RGBA/HSLA): Updates the existing alpha channel
|
|
87
|
+
* @instance Applies or modifies the opacity of a color. Mutate the original instance.
|
|
88
|
+
* - For solid colors (Hex6/RGB/HSL): Adds an alpha channel with the specified opacity.
|
|
89
|
+
* - For alpha colors (Hex8/RGBA/HSLA): Updates the existing alpha channel.
|
|
90
90
|
*
|
|
91
|
-
* @param opacity - A number between 0-100 representing the opacity percentage
|
|
92
|
-
* @returns
|
|
91
|
+
* @param opacity - A number between 0-100 representing the opacity percentage.
|
|
92
|
+
* @returns A new instance of `Color` containing all color formats with the applied opacity.
|
|
93
93
|
*
|
|
94
94
|
* @example
|
|
95
95
|
* const color = new Color("#ff0000");
|
|
@@ -101,7 +101,82 @@ export declare class Color {
|
|
|
101
101
|
* const alpha75 = alphaColor.applyOpacity(75); // Change to 75% opacity
|
|
102
102
|
* console.log(alpha75.hex8); // #FF0000BF
|
|
103
103
|
*/
|
|
104
|
-
applyOpacity(opacity:
|
|
104
|
+
applyOpacity(opacity: Percent): Color;
|
|
105
|
+
/**
|
|
106
|
+
* @instance Darkens the color by reducing the lightness by the given percentage.
|
|
107
|
+
* @param percent - The percentage to darken (0–100).
|
|
108
|
+
* @returns A new `Color` instance with the modified darkness.
|
|
109
|
+
*/
|
|
110
|
+
applyDarkness(percent: Percent): Color;
|
|
111
|
+
/**
|
|
112
|
+
* @instance Lightens the color by increasing the lightness by the given percentage.
|
|
113
|
+
* @param percent - The percentage to brighten (0–100).
|
|
114
|
+
* @returns A new `Color` instance with the modified lightness.
|
|
115
|
+
*/
|
|
116
|
+
applyBrightness(percent: Percent): Color;
|
|
117
|
+
/**
|
|
118
|
+
* @instance Reduces the saturation of the color to make it appear duller.
|
|
119
|
+
* @param percent - The percentage to reduce saturation (0–100).
|
|
120
|
+
* @returns A new `Color` instance with the modified saturation.
|
|
121
|
+
*/
|
|
122
|
+
applyDullness(percent: Percent): Color;
|
|
123
|
+
/**
|
|
124
|
+
* @instance Softens the color toward white by reducing saturation and increasing lightness based on a percentage.
|
|
125
|
+
* - *This creates a soft UI-like white shade effect (similar to some UI libraries' light color scale).*
|
|
126
|
+
* @param percent - Value from 0 to 100 representing how far to push the color toward white.
|
|
127
|
+
* @returns A new `Color` instance shifted toward white.
|
|
128
|
+
*/
|
|
129
|
+
applyWhiteShade(percent: Percent): Color;
|
|
130
|
+
/**
|
|
131
|
+
* @instance Blends the current color with another color based on the given weight.
|
|
132
|
+
* @param other - The color in any 6 `(Hex, Hex8 RGB, RGBA, HSL or HSLA)` to blend with.
|
|
133
|
+
* @param weight - A number from 0 to 1 indicating the weight of the other color. Defaults to `0.5`.
|
|
134
|
+
* - `weight = 0` → only the original color (this)
|
|
135
|
+
* - `weight = 1` → only the other color
|
|
136
|
+
* - `weight = 0.5` → equal blend between the two
|
|
137
|
+
* @returns A new `Color` instance representing the blended result.
|
|
138
|
+
*/
|
|
139
|
+
blendWith(other: ColorType, weight?: number): Color;
|
|
140
|
+
/**
|
|
141
|
+
* @instance Calculates the contrast ratio between this color and another color (WCAG).
|
|
142
|
+
* @param other - The other color to compare against.
|
|
143
|
+
* @returns A number representing the contrast ratio (rounded to 2 decimal places).
|
|
144
|
+
*/
|
|
145
|
+
contrastRatio(other: ColorType): number;
|
|
146
|
+
/**
|
|
147
|
+
* @instance Returns the complementary color by rotating the hue 180 degrees.
|
|
148
|
+
* @returns A new Color that is the complement of the current color.
|
|
149
|
+
*/
|
|
150
|
+
getComplementaryColor(): Color;
|
|
151
|
+
/**
|
|
152
|
+
* @instance Generates a color scheme of analogous colors, including the base color.
|
|
153
|
+
* Analogous colors are next to each other on the color wheel (±30°).
|
|
154
|
+
* @returns An array of three Color instances: [base, left, right].
|
|
155
|
+
*/
|
|
156
|
+
getAnalogousColors(): [Color, Color, Color];
|
|
157
|
+
/**
|
|
158
|
+
* @instance Generates a color triad scheme including the base color.
|
|
159
|
+
* Triadic colors are evenly spaced (120° apart) on the color wheel.
|
|
160
|
+
* @returns An array of three Color instances: [base, triad1, triad2].
|
|
161
|
+
*/
|
|
162
|
+
getTriadColors(): [Color, Color, Color];
|
|
163
|
+
/**
|
|
164
|
+
* @instance Generates a tetradic color scheme including the base color.
|
|
165
|
+
* Tetradic colors form a rectangle on the color wheel (90° apart).
|
|
166
|
+
* @returns An array of four Color instances: [base, tetrad1, tetrad2, tetrad3].
|
|
167
|
+
*/
|
|
168
|
+
getTetradColors(): [Color, Color, Color, Color];
|
|
169
|
+
/**
|
|
170
|
+
* @instance Gets the `WCAG` accessibility rating between this and another color.
|
|
171
|
+
* @param other - The other color to test contrast against.
|
|
172
|
+
* @returns 'Fail', 'AA', or 'AAA' based on `WCAG 2.1` contrast standards.
|
|
173
|
+
*/
|
|
174
|
+
getWCAGRating(other: ColorType): 'Fail' | 'AA' | 'AAA';
|
|
175
|
+
/**
|
|
176
|
+
* @instance Determines if the color is light based on its perceived brightness.
|
|
177
|
+
* @returns `true` if light, `false` if dark.
|
|
178
|
+
*/
|
|
179
|
+
isLightColor(): boolean;
|
|
105
180
|
/**
|
|
106
181
|
* @static Checks if a color is in `Hex6` format.
|
|
107
182
|
*
|
|
@@ -117,31 +192,31 @@ export declare class Color {
|
|
|
117
192
|
*/
|
|
118
193
|
static isHex8(color: string): color is Hex8;
|
|
119
194
|
/**
|
|
120
|
-
* @static Checks if a color is in `RGB` format.
|
|
195
|
+
* @static Checks if a color is in `RGB` format and within valid ranges.
|
|
121
196
|
*
|
|
122
197
|
* @param color Color to check.
|
|
123
|
-
* @returns
|
|
198
|
+
* @returns `true` if it's a `RGB` color, `false` if not.
|
|
124
199
|
*/
|
|
125
200
|
static isRGB(color: string): color is RGB;
|
|
126
201
|
/**
|
|
127
|
-
* @static Checks if a color is in `RGBA` format.
|
|
202
|
+
* @static Checks if a color is in `RGBA` format and within valid ranges.
|
|
128
203
|
*
|
|
129
204
|
* @param color Color to check.
|
|
130
|
-
* @returns
|
|
205
|
+
* @returns `true` if it's a `RGBA` color, `false` if not.
|
|
131
206
|
*/
|
|
132
207
|
static isRGBA(color: string): color is RGBA;
|
|
133
208
|
/**
|
|
134
|
-
* @static Checks if a color is in `HSL` format.
|
|
209
|
+
* @static Checks if a color is in `HSL` format and within valid ranges.
|
|
135
210
|
*
|
|
136
211
|
* @param color Color to check.
|
|
137
|
-
* @returns
|
|
212
|
+
* @returns `true` if it's a `HSL` color, `false` if not.
|
|
138
213
|
*/
|
|
139
214
|
static isHSL(color: string): color is HSL;
|
|
140
215
|
/**
|
|
141
|
-
* @static Checks if a color is in `HSLA` format.
|
|
216
|
+
* @static Checks if a color is in `HSLA` format and within valid ranges.
|
|
142
217
|
*
|
|
143
218
|
* @param color Color to check.
|
|
144
|
-
* @returns
|
|
219
|
+
* @returns `true` if it's a `HSLA` color, `false` if not.
|
|
145
220
|
*/
|
|
146
221
|
static isHSLA(color: string): color is HSLA;
|
|
147
222
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Color.d.ts","sourceRoot":"","sources":["../../../src/colors/Color.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Color.d.ts","sourceRoot":"","sources":["../../../src/colors/Color.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAIX,SAAS,EACT,IAAI,EACJ,IAAI,EACJ,GAAG,EACH,IAAI,EACJ,OAAO,EACP,GAAG,EACH,IAAI,EAEJ,MAAM,SAAS,CAAC;AAKjB;;;;;;;;;;;GAWG;AACH,qBAAa,KAAK;;IACV,GAAG,EAAE,IAAI,CAAC;IACV,IAAI,EAAE,IAAI,CAAC;I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KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;IAI3C;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;IAI3C;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,GAAG;IAIzC;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;IAI3C;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,GAAG;IAIzC;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;CAkD3C"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { AlphaValue, ColorNumbers, ColorNumbersAlpha, Hex6, Hex8, HSL, HSLA,
|
|
1
|
+
import type { AlphaValue, ColorNumbers, ColorNumbersAlpha, Hex6, Hex8, HSL, HSLA, Percent, RGB, RGBA } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Converts opacity percentage (0-100) to a 2-digit hex string.
|
|
4
4
|
*
|
|
5
5
|
* @param opacity - The opacity value as a percentage (0-100).
|
|
6
6
|
* @returns A 2-digit hex string representing the alpha value.
|
|
7
7
|
*/
|
|
8
|
-
export declare const _convertOpacityToHex: (opacity:
|
|
8
|
+
export declare const _convertOpacityToHex: (opacity: Percent) => string;
|
|
9
9
|
/**
|
|
10
10
|
* * Applies an opacity value to a color string.
|
|
11
11
|
* @param color The color string to apply opacity to.
|
|
@@ -45,54 +45,58 @@ export declare const _extractSolidColorValues: (colorString: HSL | RGB) => Color
|
|
|
45
45
|
*/
|
|
46
46
|
export declare const _extractAlphaColorValues: (colorString: HSLA | RGBA) => ColorNumbersAlpha;
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* @private Checks if a color is in `Hex` format.
|
|
49
49
|
*
|
|
50
50
|
* @param color Color to check.
|
|
51
51
|
* @returns Boolean: `true` if it's a `Hex` color, `false` if not.
|
|
52
52
|
*/
|
|
53
53
|
export declare function _isHex6(color: string): color is Hex6;
|
|
54
54
|
/**
|
|
55
|
-
*
|
|
55
|
+
* @private Checks if a color is in `Hex8` format.
|
|
56
56
|
*
|
|
57
57
|
* @param color Color to check.
|
|
58
|
-
* @returns Boolean: `true` if it's
|
|
58
|
+
* @returns Boolean: `true` if it's a `Hex8` color, `false` if not.
|
|
59
59
|
*/
|
|
60
|
-
export declare function
|
|
60
|
+
export declare function _isHex8(color: string): color is Hex8;
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
* @private Checks if a color is in `RGB` format and within valid ranges.
|
|
63
63
|
*
|
|
64
64
|
* @param color Color to check.
|
|
65
|
-
* @returns
|
|
65
|
+
* @returns `true` if it's a `RGB` color, `false` if not.
|
|
66
66
|
*/
|
|
67
|
-
export declare function
|
|
67
|
+
export declare function _isRGB(color: string): color is RGB;
|
|
68
68
|
/**
|
|
69
|
-
* @
|
|
70
|
-
* Checks if a color is in `Hex8` format.
|
|
69
|
+
* @private Checks if a color is in `RGBA` format and within valid ranges.
|
|
71
70
|
*
|
|
72
71
|
* @param color Color to check.
|
|
73
|
-
* @returns
|
|
72
|
+
* @returns `true` if it's a `RGBA` color, `false` if not.
|
|
74
73
|
*/
|
|
75
|
-
export declare function
|
|
74
|
+
export declare function _isRGBA(color: string): color is RGBA;
|
|
76
75
|
/**
|
|
77
|
-
* @
|
|
78
|
-
* Checks if a color is in `RGBA` format.
|
|
76
|
+
* @private Checks if a color is in `HSL` format and within valid ranges.
|
|
79
77
|
*
|
|
80
78
|
* @param color Color to check.
|
|
81
|
-
* @returns
|
|
79
|
+
* @returns `true` if it's a `HSL` color, `false` if not.
|
|
82
80
|
*/
|
|
83
|
-
export declare function
|
|
81
|
+
export declare function _isHSL(color: string): color is HSL;
|
|
84
82
|
/**
|
|
85
|
-
* @
|
|
86
|
-
* Checks if a color is in `HSLA` format.
|
|
83
|
+
* @private Checks if a color is in `HSLA` format and within valid ranges.
|
|
87
84
|
*
|
|
88
85
|
* @param color Color to check.
|
|
89
|
-
* @returns
|
|
86
|
+
* @returns `true` if it's a `HSLA` color, `false` if not.
|
|
90
87
|
*/
|
|
91
88
|
export declare function _isHSLA(color: string): color is HSLA;
|
|
92
89
|
/**
|
|
93
|
-
*
|
|
90
|
+
* @private Checks if a number is valid alpha value.
|
|
91
|
+
*
|
|
94
92
|
* @param value Alpha value to check.
|
|
95
|
-
* @returns
|
|
93
|
+
* @returns `true` if it's a valid alpha value, `false` if not.
|
|
96
94
|
*/
|
|
97
95
|
export declare function _isValidAlpha(value: number): value is AlphaValue;
|
|
96
|
+
/** * @private Validates RGB component (0–255). */
|
|
97
|
+
export declare function _isValidRGBComponent(value: number): boolean;
|
|
98
|
+
/** * @private Validates HSL hue (0–360). */
|
|
99
|
+
export declare function _isValidHue(value: number): boolean;
|
|
100
|
+
/** * @private Validates HSL percentage components (0–100). */
|
|
101
|
+
export declare function _isValidPercentage(value: number): boolean;
|
|
98
102
|
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/colors/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,UAAU,EACV,YAAY,EACZ,iBAAiB,EACjB,IAAI,EACJ,IAAI,EACJ,GAAG,EACH,IAAI,EACJ,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/colors/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,UAAU,EACV,YAAY,EACZ,iBAAiB,EACjB,IAAI,EACJ,IAAI,EACJ,GAAG,EACH,IAAI,EACJ,OAAO,EACP,GAAG,EACH,IAAI,EACJ,MAAM,SAAS,CAAC;AAEjB;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,YAAa,OAA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D;AAC9D,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEzD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ColorInputArray,
|
|
1
|
+
import type { ColorInputArray, Percent } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Generates a hex color based on the first character (initial) of a string or number.
|
|
4
4
|
*
|
|
@@ -9,7 +9,7 @@ import type { ColorInputArray, OpacityValue } from './types';
|
|
|
9
9
|
* @param opacity - A value from 0 to 100 representing the opacity percentage.
|
|
10
10
|
* @returns A hex color for the first character of the provided string/number.
|
|
11
11
|
*/
|
|
12
|
-
export declare function getColorForInitial(input: string | number, opacity?:
|
|
12
|
+
export declare function getColorForInitial(input: string | number, opacity?: Percent): string;
|
|
13
13
|
/**
|
|
14
14
|
* * Generates an array of hex colors based on the first character (initial) of an array of strings/numbers or even nested arrays of strings/numbers.
|
|
15
15
|
*
|
|
@@ -21,5 +21,5 @@ export declare function getColorForInitial(input: string | number, opacity?: Opa
|
|
|
21
21
|
* @param opacity - A value from 0 to 100 representing the opacity percentage.
|
|
22
22
|
* @returns A hex color for a string/number, or an array of hex colors for each element of the provided array.
|
|
23
23
|
*/
|
|
24
|
-
export declare function getColorForInitial(input: ColorInputArray, opacity?:
|
|
24
|
+
export declare function getColorForInitial(input: ColorInputArray, opacity?: Percent): string[];
|
|
25
25
|
//# sourceMappingURL=initials.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initials.d.ts","sourceRoot":"","sources":["../../../src/colors/initials.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAc,eAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"initials.d.ts","sourceRoot":"","sources":["../../../src/colors/initials.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAc,eAAe,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAEpE;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,OAAO,CAAC,EAAE,OAAO,GACf,MAAM,CAAC;AAEV;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,eAAe,EACtB,OAAO,CAAC,EAAE,OAAO,GACf,MAAM,EAAE,CAAC"}
|
|
@@ -4,8 +4,8 @@ export type ColorInput = string | number;
|
|
|
4
4
|
/** - An array of strings/numbers or nested arrays of strings/numbers for generating colors. */
|
|
5
5
|
export interface ColorInputArray extends Array<ColorInput | ColorInputArray> {
|
|
6
6
|
}
|
|
7
|
-
/** -
|
|
8
|
-
export type
|
|
7
|
+
/** - Number value in percentage `(0% - 100%)` without `%` symbol. */
|
|
8
|
+
export type Percent = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100;
|
|
9
9
|
/**
|
|
10
10
|
* * Represents a hexadecimal color code.
|
|
11
11
|
* * Format: `#3C6945`
|
|
@@ -100,4 +100,19 @@ export interface ConvertedColors<T extends ColorType> extends Record<string, Col
|
|
|
100
100
|
}
|
|
101
101
|
/** Represents an alpha value between 0 and 1 */
|
|
102
102
|
export type AlphaValue = Branded<number, 'AlphaValue'>;
|
|
103
|
+
/** An Object representing all the colors from `Color` constructor. */
|
|
104
|
+
export interface Colors {
|
|
105
|
+
/** - `Hex` color (e.g., `#ff5733`) */
|
|
106
|
+
hex: Hex6;
|
|
107
|
+
/** - `Hex8` color (Hex with opacity, e.g., `#ff573380`) */
|
|
108
|
+
hex8: Hex8;
|
|
109
|
+
/** - `RGB` color (e.g., `rgb(255, 87, 51)`) */
|
|
110
|
+
rgb: RGB;
|
|
111
|
+
/** - `RGBA` color (e.g., `rgba(255, 87, 51, 1)`) */
|
|
112
|
+
rgba: RGBA;
|
|
113
|
+
/** - `HSL` color (e.g., `hsl(14, 100%, 60%)`) */
|
|
114
|
+
hsl: HSL;
|
|
115
|
+
/** - `HSLA` color (e.g., `hsla(14, 100%, 60%, 1)`) */
|
|
116
|
+
hsla: HSLA;
|
|
117
|
+
}
|
|
103
118
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/colors/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC,+CAA+C;AAC/C,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEzC,+FAA+F;AAC/F,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,UAAU,GAAG,eAAe,CAAC;CAAG;AAE/E,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/colors/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC,+CAA+C;AAC/C,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEzC,+FAA+F;AAC/F,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,UAAU,GAAG,eAAe,CAAC;CAAG;AAE/E,qEAAqE;AACrE,MAAM,MAAM,OAAO,GAChB,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GAC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IAAI,EAAE,IAAI,CAAC;IACX,iDAAiD;IACjD,GAAG,EAAE,GAAG,CAAC;IACT,sDAAsD;IACtD,IAAI,EAAE,IAAI,CAAC;CACX"}
|
|
@@ -11,7 +11,7 @@ export declare const MINUTE_FORMATS: readonly ["mm", "m"];
|
|
|
11
11
|
export declare const SECOND_FORMATS: readonly ["ss", "s"];
|
|
12
12
|
export declare const MILLISECOND_FORMATS: readonly ["ms", "mss"];
|
|
13
13
|
export declare const TIME_FORMATS: readonly ["a", "A"];
|
|
14
|
-
export declare const sortedFormats: ("a" | "M" | "D" | "A" | "YYYY" | "YY" | "yyyy" | "yy" | "MM" | "mmm" | "mmmm" | "DD" | "Do" | "d" | "dd" | "ddd" | "H" | "HH" | "hh" | "
|
|
14
|
+
export declare const sortedFormats: ("a" | "M" | "D" | "h" | "s" | "A" | "YYYY" | "YY" | "yyyy" | "yy" | "MM" | "mmm" | "mmmm" | "DD" | "Do" | "d" | "dd" | "ddd" | "H" | "HH" | "hh" | "mm" | "m" | "ss" | "ms" | "mss")[];
|
|
15
15
|
export declare const TIME_ZONES: {
|
|
16
16
|
/** International Date Line West (Baker Island, Howland Island) */
|
|
17
17
|
readonly IDLW: number;
|
package/dist/esm/colors/Color.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { convertColorCode } from './convert';
|
|
2
|
-
import { _convertOpacityToHex, _extractAlphaColorValues, _extractSolidColorValues, } from './helpers';
|
|
2
|
+
import { _convertOpacityToHex, _extractAlphaColorValues, _extractSolidColorValues, _isHSL, _isHSLA, _isRGB, _isRGBA, } from './helpers';
|
|
3
3
|
import { generateRandomHSLColor } from './random';
|
|
4
4
|
const hsl = generateRandomHSLColor();
|
|
5
5
|
const { hex, rgb } = convertColorCode(hsl);
|
|
6
6
|
/**
|
|
7
7
|
* * Class representing a color and its conversions among `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` and `HSLA` formats.
|
|
8
|
-
* * It has
|
|
8
|
+
* * It has 13 instance methods to manipulate and play with the color values.
|
|
9
9
|
* * It has 6 static methods that can be used to check if a color is in `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` format.
|
|
10
10
|
*
|
|
11
11
|
* @property hex - The color in `Hex` format.
|
|
@@ -38,7 +38,7 @@ export class Color {
|
|
|
38
38
|
* If no color is passed, a random color will be generated.
|
|
39
39
|
*
|
|
40
40
|
* Additionally:
|
|
41
|
-
* -
|
|
41
|
+
* - It has 13 instance methods to manipulate and play with the color values.
|
|
42
42
|
* - Use static methods like `Color.isHex6(color)` to validate color strings.
|
|
43
43
|
*
|
|
44
44
|
* @param toConvert - An optional input color string in any supported format (`Hex`, `Hex8`, `RGB`, `RGBA`, `HSL`, or `HSLA`) to convert in all other (includes the current format) formats.
|
|
@@ -115,12 +115,12 @@ export class Color {
|
|
|
115
115
|
yield this.hsla;
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
118
|
-
*
|
|
119
|
-
* - For solid colors (Hex6/RGB/HSL): Adds an alpha channel with the specified opacity
|
|
120
|
-
* - For alpha colors (Hex8/RGBA/HSLA): Updates the existing alpha channel
|
|
118
|
+
* @instance Applies or modifies the opacity of a color. Mutate the original instance.
|
|
119
|
+
* - For solid colors (Hex6/RGB/HSL): Adds an alpha channel with the specified opacity.
|
|
120
|
+
* - For alpha colors (Hex8/RGBA/HSLA): Updates the existing alpha channel.
|
|
121
121
|
*
|
|
122
|
-
* @param opacity - A number between 0-100 representing the opacity percentage
|
|
123
|
-
* @returns
|
|
122
|
+
* @param opacity - A number between 0-100 representing the opacity percentage.
|
|
123
|
+
* @returns A new instance of `Color` containing all color formats with the applied opacity.
|
|
124
124
|
*
|
|
125
125
|
* @example
|
|
126
126
|
* const color = new Color("#ff0000");
|
|
@@ -138,14 +138,168 @@ export class Color {
|
|
|
138
138
|
const alphaDecimal = validOpacity / 100;
|
|
139
139
|
const rgbValues = _extractSolidColorValues(this.rgb);
|
|
140
140
|
const hslValues = _extractSolidColorValues(this.hsl);
|
|
141
|
-
return {
|
|
141
|
+
return Color.#fromParts({
|
|
142
142
|
hex: this.hex.slice(0, 7).toUpperCase(),
|
|
143
143
|
hex8: `${this.hex.slice(0, 7)}${alphaHex}`.toUpperCase(),
|
|
144
144
|
rgb: `rgb(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]})`,
|
|
145
145
|
rgba: `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, ${alphaDecimal})`,
|
|
146
146
|
hsl: `hsl(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%)`,
|
|
147
147
|
hsla: `hsla(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%, ${alphaDecimal})`,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @instance Darkens the color by reducing the lightness by the given percentage.
|
|
152
|
+
* @param percent - The percentage to darken (0–100).
|
|
153
|
+
* @returns A new `Color` instance with the modified darkness.
|
|
154
|
+
*/
|
|
155
|
+
applyDarkness(percent) {
|
|
156
|
+
const [h, s, l] = _extractSolidColorValues(this.hsl);
|
|
157
|
+
const newL = Math.max(0, l - percent);
|
|
158
|
+
const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
|
|
159
|
+
return new Color(newHSL);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* @instance Lightens the color by increasing the lightness by the given percentage.
|
|
163
|
+
* @param percent - The percentage to brighten (0–100).
|
|
164
|
+
* @returns A new `Color` instance with the modified lightness.
|
|
165
|
+
*/
|
|
166
|
+
applyBrightness(percent) {
|
|
167
|
+
const [h, s, l] = _extractSolidColorValues(this.hsl);
|
|
168
|
+
const newL = Math.min(100, l + percent);
|
|
169
|
+
const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
|
|
170
|
+
return new Color(newHSL);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* @instance Reduces the saturation of the color to make it appear duller.
|
|
174
|
+
* @param percent - The percentage to reduce saturation (0–100).
|
|
175
|
+
* @returns A new `Color` instance with the modified saturation.
|
|
176
|
+
*/
|
|
177
|
+
applyDullness(percent) {
|
|
178
|
+
const [h, s, l] = _extractSolidColorValues(this.hsl);
|
|
179
|
+
const newS = Math.max(0, s - percent);
|
|
180
|
+
const newHSL = `hsl(${h}, ${newS}%, ${l}%)`;
|
|
181
|
+
return new Color(newHSL);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @instance Softens the color toward white by reducing saturation and increasing lightness based on a percentage.
|
|
185
|
+
* - *This creates a soft UI-like white shade effect (similar to some UI libraries' light color scale).*
|
|
186
|
+
* @param percent - Value from 0 to 100 representing how far to push the color toward white.
|
|
187
|
+
* @returns A new `Color` instance shifted toward white.
|
|
188
|
+
*/
|
|
189
|
+
applyWhiteShade(percent) {
|
|
190
|
+
const [h, s, l] = _extractSolidColorValues(this.hsl);
|
|
191
|
+
// Cap values to avoid overshooting
|
|
192
|
+
const newS = Math.max(0, s - (s * percent) / 100);
|
|
193
|
+
const newL = Math.min(100, l + ((100 - l) * percent) / 100);
|
|
194
|
+
const newHSL = `hsl(${h}, ${newS}%, ${newL}%)`;
|
|
195
|
+
return new Color(newHSL);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* @instance Blends the current color with another color based on the given weight.
|
|
199
|
+
* @param other - The color in any 6 `(Hex, Hex8 RGB, RGBA, HSL or HSLA)` to blend with.
|
|
200
|
+
* @param weight - A number from 0 to 1 indicating the weight of the other color. Defaults to `0.5`.
|
|
201
|
+
* - `weight = 0` → only the original color (this)
|
|
202
|
+
* - `weight = 1` → only the other color
|
|
203
|
+
* - `weight = 0.5` → equal blend between the two
|
|
204
|
+
* @returns A new `Color` instance representing the blended result.
|
|
205
|
+
*/
|
|
206
|
+
blendWith(other, weight = 0.5) {
|
|
207
|
+
const w = Math.max(0, Math.min(1, weight));
|
|
208
|
+
const converted = new Color(other);
|
|
209
|
+
const rgb1 = _extractSolidColorValues(this.rgb);
|
|
210
|
+
const rgb2 = _extractSolidColorValues(converted.rgb);
|
|
211
|
+
const blended = rgb1.map((c, i) => Math.round(c * (1 - w) + rgb2[i] * w));
|
|
212
|
+
const blendedRGB = `rgb(${blended[0]}, ${blended[1]}, ${blended[2]})`;
|
|
213
|
+
return new Color(blendedRGB);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* @instance Calculates the contrast ratio between this color and another color (WCAG).
|
|
217
|
+
* @param other - The other color to compare against.
|
|
218
|
+
* @returns A number representing the contrast ratio (rounded to 2 decimal places).
|
|
219
|
+
*/
|
|
220
|
+
contrastRatio(other) {
|
|
221
|
+
const newColor = new Color(other);
|
|
222
|
+
const luminance = (rgb) => {
|
|
223
|
+
const [r, g, b] = _extractSolidColorValues(rgb).map((v) => {
|
|
224
|
+
const c = v / 255;
|
|
225
|
+
return c <= 0.03928 ?
|
|
226
|
+
c / 12.92
|
|
227
|
+
: Math.pow((c + 0.055) / 1.055, 2.4);
|
|
228
|
+
});
|
|
229
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
148
230
|
};
|
|
231
|
+
const lum1 = luminance(this.rgb);
|
|
232
|
+
const lum2 = luminance(newColor.rgb);
|
|
233
|
+
const brighter = Math.max(lum1, lum2);
|
|
234
|
+
const darker = Math.min(lum1, lum2);
|
|
235
|
+
const ratio = (brighter + 0.05) / (darker + 0.05);
|
|
236
|
+
return Math.round(ratio * 100) / 100;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* @instance Returns the complementary color by rotating the hue 180 degrees.
|
|
240
|
+
* @returns A new Color that is the complement of the current color.
|
|
241
|
+
*/
|
|
242
|
+
getComplementaryColor() {
|
|
243
|
+
const [h, s, l] = _extractSolidColorValues(this.hsl);
|
|
244
|
+
const newHue = (h + 180) % 360;
|
|
245
|
+
const newHSL = `hsl(${newHue}, ${s}%, ${l}%)`;
|
|
246
|
+
return new Color(newHSL);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* @instance Generates a color scheme of analogous colors, including the base color.
|
|
250
|
+
* Analogous colors are next to each other on the color wheel (±30°).
|
|
251
|
+
* @returns An array of three Color instances: [base, left, right].
|
|
252
|
+
*/
|
|
253
|
+
getAnalogousColors() {
|
|
254
|
+
const [h, s, l] = _extractSolidColorValues(this.hsl);
|
|
255
|
+
const left = `hsl(${(h + 330) % 360}, ${s}%, ${l}%)`;
|
|
256
|
+
const right = `hsl(${(h + 30) % 360}, ${s}%, ${l}%)`;
|
|
257
|
+
return [this, new Color(left), new Color(right)];
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* @instance Generates a color triad scheme including the base color.
|
|
261
|
+
* Triadic colors are evenly spaced (120° apart) on the color wheel.
|
|
262
|
+
* @returns An array of three Color instances: [base, triad1, triad2].
|
|
263
|
+
*/
|
|
264
|
+
getTriadColors() {
|
|
265
|
+
const [h, s, l] = _extractSolidColorValues(this.hsl);
|
|
266
|
+
const c1 = `hsl(${(h + 120) % 360}, ${s}%, ${l}%)`;
|
|
267
|
+
const c2 = `hsl(${(h + 240) % 360}, ${s}%, ${l}%)`;
|
|
268
|
+
return [this, new Color(c1), new Color(c2)];
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* @instance Generates a tetradic color scheme including the base color.
|
|
272
|
+
* Tetradic colors form a rectangle on the color wheel (90° apart).
|
|
273
|
+
* @returns An array of four Color instances: [base, tetrad1, tetrad2, tetrad3].
|
|
274
|
+
*/
|
|
275
|
+
getTetradColors() {
|
|
276
|
+
const [h, s, l] = _extractSolidColorValues(this.hsl);
|
|
277
|
+
const c1 = `hsl(${(h + 90) % 360}, ${s}%, ${l}%)`;
|
|
278
|
+
const c2 = `hsl(${(h + 180) % 360}, ${s}%, ${l}%)`;
|
|
279
|
+
const c3 = `hsl(${(h + 270) % 360}, ${s}%, ${l}%)`;
|
|
280
|
+
return [this, new Color(c1), new Color(c2), new Color(c3)];
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* @instance Gets the `WCAG` accessibility rating between this and another color.
|
|
284
|
+
* @param other - The other color to test contrast against.
|
|
285
|
+
* @returns 'Fail', 'AA', or 'AAA' based on `WCAG 2.1` contrast standards.
|
|
286
|
+
*/
|
|
287
|
+
getWCAGRating(other) {
|
|
288
|
+
const ratio = this.contrastRatio(other);
|
|
289
|
+
if (ratio >= 7)
|
|
290
|
+
return 'AAA';
|
|
291
|
+
if (ratio >= 4.5)
|
|
292
|
+
return 'AA';
|
|
293
|
+
return 'Fail';
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* @instance Determines if the color is light based on its perceived brightness.
|
|
297
|
+
* @returns `true` if light, `false` if dark.
|
|
298
|
+
*/
|
|
299
|
+
isLightColor() {
|
|
300
|
+
const [r, g, b] = _extractSolidColorValues(this.rgb);
|
|
301
|
+
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
|
|
302
|
+
return brightness > 127.5;
|
|
149
303
|
}
|
|
150
304
|
/**
|
|
151
305
|
* @static Checks if a color is in `Hex6` format.
|
|
@@ -166,40 +320,40 @@ export class Color {
|
|
|
166
320
|
return /^#[0-9A-Fa-f]{8}$/.test(color);
|
|
167
321
|
}
|
|
168
322
|
/**
|
|
169
|
-
* @static Checks if a color is in `RGB` format.
|
|
323
|
+
* @static Checks if a color is in `RGB` format and within valid ranges.
|
|
170
324
|
*
|
|
171
325
|
* @param color Color to check.
|
|
172
|
-
* @returns
|
|
326
|
+
* @returns `true` if it's a `RGB` color, `false` if not.
|
|
173
327
|
*/
|
|
174
328
|
static isRGB(color) {
|
|
175
|
-
return
|
|
329
|
+
return _isRGB(color);
|
|
176
330
|
}
|
|
177
331
|
/**
|
|
178
|
-
* @static Checks if a color is in `RGBA` format.
|
|
332
|
+
* @static Checks if a color is in `RGBA` format and within valid ranges.
|
|
179
333
|
*
|
|
180
334
|
* @param color Color to check.
|
|
181
|
-
* @returns
|
|
335
|
+
* @returns `true` if it's a `RGBA` color, `false` if not.
|
|
182
336
|
*/
|
|
183
337
|
static isRGBA(color) {
|
|
184
|
-
return
|
|
338
|
+
return _isRGBA(color);
|
|
185
339
|
}
|
|
186
340
|
/**
|
|
187
|
-
* @static Checks if a color is in `HSL` format.
|
|
341
|
+
* @static Checks if a color is in `HSL` format and within valid ranges.
|
|
188
342
|
*
|
|
189
343
|
* @param color Color to check.
|
|
190
|
-
* @returns
|
|
344
|
+
* @returns `true` if it's a `HSL` color, `false` if not.
|
|
191
345
|
*/
|
|
192
346
|
static isHSL(color) {
|
|
193
|
-
return
|
|
347
|
+
return _isHSL(color);
|
|
194
348
|
}
|
|
195
349
|
/**
|
|
196
|
-
* @static Checks if a color is in `HSLA` format.
|
|
350
|
+
* @static Checks if a color is in `HSLA` format and within valid ranges.
|
|
197
351
|
*
|
|
198
352
|
* @param color Color to check.
|
|
199
|
-
* @returns
|
|
353
|
+
* @returns `true` if it's a `HSLA` color, `false` if not.
|
|
200
354
|
*/
|
|
201
355
|
static isHSLA(color) {
|
|
202
|
-
return
|
|
356
|
+
return _isHSLA(color);
|
|
203
357
|
}
|
|
204
358
|
/**
|
|
205
359
|
* @private Converts the given color to all other formats while preserving the original.
|
|
@@ -234,4 +388,19 @@ export class Color {
|
|
|
234
388
|
}
|
|
235
389
|
throw new Error(`Unrecognized color format: ${color}`);
|
|
236
390
|
}
|
|
391
|
+
/**
|
|
392
|
+
* @private @static Internal factory to create a Color instance from parsed parts.
|
|
393
|
+
* @param parts All the color parts as object.
|
|
394
|
+
* @returns An instance of `Color`.
|
|
395
|
+
*/
|
|
396
|
+
static #fromParts(parts) {
|
|
397
|
+
const color = Object.create(Color.prototype);
|
|
398
|
+
color.hex = parts.hex;
|
|
399
|
+
color.hex8 = parts.hex8;
|
|
400
|
+
color.rgb = parts.rgb;
|
|
401
|
+
color.rgba = parts.rgba;
|
|
402
|
+
color.hsl = parts.hsl;
|
|
403
|
+
color.hsla = parts.hsla;
|
|
404
|
+
return color;
|
|
405
|
+
}
|
|
237
406
|
}
|
|
@@ -81,7 +81,7 @@ export const _extractAlphaColorValues = (colorString) => {
|
|
|
81
81
|
return (colorString.match(/[\d.]+%?/g) || []).map((value) => parseFloat(value));
|
|
82
82
|
};
|
|
83
83
|
/**
|
|
84
|
-
*
|
|
84
|
+
* @private Checks if a color is in `Hex` format.
|
|
85
85
|
*
|
|
86
86
|
* @param color Color to check.
|
|
87
87
|
* @returns Boolean: `true` if it's a `Hex` color, `false` if not.
|
|
@@ -90,58 +90,92 @@ export function _isHex6(color) {
|
|
|
90
90
|
return /^#[0-9A-Fa-f]{6}$/.test(color);
|
|
91
91
|
}
|
|
92
92
|
/**
|
|
93
|
-
*
|
|
93
|
+
* @private Checks if a color is in `Hex8` format.
|
|
94
94
|
*
|
|
95
95
|
* @param color Color to check.
|
|
96
|
-
* @returns Boolean: `true` if it's
|
|
96
|
+
* @returns Boolean: `true` if it's a `Hex8` color, `false` if not.
|
|
97
97
|
*/
|
|
98
|
-
export function
|
|
99
|
-
return
|
|
98
|
+
export function _isHex8(color) {
|
|
99
|
+
return /^#[0-9A-Fa-f]{8}$/.test(color);
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
102
|
+
* @private Checks if a color is in `RGB` format and within valid ranges.
|
|
103
103
|
*
|
|
104
104
|
* @param color Color to check.
|
|
105
|
-
* @returns
|
|
105
|
+
* @returns `true` if it's a `RGB` color, `false` if not.
|
|
106
106
|
*/
|
|
107
|
-
export function
|
|
108
|
-
|
|
107
|
+
export function _isRGB(color) {
|
|
108
|
+
const match = color.match(/^rgb\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)\s*\)$/);
|
|
109
|
+
if (!match)
|
|
110
|
+
return false;
|
|
111
|
+
const [r, g, b] = match.slice(1).map(Number);
|
|
112
|
+
return (_isValidRGBComponent(r) &&
|
|
113
|
+
_isValidRGBComponent(g) &&
|
|
114
|
+
_isValidRGBComponent(b));
|
|
109
115
|
}
|
|
110
116
|
/**
|
|
111
|
-
* @
|
|
112
|
-
* Checks if a color is in `Hex8` format.
|
|
117
|
+
* @private Checks if a color is in `RGBA` format and within valid ranges.
|
|
113
118
|
*
|
|
114
119
|
* @param color Color to check.
|
|
115
|
-
* @returns
|
|
120
|
+
* @returns `true` if it's a `RGBA` color, `false` if not.
|
|
116
121
|
*/
|
|
117
|
-
export function
|
|
118
|
-
|
|
122
|
+
export function _isRGBA(color) {
|
|
123
|
+
const match = color.match(/^rgba\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(0|1|0?\.\d+)\s*\)$/);
|
|
124
|
+
if (!match)
|
|
125
|
+
return false;
|
|
126
|
+
const [r, g, b, a] = match.slice(1).map(Number);
|
|
127
|
+
return (_isValidRGBComponent(r) &&
|
|
128
|
+
_isValidRGBComponent(g) &&
|
|
129
|
+
_isValidRGBComponent(b) &&
|
|
130
|
+
_isValidAlpha(a));
|
|
119
131
|
}
|
|
120
132
|
/**
|
|
121
|
-
* @
|
|
122
|
-
* Checks if a color is in `RGBA` format.
|
|
133
|
+
* @private Checks if a color is in `HSL` format and within valid ranges.
|
|
123
134
|
*
|
|
124
135
|
* @param color Color to check.
|
|
125
|
-
* @returns
|
|
136
|
+
* @returns `true` if it's a `HSL` color, `false` if not.
|
|
126
137
|
*/
|
|
127
|
-
export function
|
|
128
|
-
|
|
138
|
+
export function _isHSL(color) {
|
|
139
|
+
const match = color.match(/^hsl\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)%,\s*(\d{1,3}(?:\.\d+)?)%\s*\)$/);
|
|
140
|
+
if (!match)
|
|
141
|
+
return false;
|
|
142
|
+
const [h, s, l] = match.slice(1).map(Number);
|
|
143
|
+
return _isValidHue(h) && _isValidPercentage(s) && _isValidPercentage(l);
|
|
129
144
|
}
|
|
130
145
|
/**
|
|
131
|
-
* @
|
|
132
|
-
* Checks if a color is in `HSLA` format.
|
|
146
|
+
* @private Checks if a color is in `HSLA` format and within valid ranges.
|
|
133
147
|
*
|
|
134
148
|
* @param color Color to check.
|
|
135
|
-
* @returns
|
|
149
|
+
* @returns `true` if it's a `HSLA` color, `false` if not.
|
|
136
150
|
*/
|
|
137
151
|
export function _isHSLA(color) {
|
|
138
|
-
|
|
152
|
+
const match = color.match(/^hsla\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)%,\s*(\d{1,3}(?:\.\d+)?)%,\s*(0|1|0?\.\d+)\s*\)$/);
|
|
153
|
+
if (!match)
|
|
154
|
+
return false;
|
|
155
|
+
const [h, s, l, a] = match.slice(1).map(Number);
|
|
156
|
+
return (_isValidHue(h) &&
|
|
157
|
+
_isValidPercentage(s) &&
|
|
158
|
+
_isValidPercentage(l) &&
|
|
159
|
+
_isValidAlpha(a));
|
|
139
160
|
}
|
|
140
161
|
/**
|
|
141
|
-
*
|
|
162
|
+
* @private Checks if a number is valid alpha value.
|
|
163
|
+
*
|
|
142
164
|
* @param value Alpha value to check.
|
|
143
|
-
* @returns
|
|
165
|
+
* @returns `true` if it's a valid alpha value, `false` if not.
|
|
144
166
|
*/
|
|
145
167
|
export function _isValidAlpha(value) {
|
|
146
168
|
return value >= 0 && value <= 1 && !isNaN(value);
|
|
147
169
|
}
|
|
170
|
+
/** * @private Validates RGB component (0–255). */
|
|
171
|
+
export function _isValidRGBComponent(value) {
|
|
172
|
+
return value >= 0 && value <= 255;
|
|
173
|
+
}
|
|
174
|
+
/** * @private Validates HSL hue (0–360). */
|
|
175
|
+
export function _isValidHue(value) {
|
|
176
|
+
return value >= 0 && value <= 360;
|
|
177
|
+
}
|
|
178
|
+
/** * @private Validates HSL percentage components (0–100). */
|
|
179
|
+
export function _isValidPercentage(value) {
|
|
180
|
+
return value >= 0 && value <= 100;
|
|
181
|
+
}
|
package/package.json
CHANGED