nhb-toolbox 2.8.1 → 2.8.3
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/array/basics.js +7 -15
- package/dist/array/sort.js +1 -4
- package/dist/array/transform.js +4 -9
- package/dist/array/types.js +1 -2
- package/dist/colors/Color.js +17 -21
- package/dist/colors/constants.js +2 -5
- package/dist/colors/convert.js +56 -73
- package/dist/colors/helpers.js +13 -29
- package/dist/colors/initials.d.ts +2 -2
- package/dist/colors/initials.d.ts.map +1 -1
- package/dist/colors/initials.js +13 -16
- package/dist/colors/random.js +7 -12
- package/dist/colors/types.js +1 -2
- package/dist/form/convert.js +4 -9
- package/dist/form/guards.js +4 -10
- package/dist/form/transform.js +9 -13
- package/dist/form/types.js +1 -2
- package/dist/guards/non-primitives.d.ts +10 -10
- package/dist/guards/non-primitives.js +27 -44
- package/dist/guards/primitives.d.ts +12 -12
- package/dist/guards/primitives.js +24 -38
- package/dist/guards/specials.d.ts +12 -12
- package/dist/guards/specials.js +35 -49
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -156
- package/dist/number/basics.js +11 -21
- package/dist/number/constants.js +4 -7
- package/dist/number/convert.js +5 -8
- package/dist/number/helpers.js +11 -18
- package/dist/number/prime.js +3 -8
- package/dist/number/range.js +12 -15
- package/dist/number/types.js +1 -2
- package/dist/object/basics.js +8 -16
- package/dist/object/convert.js +1 -4
- package/dist/object/objectify.js +22 -32
- package/dist/object/sanitize.js +8 -11
- package/dist/object/types.js +1 -2
- package/dist/string/anagram.js +1 -4
- package/dist/string/basics.js +4 -11
- package/dist/string/constants.js +1 -4
- package/dist/string/convert.js +4 -9
- package/dist/string/types.js +1 -2
- package/dist/types/index.js +1 -2
- package/dist/utils/index.js +8 -15
- package/package.json +2 -1
package/dist/array/basics.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLastArrayElement = exports.shuffleArray = exports.isInvalidOrEmptyArray = exports.filterArrayOfObjects = exports.flattenArray = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* * Flattens a nested array recursively or wraps any non-array data type in an array.
|
|
6
3
|
*
|
|
@@ -8,15 +5,14 @@ exports.getLastArrayElement = exports.shuffleArray = exports.isInvalidOrEmptyArr
|
|
|
8
5
|
* @param input - The input value, which can be a nested array or a non-array value.
|
|
9
6
|
* @returns A fully flattened array of type `Flatten<T>`. If the input is not an array, it wraps it in a single-element array.
|
|
10
7
|
*/
|
|
11
|
-
const flattenArray = (input) => {
|
|
8
|
+
export const flattenArray = (input) => {
|
|
12
9
|
if (!Array.isArray(input))
|
|
13
10
|
return [input];
|
|
14
11
|
return input.reduce((acc, item) => {
|
|
15
12
|
// If item is an array, recursively flatten it; otherwise, add it directly.
|
|
16
|
-
return acc.concat(Array.isArray(item) ?
|
|
13
|
+
return acc.concat(Array.isArray(item) ? flattenArray(item) : [item]);
|
|
17
14
|
}, []);
|
|
18
15
|
};
|
|
19
|
-
exports.flattenArray = flattenArray;
|
|
20
16
|
/**
|
|
21
17
|
* * Filters an array of objects based on multiple conditions for specified keys.
|
|
22
18
|
*
|
|
@@ -27,7 +23,7 @@ exports.flattenArray = flattenArray;
|
|
|
27
23
|
* @returns The filtered array of objects.
|
|
28
24
|
* @throws {Error} If the input is not a valid array.
|
|
29
25
|
*/
|
|
30
|
-
const filterArrayOfObjects = (array, conditions) => {
|
|
26
|
+
export const filterArrayOfObjects = (array, conditions) => {
|
|
31
27
|
if (!Array.isArray(array)) {
|
|
32
28
|
throw new Error('The provided input is not a valid array!');
|
|
33
29
|
}
|
|
@@ -38,14 +34,13 @@ const filterArrayOfObjects = (array, conditions) => {
|
|
|
38
34
|
return true;
|
|
39
35
|
}));
|
|
40
36
|
};
|
|
41
|
-
exports.filterArrayOfObjects = filterArrayOfObjects;
|
|
42
37
|
/**
|
|
43
38
|
* * Checks if a value is an empty array or an array with only empty values.
|
|
44
39
|
*
|
|
45
40
|
* @param value - The value to check.
|
|
46
41
|
* @returns `true` if the value is not an array, an empty array, or an array containing only `null`, `undefined`, empty objects, or empty arrays.
|
|
47
42
|
*/
|
|
48
|
-
const isInvalidOrEmptyArray = (value) => {
|
|
43
|
+
export const isInvalidOrEmptyArray = (value) => {
|
|
49
44
|
if (!Array.isArray(value))
|
|
50
45
|
return true;
|
|
51
46
|
if (value.length === 0)
|
|
@@ -54,15 +49,14 @@ const isInvalidOrEmptyArray = (value) => {
|
|
|
54
49
|
(Array.isArray(item) && item.length === 0) || // Empty array
|
|
55
50
|
(typeof item === 'object' && Object.keys(item || {}).length === 0));
|
|
56
51
|
};
|
|
57
|
-
exports.isInvalidOrEmptyArray = isInvalidOrEmptyArray;
|
|
58
52
|
/**
|
|
59
53
|
* * Shuffle the elements of an array.
|
|
60
54
|
*
|
|
61
55
|
* @param array Array to shuffle.
|
|
62
56
|
* @returns Shuffled array.
|
|
63
57
|
*/
|
|
64
|
-
const shuffleArray = (array) => {
|
|
65
|
-
if (
|
|
58
|
+
export const shuffleArray = (array) => {
|
|
59
|
+
if (isInvalidOrEmptyArray(array))
|
|
66
60
|
return array;
|
|
67
61
|
const shuffled = structuredClone(array);
|
|
68
62
|
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
@@ -71,14 +65,12 @@ const shuffleArray = (array) => {
|
|
|
71
65
|
}
|
|
72
66
|
return shuffled;
|
|
73
67
|
};
|
|
74
|
-
exports.shuffleArray = shuffleArray;
|
|
75
68
|
/**
|
|
76
69
|
* * Get the last element of an array.
|
|
77
70
|
*
|
|
78
71
|
* @param array Array to get the last element from.
|
|
79
72
|
* @returns The last element or `undefined` if the array is empty.
|
|
80
73
|
*/
|
|
81
|
-
const getLastArrayElement = (array) => {
|
|
74
|
+
export const getLastArrayElement = (array) => {
|
|
82
75
|
return array?.length > 0 ? array[array?.length - 1] : undefined;
|
|
83
76
|
};
|
|
84
|
-
exports.getLastArrayElement = getLastArrayElement;
|
package/dist/array/sort.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sortAnArray = sortAnArray;
|
|
4
1
|
/**
|
|
5
2
|
* * Sorts an array of strings, numbers, booleans, or objects based on the provided options.
|
|
6
3
|
*
|
|
@@ -13,7 +10,7 @@ exports.sortAnArray = sortAnArray;
|
|
|
13
10
|
* @param options - Sorting options for objects.
|
|
14
11
|
* @returns The sorted array.
|
|
15
12
|
*/
|
|
16
|
-
function sortAnArray(array, options) {
|
|
13
|
+
export function sortAnArray(array, options) {
|
|
17
14
|
if (!Array.isArray(array) || array.length === 0)
|
|
18
15
|
return array;
|
|
19
16
|
// Check if the array contains strings
|
package/dist/array/transform.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createOptionsArray = void 0;
|
|
4
|
-
exports.removeDuplicatesFromArray = removeDuplicatesFromArray;
|
|
5
|
-
const utils_1 = require("../utils");
|
|
1
|
+
import { isDeepEqual } from '../utils';
|
|
6
2
|
/**
|
|
7
3
|
* * Converts an array of objects into a formatted array of options.
|
|
8
4
|
*
|
|
@@ -11,7 +7,7 @@ const utils_1 = require("../utils");
|
|
|
11
7
|
* @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
|
|
12
8
|
* @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
|
|
13
9
|
*/
|
|
14
|
-
const createOptionsArray = (data, config) => {
|
|
10
|
+
export const createOptionsArray = (data, config) => {
|
|
15
11
|
const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', } = config || {};
|
|
16
12
|
if (data && data.length) {
|
|
17
13
|
return data.map((datum) => ({
|
|
@@ -23,13 +19,12 @@ const createOptionsArray = (data, config) => {
|
|
|
23
19
|
return [];
|
|
24
20
|
}
|
|
25
21
|
};
|
|
26
|
-
exports.createOptionsArray = createOptionsArray;
|
|
27
22
|
/**
|
|
28
23
|
* * Removes duplicate values from an array, supporting deep comparison for objects and arrays.
|
|
29
24
|
*
|
|
30
25
|
* @param array - The array from which duplicates need to be removed.
|
|
31
26
|
* @returns A new array with duplicates removed.
|
|
32
27
|
*/
|
|
33
|
-
function removeDuplicatesFromArray(array) {
|
|
34
|
-
return array.filter((item, index, self) => index === self.findIndex((el) =>
|
|
28
|
+
export function removeDuplicatesFromArray(array) {
|
|
29
|
+
return array.filter((item, index, self) => index === self.findIndex((el) => isDeepEqual(el, item)));
|
|
35
30
|
}
|
package/dist/array/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/colors/Color.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const random_1 = require("./random");
|
|
7
|
-
const hsl = (0, random_1.generateRandomHSLColor)();
|
|
8
|
-
const hexRGB = (0, convert_1.convertColorCode)(hsl);
|
|
1
|
+
import { convertColorCode } from './convert';
|
|
2
|
+
import { _convertOpacityToHex, _extractAlphaColorValues, _extractSolidColorValues, } from './helpers';
|
|
3
|
+
import { generateRandomHSLColor } from './random';
|
|
4
|
+
const hsl = generateRandomHSLColor();
|
|
5
|
+
const hexRGB = convertColorCode(hsl);
|
|
9
6
|
/**
|
|
10
7
|
* * Class representing a color and its conversions between Hex, RGB, and HSL formats.
|
|
11
8
|
* * It has 1 instance method to apply opacity to `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` color.
|
|
@@ -24,7 +21,7 @@ const hexRGB = (0, convert_1.convertColorCode)(hsl);
|
|
|
24
21
|
* const randomColor = new Color(); // Generate a random color
|
|
25
22
|
* console.log(randomColor.hex, randomColor.rgb, randomColor.hsl);
|
|
26
23
|
*/
|
|
27
|
-
class Color {
|
|
24
|
+
export class Color {
|
|
28
25
|
hex;
|
|
29
26
|
rgb;
|
|
30
27
|
hsl;
|
|
@@ -82,11 +79,11 @@ class Color {
|
|
|
82
79
|
*/
|
|
83
80
|
applyOpacity(opacity) {
|
|
84
81
|
const validOpacity = Math.min(100, Math.max(0, opacity));
|
|
85
|
-
const alphaHex =
|
|
82
|
+
const alphaHex = _convertOpacityToHex(opacity);
|
|
86
83
|
const alphaDecimal = validOpacity / 100;
|
|
87
84
|
if (Color.isHex8(this.hex)) {
|
|
88
|
-
const rgbaValues =
|
|
89
|
-
const hslaValues =
|
|
85
|
+
const rgbaValues = _extractAlphaColorValues(this.rgb);
|
|
86
|
+
const hslaValues = _extractAlphaColorValues(this.hsl);
|
|
90
87
|
return {
|
|
91
88
|
hex: this.hex.slice(0, 7),
|
|
92
89
|
hex8: `${this.hex.slice(0, 7)}${alphaHex}`,
|
|
@@ -96,8 +93,8 @@ class Color {
|
|
|
96
93
|
hsla: `hsla(${hslaValues[0]}, ${hslaValues[1]}%, ${hslaValues[2]}%, ${alphaDecimal})`,
|
|
97
94
|
};
|
|
98
95
|
}
|
|
99
|
-
const rgbValues =
|
|
100
|
-
const hslValues =
|
|
96
|
+
const rgbValues = _extractSolidColorValues(this.rgb);
|
|
97
|
+
const hslValues = _extractSolidColorValues(this.hsl);
|
|
101
98
|
return {
|
|
102
99
|
hex: this.hex,
|
|
103
100
|
hex8: `${this.hex}${alphaHex}`,
|
|
@@ -176,30 +173,29 @@ class Color {
|
|
|
176
173
|
*/
|
|
177
174
|
_convertColorToOthers(color) {
|
|
178
175
|
if (Color.isHex6(color)) {
|
|
179
|
-
const { rgb, hsl } =
|
|
176
|
+
const { rgb, hsl } = convertColorCode(color);
|
|
180
177
|
return { hex: color, rgb, hsl };
|
|
181
178
|
}
|
|
182
179
|
else if (Color.isRGB(color)) {
|
|
183
|
-
const { hex, hsl } =
|
|
180
|
+
const { hex, hsl } = convertColorCode(color);
|
|
184
181
|
return { hex, rgb: color, hsl };
|
|
185
182
|
}
|
|
186
183
|
else if (Color.isHSL(color)) {
|
|
187
|
-
const { hex, rgb } =
|
|
184
|
+
const { hex, rgb } = convertColorCode(color);
|
|
188
185
|
return { hex, rgb, hsl: color };
|
|
189
186
|
}
|
|
190
187
|
else if (Color.isHex8(color)) {
|
|
191
|
-
const { rgba, hsla } =
|
|
188
|
+
const { rgba, hsla } = convertColorCode(color);
|
|
192
189
|
return { hex8: color, rgba, hsla };
|
|
193
190
|
}
|
|
194
191
|
else if (Color.isRGBA(color)) {
|
|
195
|
-
const { hex8, hsla } =
|
|
192
|
+
const { hex8, hsla } = convertColorCode(color);
|
|
196
193
|
return { hex8, rgba: color, hsla };
|
|
197
194
|
}
|
|
198
195
|
else if (Color.isHSLA(color)) {
|
|
199
|
-
const { hex8, rgba } =
|
|
196
|
+
const { hex8, rgba } = convertColorCode(color);
|
|
200
197
|
return { hex8, rgba, hsla: color };
|
|
201
198
|
}
|
|
202
199
|
throw new Error(`Unrecognized Color Format! ${color}`);
|
|
203
200
|
}
|
|
204
201
|
}
|
|
205
|
-
exports.Color = Color;
|
package/dist/colors/constants.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.numberColorPalette = exports.alphabetColorPalette = void 0;
|
|
4
1
|
/** Colors based on the ASCII value of the letter. */
|
|
5
|
-
|
|
2
|
+
export const alphabetColorPalette = [
|
|
6
3
|
'#00094C',
|
|
7
4
|
'#00376E',
|
|
8
5
|
'#005600',
|
|
@@ -31,7 +28,7 @@ exports.alphabetColorPalette = [
|
|
|
31
28
|
'#824809',
|
|
32
29
|
];
|
|
33
30
|
/** Colors based on the index of numbers. */
|
|
34
|
-
|
|
31
|
+
export const numberColorPalette = [
|
|
35
32
|
'#893213',
|
|
36
33
|
'#A44C15',
|
|
37
34
|
'#8B4513',
|
package/dist/colors/convert.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertHex8ToHsla = exports.convertHslaToHex8 = exports.convertHex8ToRgba = exports.convertRgbaToHsla = exports.convertHslaToRgba = exports.convertRgbaToHex8 = exports.convertRgbToRgba = exports.convertHexToRgb = exports.convertRgbToHex = exports.convertHexToHsl = exports.convertHslToHex = exports.convertRgbToHsl = exports.convertHslToRgb = void 0;
|
|
4
|
-
exports.convertColorCode = convertColorCode;
|
|
5
|
-
const helpers_1 = require("./helpers");
|
|
1
|
+
import { _convertOpacityToHex, _extractAlphaColorValues, _extractSolidColorValues, _isHex6, _isHex8, _isHSL, _isHSLA, _isRGB, _isRGBA, _isValidAlpha, } from './helpers';
|
|
6
2
|
/**
|
|
7
3
|
* * Converts HSL to RGB color format.
|
|
8
4
|
*
|
|
@@ -11,7 +7,7 @@ const helpers_1 = require("./helpers");
|
|
|
11
7
|
* @param l - The lightness component of the HSL color, as a percentage (0 to 100).
|
|
12
8
|
* @returns A string representing the color in RGB format (e.g., `rgb(255, 0, 0)`).
|
|
13
9
|
*/
|
|
14
|
-
const convertHslToRgb = (h, s, l) => {
|
|
10
|
+
export const convertHslToRgb = (h, s, l) => {
|
|
15
11
|
// Normalize the HSL values
|
|
16
12
|
s /= 100;
|
|
17
13
|
l /= 100;
|
|
@@ -36,7 +32,6 @@ const convertHslToRgb = (h, s, l) => {
|
|
|
36
32
|
b = Math.round((b + m) * 255);
|
|
37
33
|
return `rgb(${r}, ${g}, ${b})`;
|
|
38
34
|
};
|
|
39
|
-
exports.convertHslToRgb = convertHslToRgb;
|
|
40
35
|
/**
|
|
41
36
|
* * Converts RGB to HSL color format.
|
|
42
37
|
*
|
|
@@ -45,7 +40,7 @@ exports.convertHslToRgb = convertHslToRgb;
|
|
|
45
40
|
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
46
41
|
* @returns A string representing the color in HSL format (e.g., `hsl(0, 100%, 50%)`).
|
|
47
42
|
*/
|
|
48
|
-
const convertRgbToHsl = (r, g, b) => {
|
|
43
|
+
export const convertRgbToHsl = (r, g, b) => {
|
|
49
44
|
r /= 255;
|
|
50
45
|
g /= 255;
|
|
51
46
|
b /= 255;
|
|
@@ -71,7 +66,6 @@ const convertRgbToHsl = (r, g, b) => {
|
|
|
71
66
|
}
|
|
72
67
|
return `hsl(${Math.round(h)}, ${Number((s * 100).toFixed(2))}%, ${Number((l * 100).toFixed(2))}%)`;
|
|
73
68
|
};
|
|
74
|
-
exports.convertRgbToHsl = convertRgbToHsl;
|
|
75
69
|
/**
|
|
76
70
|
* * Converts HSL to Hex color format.
|
|
77
71
|
*
|
|
@@ -80,18 +74,17 @@ exports.convertRgbToHsl = convertRgbToHsl;
|
|
|
80
74
|
* @param l - The lightness component of the HSL color, as a percentage (0 to 100).
|
|
81
75
|
* @returns A string representing the color in Hex format (e.g., `#FF0000`).
|
|
82
76
|
*/
|
|
83
|
-
const convertHslToHex = (h, s, l) => {
|
|
84
|
-
const rgb =
|
|
85
|
-
return
|
|
77
|
+
export const convertHslToHex = (h, s, l) => {
|
|
78
|
+
const rgb = convertHslToRgb(h, s, l).match(/\d+/g).map(Number);
|
|
79
|
+
return convertRgbToHex(rgb[0], rgb[1], rgb[2]);
|
|
86
80
|
};
|
|
87
|
-
exports.convertHslToHex = convertHslToHex;
|
|
88
81
|
/**
|
|
89
82
|
* * Converts Hex to HSL color format.
|
|
90
83
|
*
|
|
91
84
|
* @param hex - A string representing the color in Hex format (e.g., `#FF0000`).
|
|
92
85
|
* @returns A string representing the color in HSL format (e.g., `hsl(0, 100%, 50%)`).
|
|
93
86
|
*/
|
|
94
|
-
const convertHexToHsl = (hex) => {
|
|
87
|
+
export const convertHexToHsl = (hex) => {
|
|
95
88
|
let newHex = hex.replace('#', '');
|
|
96
89
|
if (newHex.length === 3) {
|
|
97
90
|
newHex = newHex
|
|
@@ -102,9 +95,8 @@ const convertHexToHsl = (hex) => {
|
|
|
102
95
|
const r = parseInt(newHex.slice(0, 2), 16);
|
|
103
96
|
const g = parseInt(newHex.slice(2, 4), 16);
|
|
104
97
|
const b = parseInt(newHex.slice(4, 6), 16);
|
|
105
|
-
return
|
|
98
|
+
return convertRgbToHsl(r, g, b);
|
|
106
99
|
};
|
|
107
|
-
exports.convertHexToHsl = convertHexToHsl;
|
|
108
100
|
/**
|
|
109
101
|
* * Converts RGB to Hex color format.
|
|
110
102
|
*
|
|
@@ -113,21 +105,20 @@ exports.convertHexToHsl = convertHexToHsl;
|
|
|
113
105
|
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
114
106
|
* @returns A string representing the color in Hex format (e.g., `#FF0000`).
|
|
115
107
|
*/
|
|
116
|
-
const convertRgbToHex = (r, g, b) => {
|
|
108
|
+
export const convertRgbToHex = (r, g, b) => {
|
|
117
109
|
const hex = [r, g, b]
|
|
118
110
|
.map((v) => v.toString(16).padStart(2, '0'))
|
|
119
111
|
.join('')
|
|
120
112
|
.toUpperCase();
|
|
121
113
|
return `#${hex}`;
|
|
122
114
|
};
|
|
123
|
-
exports.convertRgbToHex = convertRgbToHex;
|
|
124
115
|
/**
|
|
125
116
|
* * Converts Hex to RGB color format.
|
|
126
117
|
*
|
|
127
118
|
* @param hex - A string representing the color in Hex format (e.g., `#FF0000`).
|
|
128
119
|
* @returns A string representing the color in RGB format (e.g., `rgb(255, 0, 0)`).
|
|
129
120
|
*/
|
|
130
|
-
const convertHexToRgb = (hex) => {
|
|
121
|
+
export const convertHexToRgb = (hex) => {
|
|
131
122
|
// Remove the # if present
|
|
132
123
|
let newHex = hex.replace('#', '');
|
|
133
124
|
if (newHex.length === 3) {
|
|
@@ -141,7 +132,6 @@ const convertHexToRgb = (hex) => {
|
|
|
141
132
|
const b = parseInt(newHex.slice(4, 6), 16);
|
|
142
133
|
return `rgb(${r}, ${g}, ${b})`;
|
|
143
134
|
};
|
|
144
|
-
exports.convertHexToRgb = convertHexToRgb;
|
|
145
135
|
/**
|
|
146
136
|
* * Converts RGB to RGBA format, adding alpha (opacity).
|
|
147
137
|
*
|
|
@@ -151,15 +141,14 @@ exports.convertHexToRgb = convertHexToRgb;
|
|
|
151
141
|
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
152
142
|
* @returns A string representing the color in RGBA format (e.g., `rgba(255, 0, 0, 0.5)`).
|
|
153
143
|
*/
|
|
154
|
-
const convertRgbToRgba = (r, g, b, a = 1) => {
|
|
144
|
+
export const convertRgbToRgba = (r, g, b, a = 1) => {
|
|
155
145
|
let newAlpha = a;
|
|
156
|
-
if (!
|
|
146
|
+
if (!_isValidAlpha(a)) {
|
|
157
147
|
newAlpha = 1;
|
|
158
148
|
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
159
149
|
}
|
|
160
150
|
return `rgba(${r}, ${g}, ${b}, ${parseFloat(newAlpha.toFixed(1))})`;
|
|
161
151
|
};
|
|
162
|
-
exports.convertRgbToRgba = convertRgbToRgba;
|
|
163
152
|
/**
|
|
164
153
|
* * Converts RGBA to Hex format, including alpha channel as part of Hex8.
|
|
165
154
|
*
|
|
@@ -169,17 +158,16 @@ exports.convertRgbToRgba = convertRgbToRgba;
|
|
|
169
158
|
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
170
159
|
* @returns A string representing the color in Hex8 format (e.g., `#FF000080`).
|
|
171
160
|
*/
|
|
172
|
-
const convertRgbaToHex8 = (r, g, b, a = 1) => {
|
|
161
|
+
export const convertRgbaToHex8 = (r, g, b, a = 1) => {
|
|
173
162
|
let newAlpha = a;
|
|
174
|
-
if (!
|
|
163
|
+
if (!_isValidAlpha(a)) {
|
|
175
164
|
newAlpha = 1;
|
|
176
165
|
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
177
166
|
}
|
|
178
|
-
const hex =
|
|
179
|
-
const alphaHex =
|
|
167
|
+
const hex = convertRgbToHex(r, g, b);
|
|
168
|
+
const alphaHex = _convertOpacityToHex(Math.round(newAlpha * 100));
|
|
180
169
|
return `#${hex}${alphaHex}`;
|
|
181
170
|
};
|
|
182
|
-
exports.convertRgbaToHex8 = convertRgbaToHex8;
|
|
183
171
|
/**
|
|
184
172
|
* * Converts HSLA to RGBA color format, including alpha channel.
|
|
185
173
|
*
|
|
@@ -189,17 +177,16 @@ exports.convertRgbaToHex8 = convertRgbaToHex8;
|
|
|
189
177
|
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
190
178
|
* @returns A string representing the color in RGBA format (e.g., `rgba(255, 0, 0, 0.5)`).
|
|
191
179
|
*/
|
|
192
|
-
const convertHslaToRgba = (h, s, l, a = 1) => {
|
|
180
|
+
export const convertHslaToRgba = (h, s, l, a = 1) => {
|
|
193
181
|
let newAlpha = a;
|
|
194
|
-
if (!
|
|
182
|
+
if (!_isValidAlpha(a)) {
|
|
195
183
|
newAlpha = 1;
|
|
196
184
|
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
197
185
|
}
|
|
198
|
-
const rgb =
|
|
199
|
-
const rgbNumbers =
|
|
200
|
-
return
|
|
186
|
+
const rgb = convertHslToRgb(h, s, l);
|
|
187
|
+
const rgbNumbers = _extractSolidColorValues(rgb);
|
|
188
|
+
return convertRgbToRgba(rgbNumbers[0], rgbNumbers[1], rgbNumbers[2], parseFloat(newAlpha.toFixed(1)));
|
|
201
189
|
};
|
|
202
|
-
exports.convertHslaToRgba = convertHslaToRgba;
|
|
203
190
|
/**
|
|
204
191
|
* * Converts RGBA to HSLA color format, including alpha channel.
|
|
205
192
|
*
|
|
@@ -209,24 +196,23 @@ exports.convertHslaToRgba = convertHslaToRgba;
|
|
|
209
196
|
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
210
197
|
* @returns A string representing the color in HSLA format (e.g., `hsla(0, 100%, 50%, 0.5)`).
|
|
211
198
|
*/
|
|
212
|
-
const convertRgbaToHsla = (r, g, b, a = 1) => {
|
|
199
|
+
export const convertRgbaToHsla = (r, g, b, a = 1) => {
|
|
213
200
|
let newAlpha = a;
|
|
214
|
-
if (!
|
|
201
|
+
if (!_isValidAlpha(a)) {
|
|
215
202
|
newAlpha = 1;
|
|
216
203
|
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
217
204
|
}
|
|
218
|
-
const hsl =
|
|
219
|
-
const hslNumbers =
|
|
205
|
+
const hsl = convertRgbToHsl(r, g, b);
|
|
206
|
+
const hslNumbers = _extractSolidColorValues(hsl);
|
|
220
207
|
return `hsla(${hslNumbers[0]}, ${hslNumbers[1]}%, ${hslNumbers[2]}%, ${parseFloat(newAlpha.toFixed(1))})`;
|
|
221
208
|
};
|
|
222
|
-
exports.convertRgbaToHsla = convertRgbaToHsla;
|
|
223
209
|
/**
|
|
224
210
|
* * Converts Hex8 to RGBA color format, including alpha channel.
|
|
225
211
|
*
|
|
226
212
|
* @param hex8 - A string representing the color in Hex8 format (e.g., `#FF000080`).
|
|
227
213
|
* @returns A string representing the color in RGBA format (e.g., `rgba(255, 0, 0, 0.5)`).
|
|
228
214
|
*/
|
|
229
|
-
const convertHex8ToRgba = (hex8) => {
|
|
215
|
+
export const convertHex8ToRgba = (hex8) => {
|
|
230
216
|
const hex = hex8.replace('#', '');
|
|
231
217
|
const r = parseInt(hex.slice(0, 2), 16);
|
|
232
218
|
const g = parseInt(hex.slice(2, 4), 16);
|
|
@@ -234,7 +220,6 @@ const convertHex8ToRgba = (hex8) => {
|
|
|
234
220
|
const a = parseInt(hex.slice(6, 8), 16) / 255;
|
|
235
221
|
return `rgba(${r}, ${g}, ${b}, ${parseFloat(a.toFixed(1))})`;
|
|
236
222
|
};
|
|
237
|
-
exports.convertHex8ToRgba = convertHex8ToRgba;
|
|
238
223
|
/**
|
|
239
224
|
* * Converts HSLA to Hex8 color format, including alpha channel.
|
|
240
225
|
*
|
|
@@ -244,28 +229,26 @@ exports.convertHex8ToRgba = convertHex8ToRgba;
|
|
|
244
229
|
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
245
230
|
* @returns A string representing the color in Hex8 format (e.g., `#658789DF`).
|
|
246
231
|
*/
|
|
247
|
-
const convertHslaToHex8 = (h, s, l, a = 1) => {
|
|
232
|
+
export const convertHslaToHex8 = (h, s, l, a = 1) => {
|
|
248
233
|
let newAlpha = a;
|
|
249
|
-
if (!
|
|
234
|
+
if (!_isValidAlpha(a)) {
|
|
250
235
|
newAlpha = 1;
|
|
251
236
|
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
252
237
|
}
|
|
253
|
-
const hex =
|
|
254
|
-
const alphaHex =
|
|
238
|
+
const hex = convertHslToHex(h, s, l);
|
|
239
|
+
const alphaHex = _convertOpacityToHex(Math.round(newAlpha * 100));
|
|
255
240
|
return `#${hex}${alphaHex}`;
|
|
256
241
|
};
|
|
257
|
-
exports.convertHslaToHex8 = convertHslaToHex8;
|
|
258
242
|
/**
|
|
259
243
|
* * Converts Hex8 to HSLA color format.
|
|
260
244
|
*
|
|
261
245
|
* @param hex - A string representing the color in Hex format (e.g., `#FF0000DE`).
|
|
262
246
|
* @returns A string representing the color in HSLA format..
|
|
263
247
|
*/
|
|
264
|
-
const convertHex8ToHsla = (hex8) => {
|
|
265
|
-
const rgba =
|
|
266
|
-
return
|
|
248
|
+
export const convertHex8ToHsla = (hex8) => {
|
|
249
|
+
const rgba = convertHex8ToRgba(hex8);
|
|
250
|
+
return convertRgbaToHsla(..._extractAlphaColorValues(rgba));
|
|
267
251
|
};
|
|
268
|
-
exports.convertHex8ToHsla = convertHex8ToHsla;
|
|
269
252
|
/**
|
|
270
253
|
* * Converts a color from `Hex`, `RGB`, or `HSL` format to its equivalent representations.
|
|
271
254
|
*
|
|
@@ -273,45 +256,45 @@ exports.convertHex8ToHsla = convertHex8ToHsla;
|
|
|
273
256
|
* @returns The converted color representations excluding the input format.
|
|
274
257
|
* @throws If the color format is unrecognized throws `Error`.
|
|
275
258
|
*/
|
|
276
|
-
function convertColorCode(color) {
|
|
277
|
-
if (
|
|
259
|
+
export function convertColorCode(color) {
|
|
260
|
+
if (_isHex6(color)) {
|
|
278
261
|
return {
|
|
279
|
-
rgb:
|
|
280
|
-
hsl:
|
|
262
|
+
rgb: convertHexToRgb(color),
|
|
263
|
+
hsl: convertHexToHsl(color),
|
|
281
264
|
};
|
|
282
265
|
}
|
|
283
|
-
if (
|
|
284
|
-
const rgbValues =
|
|
266
|
+
if (_isRGB(color)) {
|
|
267
|
+
const rgbValues = _extractSolidColorValues(color);
|
|
285
268
|
return {
|
|
286
|
-
hex:
|
|
287
|
-
hsl:
|
|
269
|
+
hex: convertRgbToHex(...rgbValues),
|
|
270
|
+
hsl: convertRgbToHsl(...rgbValues),
|
|
288
271
|
};
|
|
289
272
|
}
|
|
290
|
-
if (
|
|
291
|
-
const hslValues =
|
|
273
|
+
if (_isHSL(color)) {
|
|
274
|
+
const hslValues = _extractSolidColorValues(color);
|
|
292
275
|
return {
|
|
293
|
-
hex:
|
|
294
|
-
rgb:
|
|
276
|
+
hex: convertHslToHex(...hslValues),
|
|
277
|
+
rgb: convertHslToRgb(...hslValues),
|
|
295
278
|
};
|
|
296
279
|
}
|
|
297
|
-
if (
|
|
280
|
+
if (_isHex8(color)) {
|
|
298
281
|
return {
|
|
299
|
-
rgba:
|
|
300
|
-
hsla:
|
|
282
|
+
rgba: convertHex8ToRgba(color),
|
|
283
|
+
hsla: convertHex8ToHsla(color),
|
|
301
284
|
};
|
|
302
285
|
}
|
|
303
|
-
if (
|
|
304
|
-
const rgbaValues =
|
|
286
|
+
if (_isRGBA(color)) {
|
|
287
|
+
const rgbaValues = _extractAlphaColorValues(color);
|
|
305
288
|
return {
|
|
306
|
-
hex8:
|
|
307
|
-
hsla:
|
|
289
|
+
hex8: convertRgbaToHex8(...rgbaValues),
|
|
290
|
+
hsla: convertRgbaToHsla(...rgbaValues),
|
|
308
291
|
};
|
|
309
292
|
}
|
|
310
|
-
if (
|
|
311
|
-
const hslaValues =
|
|
293
|
+
if (_isHSLA(color)) {
|
|
294
|
+
const hslaValues = _extractAlphaColorValues(color);
|
|
312
295
|
return {
|
|
313
|
-
hex8:
|
|
314
|
-
rgba:
|
|
296
|
+
hex8: convertHslaToHex8(...hslaValues),
|
|
297
|
+
rgba: convertHslaToRgba(...hslaValues),
|
|
315
298
|
};
|
|
316
299
|
}
|
|
317
300
|
throw new Error(`Unrecognized Color Format! ${color}`);
|