toolbox-x 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/Color-B3mgF9Dh.d.cts +486 -0
- package/dist/Color-D38Xrw65.d.mts +486 -0
- package/dist/Stylog-Df7eq3-j.d.cts +519 -0
- package/dist/Stylog-jvlLcMQq.d.mts +519 -0
- package/dist/array-DvW0zIu6.d.mts +130 -0
- package/dist/array-rUnEVisO.d.cts +130 -0
- package/dist/basics-D_eSv0cu.cjs +132 -0
- package/dist/basics-Dp_aEK81.mjs +115 -0
- package/dist/basics-WEYWlnRO.d.cts +95 -0
- package/dist/basics-uBSfkBEI.d.mts +95 -0
- package/dist/case-BWIt8Ash.mjs +449 -0
- package/dist/case-C-S-b5YP.d.cts +327 -0
- package/dist/case-CS8Ii3A7.cjs +526 -0
- package/dist/case-CybASFPD.d.mts +327 -0
- package/dist/change-case.cjs +32 -0
- package/dist/change-case.d.cts +18 -0
- package/dist/change-case.d.mts +18 -0
- package/dist/change-case.mjs +19 -0
- package/dist/colors.cjs +574 -0
- package/dist/colors.d.cts +355 -0
- package/dist/colors.d.mts +355 -0
- package/dist/colors.mjs +547 -0
- package/dist/constants-2gAw23_7.mjs +144 -0
- package/dist/constants-B34K0QPi.d.cts +21 -0
- package/dist/constants-BIBDKY1u.cjs +924 -0
- package/dist/constants-BWT-810U.cjs +158 -0
- package/dist/constants-BwbHnXlM.mjs +662 -0
- package/dist/constants-BxN9l5el.cjs +74 -0
- package/dist/constants-CLS_bgKD.d.mts +847 -0
- package/dist/constants-D73iFu8g.mjs +171 -0
- package/dist/constants-DAfRxaa8.mjs +62 -0
- package/dist/constants-DQYeCjlx.cjs +207 -0
- package/dist/constants-Deeie-iH.d.mts +21 -0
- package/dist/constants-DpTG9RP6.d.mts +29 -0
- package/dist/constants-DqwnkJ_d.cjs +740 -0
- package/dist/constants-DvRUY_FY.cjs +150 -0
- package/dist/constants-VcRtQu0K.d.cts +29 -0
- package/dist/constants-X5hm1UtB.mjs +912 -0
- package/dist/constants-eNd-iYsV.mjs +134 -0
- package/dist/constants-qm8FafmD.d.cts +847 -0
- package/dist/constants.cjs +415 -0
- package/dist/constants.d.cts +184 -0
- package/dist/constants.d.mts +184 -0
- package/dist/constants.mjs +378 -0
- package/dist/convert-BOCgUv2D.cjs +252 -0
- package/dist/convert-Bn4jFomQ.mjs +169 -0
- package/dist/convert-BrzlG-m_.cjs +475 -0
- package/dist/convert-DhaUoPVU.mjs +368 -0
- package/dist/converter-1P90_RcP.d.mts +402 -0
- package/dist/converter-CmkcAppi.d.cts +402 -0
- package/dist/converter.cjs +780 -0
- package/dist/converter.d.cts +29 -0
- package/dist/converter.d.mts +29 -0
- package/dist/converter.mjs +771 -0
- package/dist/countries-CIpmtEzV.cjs +1469 -0
- package/dist/countries-Cy0xiqS3.mjs +1463 -0
- package/dist/css-colors-Bx947Ng3.d.cts +179 -0
- package/dist/css-colors-CXCDqQbG.cjs +186 -0
- package/dist/css-colors-CXTp1vvy.d.mts +179 -0
- package/dist/css-colors-DfUW3nTR.mjs +180 -0
- package/dist/date.cjs +332 -0
- package/dist/date.d.cts +213 -0
- package/dist/date.d.mts +213 -0
- package/dist/date.mjs +298 -0
- package/dist/dom.cjs +461 -0
- package/dist/dom.d.cts +228 -0
- package/dist/dom.d.mts +228 -0
- package/dist/dom.mjs +429 -0
- package/dist/form-BMFVGUrN.d.mts +118 -0
- package/dist/form-DRFbryvK.d.cts +118 -0
- package/dist/guards-3kaUX66g.mjs +157 -0
- package/dist/guards-C8gkvIHb.cjs +240 -0
- package/dist/guards-DdyU4h4o.mjs +110 -0
- package/dist/guards-Efhp1mNy.cjs +151 -0
- package/dist/guards.cjs +172 -0
- package/dist/guards.d.cts +399 -0
- package/dist/guards.d.mts +399 -0
- package/dist/guards.mjs +75 -0
- package/dist/hash-B6JPEyAz.d.mts +131 -0
- package/dist/hash-NTpeKYB_.d.cts +131 -0
- package/dist/hash.cjs +2126 -0
- package/dist/hash.d.cts +1239 -0
- package/dist/hash.d.mts +1239 -0
- package/dist/hash.mjs +2095 -0
- package/dist/http-status-BAZdtr7-.d.mts +65 -0
- package/dist/http-status-U_3MtoGb.d.cts +65 -0
- package/dist/http-status.cjs +173 -0
- package/dist/http-status.d.cts +142 -0
- package/dist/http-status.d.mts +142 -0
- package/dist/http-status.mjs +171 -0
- package/dist/index.cjs +2551 -0
- package/dist/index.d.cts +1493 -0
- package/dist/index.d.mts +1493 -0
- package/dist/index.mjs +2357 -0
- package/dist/object-B0TV3eHx.d.mts +8052 -0
- package/dist/object-Blq0Amdv.d.cts +8052 -0
- package/dist/objectify-CDs0Fbr1.mjs +417 -0
- package/dist/objectify-DIJ-OBmo.cjs +524 -0
- package/dist/paginator.cjs +245 -0
- package/dist/paginator.d.cts +144 -0
- package/dist/paginator.d.mts +144 -0
- package/dist/paginator.mjs +243 -0
- package/dist/parse-2ubxXZRp.cjs +211 -0
- package/dist/parse-N7g942uy.mjs +164 -0
- package/dist/pluralizer-BjMIc6uT.d.mts +42 -0
- package/dist/pluralizer-Cb6ZmrDl.d.cts +42 -0
- package/dist/pluralizer.cjs +678 -0
- package/dist/pluralizer.d.cts +152 -0
- package/dist/pluralizer.d.mts +152 -0
- package/dist/pluralizer.mjs +676 -0
- package/dist/primitives-B26uZolQ.cjs +228 -0
- package/dist/primitives-KsFUp3kQ.mjs +144 -0
- package/dist/specials-D48_IZbd.d.mts +108 -0
- package/dist/specials-DzLr1ZgU.cjs +477 -0
- package/dist/specials-LVONlKbQ.d.cts +108 -0
- package/dist/specials-uhDuRg8H.mjs +292 -0
- package/dist/string-CBAbxaG1.d.mts +258 -0
- package/dist/string-CsNsm_65.d.cts +258 -0
- package/dist/stylog.cjs +621 -0
- package/dist/stylog.d.cts +49 -0
- package/dist/stylog.d.mts +49 -0
- package/dist/stylog.mjs +614 -0
- package/dist/timezone-B2OYK6Fh.mjs +5589 -0
- package/dist/timezone-Beh9IGpw.cjs +5625 -0
- package/dist/types/array.cjs +16 -0
- package/dist/types/array.d.cts +18 -0
- package/dist/types/array.d.mts +18 -0
- package/dist/types/array.mjs +17 -0
- package/dist/types/colors.cjs +16 -0
- package/dist/types/colors.d.cts +18 -0
- package/dist/types/colors.d.mts +18 -0
- package/dist/types/colors.mjs +17 -0
- package/dist/types/converter.cjs +16 -0
- package/dist/types/converter.d.cts +18 -0
- package/dist/types/converter.d.mts +18 -0
- package/dist/types/converter.mjs +17 -0
- package/dist/types/form.cjs +16 -0
- package/dist/types/form.d.cts +18 -0
- package/dist/types/form.d.mts +18 -0
- package/dist/types/form.mjs +17 -0
- package/dist/types/hash.cjs +16 -0
- package/dist/types/hash.d.cts +18 -0
- package/dist/types/hash.d.mts +18 -0
- package/dist/types/hash.mjs +17 -0
- package/dist/types/http-status.cjs +16 -0
- package/dist/types/http-status.d.cts +18 -0
- package/dist/types/http-status.d.mts +18 -0
- package/dist/types/http-status.mjs +17 -0
- package/dist/types/index.cjs +16 -0
- package/dist/types/index.d.cts +18 -0
- package/dist/types/index.d.mts +18 -0
- package/dist/types/index.mjs +17 -0
- package/dist/types/number.cjs +16 -0
- package/dist/types/number.d.cts +18 -0
- package/dist/types/number.d.mts +18 -0
- package/dist/types/number.mjs +17 -0
- package/dist/types/object.cjs +16 -0
- package/dist/types/object.d.cts +18 -0
- package/dist/types/object.d.mts +18 -0
- package/dist/types/object.mjs +17 -0
- package/dist/types/pluralizer.cjs +16 -0
- package/dist/types/pluralizer.d.cts +18 -0
- package/dist/types/pluralizer.d.mts +18 -0
- package/dist/types/pluralizer.mjs +17 -0
- package/dist/types/string.cjs +16 -0
- package/dist/types/string.d.cts +18 -0
- package/dist/types/string.d.mts +18 -0
- package/dist/types/string.mjs +17 -0
- package/dist/types/stylog.cjs +16 -0
- package/dist/types/stylog.d.cts +18 -0
- package/dist/types/stylog.d.mts +18 -0
- package/dist/types/stylog.mjs +17 -0
- package/dist/types/utils.cjs +16 -0
- package/dist/types/utils.d.cts +18 -0
- package/dist/types/utils.d.mts +18 -0
- package/dist/types/utils.mjs +17 -0
- package/dist/types/verbalizer.cjs +16 -0
- package/dist/types/verbalizer.d.cts +30 -0
- package/dist/types/verbalizer.d.mts +30 -0
- package/dist/types/verbalizer.mjs +17 -0
- package/dist/utilities-CLUmdQeV.cjs +140 -0
- package/dist/utilities-m5yFKqLd.mjs +105 -0
- package/dist/utils-ClW9LA6f.mjs +449 -0
- package/dist/utils-DLFRgXUC.cjs +568 -0
- package/dist/verbalizer.cjs +998 -0
- package/dist/verbalizer.d.cts +148 -0
- package/dist/verbalizer.d.mts +148 -0
- package/dist/verbalizer.mjs +996 -0
- package/package.json +249 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 - present Nazmul Hassan
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { a as isNonEmptyString, c as isNumber } from "./primitives-KsFUp3kQ.mjs";
|
|
18
|
+
import { a as isHex8, c as _applyOpacity, d as _isValidAlpha, i as isHex6, n as isHSL, o as isRGB, r as isHSLA, s as isRGBA } from "./guards-3kaUX66g.mjs";
|
|
19
|
+
|
|
20
|
+
//#region src/colors/utils.ts
|
|
21
|
+
/**
|
|
22
|
+
* * Extracts numbers from a color string like `rgb(66, 103, 69)` or `hsl(120, 42.86%, 41.18%)`.
|
|
23
|
+
* * Converts percentage values to decimal (e.g., `42.86%` → `42.86`).
|
|
24
|
+
*
|
|
25
|
+
* @param color The color string in RGB or HSL format.
|
|
26
|
+
* @returns A tuple of 3 extracted numbers. `[number, number, number]`
|
|
27
|
+
*
|
|
28
|
+
* @remarks If the input color is not in `HSL` or `RGB` format, it will return `[0, 0, 0]`
|
|
29
|
+
*/
|
|
30
|
+
function extractSolidColorValues(color) {
|
|
31
|
+
if (isHSL(color) || isRGB(color)) return (color?.trim()?.match(/[\d.]+%?/g) || [])?.map((value) => parseFloat(value));
|
|
32
|
+
return [
|
|
33
|
+
0,
|
|
34
|
+
0,
|
|
35
|
+
0
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* * Extracts numbers from a color string like `rgba(66, 103, 69, 0.6)` or `hsla(120, 42.86%, 41.18%, 0.9)`.
|
|
40
|
+
* * Converts percentage values to decimal (e.g., `42.86%` → `42.86`).
|
|
41
|
+
*
|
|
42
|
+
* @param color The color string in RGBA or HSLA format.
|
|
43
|
+
* @returns A tuple of 4 extracted numbers. `[number, number, number, number]`
|
|
44
|
+
*
|
|
45
|
+
* @remarks If the input color is not in `HSLA` or `RGBA` format, it will return `[0, 0, 0, 0]`
|
|
46
|
+
*/
|
|
47
|
+
function extractAlphaColorValues(color) {
|
|
48
|
+
if (isHSLA(color) || isRGBA(color)) return (color?.trim()?.match(/[\d.]+%?/g) || [])?.map((value) => parseFloat(value));
|
|
49
|
+
return [
|
|
50
|
+
0,
|
|
51
|
+
0,
|
|
52
|
+
0,
|
|
53
|
+
0
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* * Converts percentage (0-100) to a 2-digit hex string.
|
|
58
|
+
*
|
|
59
|
+
* @param percent - The percentage (0-100) value.
|
|
60
|
+
* @returns A 2-digit hex string representing the percentage (0-100) value.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* - This function ensures that the percentage value is clamped between 0 and 100, converts it to a value between 0 and 255, and then formats it as a 2-digit hexadecimal string.
|
|
64
|
+
* - The resulting hex string is in uppercase for consistency with common hex color formats.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* percentToHex(0); // Returns "00"
|
|
68
|
+
* percentToHex(50); // Returns "7F"
|
|
69
|
+
* percentToHex(100); // Returns "FF"
|
|
70
|
+
* percentToHex(150); // Returns "FF" (ts compiler error and clamped to 100)
|
|
71
|
+
* percentToHex(-20); // Returns "00" (ts compiler error and clamped to 0)
|
|
72
|
+
*/
|
|
73
|
+
function percentToHex(percent) {
|
|
74
|
+
return Math.round(Math.min(100, Math.max(0, percent)) / 100 * 255).toString(16).padStart(2, "0").toUpperCase();
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* * Applies an opacity value to a hex color string.
|
|
78
|
+
*
|
|
79
|
+
* @param color - The hex color string in the format `#RRGGBB` or `#RRGGBBAA`.
|
|
80
|
+
* @param opacity - The opacity value as a percentage (0-100) or a 2-digit hex string.
|
|
81
|
+
* @returns The hex color string with the applied opacity in `#RRGGBBAA` format.
|
|
82
|
+
*/
|
|
83
|
+
function applyOpacityToHex(color, opacity) {
|
|
84
|
+
if (isHex6(color) || isHex8(color)) {
|
|
85
|
+
const upperColor = color.toUpperCase();
|
|
86
|
+
if (isNumber(opacity)) return _applyOpacity(upperColor, percentToHex(opacity));
|
|
87
|
+
else if (isNonEmptyString(opacity) && /^[0-9A-Fa-f]{2}$/.test(opacity)) return _applyOpacity(upperColor, opacity.toUpperCase());
|
|
88
|
+
else return _applyOpacity(upperColor, percentToHex(100));
|
|
89
|
+
} else throw new TypeError("Invalid color value!", { cause: "Value must be a hex color string in the format #RRGGBB or #RRGGBBAA." });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/colors/convert.ts
|
|
94
|
+
/**
|
|
95
|
+
* * Converts HSL to RGB color format.
|
|
96
|
+
*
|
|
97
|
+
* @param h - The hue component of the HSL color, in degrees (0 to 360).
|
|
98
|
+
* @param s - The saturation component of the HSL color, as a percentage (0 to 100).
|
|
99
|
+
* @param l - The lightness component of the HSL color, as a percentage (0 to 100).
|
|
100
|
+
* @returns A string representing the color in RGB format (e.g., `rgb(255, 0, 0)`).
|
|
101
|
+
*/
|
|
102
|
+
const convertHslToRgb = (h, s, l) => {
|
|
103
|
+
s /= 100;
|
|
104
|
+
l /= 100;
|
|
105
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
106
|
+
const x = c * (1 - Math.abs(h / 60 % 2 - 1));
|
|
107
|
+
const m = l - c / 2;
|
|
108
|
+
let r = 0, g = 0, b = 0;
|
|
109
|
+
if (h >= 0 && h < 60) [r, g] = [c, x];
|
|
110
|
+
else if (h >= 60 && h < 120) [r, g] = [x, c];
|
|
111
|
+
else if (h >= 120 && h < 180) [g, b] = [c, x];
|
|
112
|
+
else if (h >= 180 && h < 240) [g, b] = [x, c];
|
|
113
|
+
else if (h >= 240 && h < 300) [r, b] = [x, c];
|
|
114
|
+
else if (h >= 300 && h < 360) [r, b] = [c, x];
|
|
115
|
+
r = Math.round((r + m) * 255);
|
|
116
|
+
g = Math.round((g + m) * 255);
|
|
117
|
+
b = Math.round((b + m) * 255);
|
|
118
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* * Converts RGB to HSL color format.
|
|
122
|
+
*
|
|
123
|
+
* @param r - The red component of the RGB color, in the range 0 to 255.
|
|
124
|
+
* @param g - The green component of the RGB color, in the range 0 to 255.
|
|
125
|
+
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
126
|
+
* @returns A string representing the color in HSL format (e.g., `hsl(0, 100%, 50%)`).
|
|
127
|
+
*/
|
|
128
|
+
const convertRgbToHsl = (r, g, b) => {
|
|
129
|
+
r /= 255;
|
|
130
|
+
g /= 255;
|
|
131
|
+
b /= 255;
|
|
132
|
+
const max = Math.max(r, g, b);
|
|
133
|
+
const min = Math.min(r, g, b);
|
|
134
|
+
let h = 0, s = 0;
|
|
135
|
+
const l = (max + min) / 2;
|
|
136
|
+
if (max !== min) {
|
|
137
|
+
const diff = max - min;
|
|
138
|
+
s = l > .5 ? diff / (2 - max - min) : diff / (max + min);
|
|
139
|
+
switch (max) {
|
|
140
|
+
case r:
|
|
141
|
+
h = (g - b) / diff + (g < b ? 6 : 0);
|
|
142
|
+
break;
|
|
143
|
+
case g:
|
|
144
|
+
h = (b - r) / diff + 2;
|
|
145
|
+
break;
|
|
146
|
+
case b:
|
|
147
|
+
h = (r - g) / diff + 4;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
h *= 60;
|
|
151
|
+
}
|
|
152
|
+
return `hsl(${Math.round(h)}, ${Number((s * 100).toFixed(2))}%, ${Number((l * 100).toFixed(2))}%)`;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* * Converts HSL to Hex color format.
|
|
156
|
+
*
|
|
157
|
+
* @param h - The hue component of the HSL color, in degrees (0 to 360).
|
|
158
|
+
* @param s - The saturation component of the HSL color, as a percentage (0 to 100).
|
|
159
|
+
* @param l - The lightness component of the HSL color, as a percentage (0 to 100).
|
|
160
|
+
* @returns A string representing the color in Hex format (e.g., `#FF0000`).
|
|
161
|
+
*/
|
|
162
|
+
const convertHslToHex = (h, s, l) => {
|
|
163
|
+
const rgb = convertHslToRgb(h, s, l).match(/\d+/g).map(Number);
|
|
164
|
+
return convertRgbToHex(rgb[0], rgb[1], rgb[2]);
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* * Converts Hex to HSL color format.
|
|
168
|
+
*
|
|
169
|
+
* @param hex - A string representing the color in Hex format (e.g., `#FF0000`).
|
|
170
|
+
* @returns A string representing the color in HSL format (e.g., `hsl(0, 100%, 50%)`).
|
|
171
|
+
*/
|
|
172
|
+
const convertHexToHsl = (hex) => {
|
|
173
|
+
let newHex = hex?.trim()?.replace("#", "");
|
|
174
|
+
if (newHex?.length === 3) newHex = newHex?.split("")?.map((char) => char + char)?.join("");
|
|
175
|
+
return convertRgbToHsl(parseInt(newHex.slice(0, 2), 16), parseInt(newHex.slice(2, 4), 16), parseInt(newHex.slice(4, 6), 16));
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* * Converts RGB to Hex color format.
|
|
179
|
+
*
|
|
180
|
+
* @param r - The red component of the RGB color, in the range 0 to 255.
|
|
181
|
+
* @param g - The green component of the RGB color, in the range 0 to 255.
|
|
182
|
+
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
183
|
+
* @returns A string representing the color in Hex format (e.g., `#FF0000`).
|
|
184
|
+
*/
|
|
185
|
+
const convertRgbToHex = (r, g, b) => {
|
|
186
|
+
return `#${[
|
|
187
|
+
r,
|
|
188
|
+
g,
|
|
189
|
+
b
|
|
190
|
+
]?.map((v) => v.toString(16).padStart(2, "0"))?.join("")?.toUpperCase()}`;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* * Converts Hex to RGB color format.
|
|
194
|
+
*
|
|
195
|
+
* @param hex - A string representing the color in Hex format (e.g., `#FF0000`).
|
|
196
|
+
* @returns A string representing the color in RGB format (e.g., `rgb(255, 0, 0)`).
|
|
197
|
+
*/
|
|
198
|
+
const convertHexToRgb = (hex) => {
|
|
199
|
+
let newHex = hex?.trim()?.replace("#", "");
|
|
200
|
+
if (newHex?.length === 3) newHex = newHex?.split("")?.map((char) => char + char)?.join("");
|
|
201
|
+
return `rgb(${parseInt(newHex.slice(0, 2), 16)}, ${parseInt(newHex.slice(2, 4), 16)}, ${parseInt(newHex.slice(4, 6), 16)})`;
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* * Converts RGB to RGBA format, adding alpha (opacity).
|
|
205
|
+
*
|
|
206
|
+
* @param r - The red component of the RGB color, in the range 0 to 255.
|
|
207
|
+
* @param g - The green component of the RGB color, in the range 0 to 255.
|
|
208
|
+
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
209
|
+
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
210
|
+
* @returns A string representing the color in RGBA format (e.g., `rgba(255, 0, 0, 0.5)`).
|
|
211
|
+
*/
|
|
212
|
+
const convertRgbToRgba = (r, g, b, a = 1) => {
|
|
213
|
+
let newAlpha = a;
|
|
214
|
+
if (!_isValidAlpha(a)) {
|
|
215
|
+
newAlpha = 1;
|
|
216
|
+
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
217
|
+
}
|
|
218
|
+
return `rgba(${r}, ${g}, ${b}, ${parseFloat(newAlpha.toFixed(1))})`;
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* * Converts RGBA to Hex format, including alpha channel as part of Hex8.
|
|
222
|
+
*
|
|
223
|
+
* @param r - The red component of the RGB color, in the range 0 to 255.
|
|
224
|
+
* @param g - The green component of the RGB color, in the range 0 to 255.
|
|
225
|
+
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
226
|
+
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
227
|
+
* @returns A string representing the color in Hex8 format (e.g., `#FF000080`).
|
|
228
|
+
*/
|
|
229
|
+
const convertRgbaToHex8 = (r, g, b, a = 1) => {
|
|
230
|
+
let newAlpha = a;
|
|
231
|
+
if (!_isValidAlpha(a)) {
|
|
232
|
+
newAlpha = 1;
|
|
233
|
+
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
234
|
+
}
|
|
235
|
+
const alphaHex = percentToHex(Math.round(newAlpha * 100));
|
|
236
|
+
return `${convertRgbToHex(r, g, b)}${alphaHex}`;
|
|
237
|
+
};
|
|
238
|
+
/**
|
|
239
|
+
* * Converts HSLA to RGBA color format, including alpha channel.
|
|
240
|
+
*
|
|
241
|
+
* @param h - The hue component of the HSL color, in degrees (0 to 360).
|
|
242
|
+
* @param s - The saturation component of the HSL color, as a percentage (0 to 100).
|
|
243
|
+
* @param l - The lightness component of the HSL color, as a percentage (0 to 100).
|
|
244
|
+
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
245
|
+
* @returns A string representing the color in RGBA format (e.g., `rgba(255, 0, 0, 0.5)`).
|
|
246
|
+
*/
|
|
247
|
+
const convertHslaToRgba = (h, s, l, a = 1) => {
|
|
248
|
+
let newAlpha = a;
|
|
249
|
+
if (!_isValidAlpha(a)) {
|
|
250
|
+
newAlpha = 1;
|
|
251
|
+
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
252
|
+
}
|
|
253
|
+
const [r, g, b] = extractSolidColorValues(convertHslToRgb(h, s, l));
|
|
254
|
+
return convertRgbToRgba(r, g, b, parseFloat(newAlpha.toFixed(1)));
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
* * Converts RGBA to HSLA color format, including alpha channel.
|
|
258
|
+
*
|
|
259
|
+
* @param r - The red component of the RGB color, in the range 0 to 255.
|
|
260
|
+
* @param g - The green component of the RGB color, in the range 0 to 255.
|
|
261
|
+
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
262
|
+
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
263
|
+
* @returns A string representing the color in HSLA format (e.g., `hsla(0, 100%, 50%, 0.5)`).
|
|
264
|
+
*/
|
|
265
|
+
const convertRgbaToHsla = (r, g, b, a = 1) => {
|
|
266
|
+
let newAlpha = a;
|
|
267
|
+
if (!_isValidAlpha(a)) {
|
|
268
|
+
newAlpha = 1;
|
|
269
|
+
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
270
|
+
}
|
|
271
|
+
const [h, s, l] = extractSolidColorValues(convertRgbToHsl(r, g, b));
|
|
272
|
+
return `hsla(${h}, ${s}%, ${l}%, ${parseFloat(newAlpha.toFixed(1))})`;
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* * Converts Hex8 to RGBA color format, including alpha channel.
|
|
276
|
+
* - `Special Note:` Cast the parameter to `Hex8` before converting to `RGBA`.
|
|
277
|
+
* @example convertHex8ToRgba('#FFF122DE' as Hex8)
|
|
278
|
+
*
|
|
279
|
+
* @param hex8 - A string representing the color in Hex8 format (e.g., `#FF000080`).
|
|
280
|
+
* @returns A string representing the color in RGBA format (e.g., `rgba(255, 0, 0, 0.5)`).
|
|
281
|
+
*/
|
|
282
|
+
const convertHex8ToRgba = (hex8) => {
|
|
283
|
+
const hex = hex8?.trim()?.replace("#", "");
|
|
284
|
+
const r = parseInt(hex.slice(0, 2), 16);
|
|
285
|
+
const g = parseInt(hex.slice(2, 4), 16);
|
|
286
|
+
const b = parseInt(hex.slice(4, 6), 16);
|
|
287
|
+
const a = parseInt(hex.slice(6, 8), 16) / 255;
|
|
288
|
+
return `rgba(${r}, ${g}, ${b}, ${parseFloat(a.toFixed(1))})`;
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* * Converts HSLA to Hex8 color format, including alpha channel.
|
|
292
|
+
*
|
|
293
|
+
* @param h - The hue component of the HSL color, in degrees (0 to 360).
|
|
294
|
+
* @param s - The saturation component of the HSL color, as a percentage (0 to 100).
|
|
295
|
+
* @param l - The lightness component of the HSL color, as a percentage (0 to 100).
|
|
296
|
+
* @param a - The alpha (opacity) value, in the range 0 to 1.
|
|
297
|
+
* @returns A string representing the color in Hex8 format (e.g., `#658789DF`).
|
|
298
|
+
*/
|
|
299
|
+
const convertHslaToHex8 = (h, s, l, a = 1) => {
|
|
300
|
+
let newAlpha = a;
|
|
301
|
+
if (!_isValidAlpha(a)) {
|
|
302
|
+
newAlpha = 1;
|
|
303
|
+
console.warn(`Alpha value must be between 0-1, ${a} converted to 1!`);
|
|
304
|
+
}
|
|
305
|
+
const alphaHex = percentToHex(Math.round(newAlpha * 100));
|
|
306
|
+
return `${convertHslToHex(h, s, l)}${alphaHex}`;
|
|
307
|
+
};
|
|
308
|
+
/**
|
|
309
|
+
* * Converts Hex8 to HSLA color format.
|
|
310
|
+
* - `Special Note:` Cast the parameter to `Hex8` before converting to `HSLA`.
|
|
311
|
+
* @example convertHex8ToHsla('#FFF122DE' as Hex8)
|
|
312
|
+
*
|
|
313
|
+
* @param hex - A string representing the color in Hex format (e.g., `#FF0000DE`).
|
|
314
|
+
* @returns A string representing the color in HSLA format..
|
|
315
|
+
*/
|
|
316
|
+
const convertHex8ToHsla = (hex8) => {
|
|
317
|
+
return convertRgbaToHsla(...extractAlphaColorValues(convertHex8ToRgba(hex8)));
|
|
318
|
+
};
|
|
319
|
+
/**
|
|
320
|
+
* * Converts a color from `Hex`, `RGB`, or `HSL` format to its equivalent representations.
|
|
321
|
+
*
|
|
322
|
+
* @param color The color string in `Hex`, `RGB`, or `HSL` format.
|
|
323
|
+
* @returns The converted color representations excluding the input format.
|
|
324
|
+
* @throws If the color format is unrecognized throws `Error`.
|
|
325
|
+
*/
|
|
326
|
+
function convertColorCode(color) {
|
|
327
|
+
const trimmedColor = color?.trim();
|
|
328
|
+
if (isHex6(trimmedColor)) return {
|
|
329
|
+
rgb: convertHexToRgb(trimmedColor),
|
|
330
|
+
hsl: convertHexToHsl(trimmedColor)
|
|
331
|
+
};
|
|
332
|
+
if (isRGB(trimmedColor)) {
|
|
333
|
+
const rgbValues = extractSolidColorValues(trimmedColor);
|
|
334
|
+
return {
|
|
335
|
+
hex: convertRgbToHex(...rgbValues),
|
|
336
|
+
hsl: convertRgbToHsl(...rgbValues)
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
if (isHSL(trimmedColor)) {
|
|
340
|
+
const hslValues = extractSolidColorValues(trimmedColor);
|
|
341
|
+
return {
|
|
342
|
+
hex: convertHslToHex(...hslValues),
|
|
343
|
+
rgb: convertHslToRgb(...hslValues)
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
if (isHex8(trimmedColor)) return {
|
|
347
|
+
rgba: convertHex8ToRgba(trimmedColor),
|
|
348
|
+
hsla: convertHex8ToHsla(trimmedColor)
|
|
349
|
+
};
|
|
350
|
+
if (isRGBA(trimmedColor)) {
|
|
351
|
+
const rgbaValues = extractAlphaColorValues(trimmedColor);
|
|
352
|
+
return {
|
|
353
|
+
hex8: convertRgbaToHex8(...rgbaValues),
|
|
354
|
+
hsla: convertRgbaToHsla(...rgbaValues)
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
if (isHSLA(trimmedColor)) {
|
|
358
|
+
const hslaValues = extractAlphaColorValues(trimmedColor);
|
|
359
|
+
return {
|
|
360
|
+
hex8: convertHslaToHex8(...hslaValues),
|
|
361
|
+
rgba: convertHslaToRgba(...hslaValues)
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
throw new TypeError(`${color} is not a valid color!`, { cause: "Unrecognized Color Format" });
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
//#endregion
|
|
368
|
+
export { extractSolidColorValues as _, convertHexToRgb as a, convertHslaToHex8 as c, convertRgbToHsl as d, convertRgbToRgba as f, extractAlphaColorValues as g, applyOpacityToHex as h, convertHexToHsl as i, convertHslaToRgba as l, convertRgbaToHsla as m, convertHex8ToHsla as n, convertHslToHex as o, convertRgbaToHex8 as p, convertHex8ToRgba as r, convertHslToRgb as s, convertColorCode as t, convertRgbToHex as u, percentToHex as v };
|