@hairy/palette 0.2.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/CHANGELOG.md +13 -0
- package/LICENSE +23 -0
- package/index.cjs.js +217 -0
- package/index.cjs.js.map +7 -0
- package/index.d.ts +107 -0
- package/index.esm.js +184 -0
- package/index.esm.js.map +7 -0
- package/index.iife.js +206 -0
- package/index.iife.js.map +7 -0
- package/index.iife.min.js +2 -0
- package/index.iife.min.js.map +7 -0
- package/index.md +82 -0
- package/package.json +22 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019-PRESENT Anthony Fu<https://github.com/TuiMao233>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// index.ts
|
|
20
|
+
var util_color_exports = {};
|
|
21
|
+
__export(util_color_exports, {
|
|
22
|
+
blend: () => blend,
|
|
23
|
+
brightness: () => brightness,
|
|
24
|
+
changeAlpha: () => changeAlpha,
|
|
25
|
+
hexToRgb: () => hexToRgb,
|
|
26
|
+
hsvToRgb: () => hsvToRgb,
|
|
27
|
+
lighten: () => lighten,
|
|
28
|
+
luminosity: () => luminosity,
|
|
29
|
+
rgbToHex: () => rgbToHex,
|
|
30
|
+
rgbToHsv: () => rgbToHsv,
|
|
31
|
+
textToRgb: () => textToRgb
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(util_color_exports);
|
|
34
|
+
var reRGBA = /^rgb(a)?\((\d{1,3}),(\d{1,3}),(\d{1,3}),?([01]?\.?\d*?)?\)$/;
|
|
35
|
+
function rgbToHex({ r, g, b, a }) {
|
|
36
|
+
const alpha = a !== void 0;
|
|
37
|
+
r = Math.round(r);
|
|
38
|
+
g = Math.round(g);
|
|
39
|
+
b = Math.round(b);
|
|
40
|
+
if (r > 255 || g > 255 || b > 255 || alpha && a > 100) {
|
|
41
|
+
throw new TypeError("Expected 3 numbers below 256 (and optionally one below 100)");
|
|
42
|
+
}
|
|
43
|
+
if (alpha) {
|
|
44
|
+
a = Number((Math.round(255 * a / 100) | 1 << 8).toString(16).slice(1));
|
|
45
|
+
}
|
|
46
|
+
return "#" + (b | g << 8 | r << 16 | 1 << 24).toString(16).slice(1) + a;
|
|
47
|
+
}
|
|
48
|
+
function hexToRgb(hex) {
|
|
49
|
+
if (typeof hex !== "string") {
|
|
50
|
+
throw new TypeError("Expected a string");
|
|
51
|
+
}
|
|
52
|
+
hex = hex.replace(/^#/, "");
|
|
53
|
+
if (hex.length === 3) {
|
|
54
|
+
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
|
|
55
|
+
} else if (hex.length === 4) {
|
|
56
|
+
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
|
|
57
|
+
}
|
|
58
|
+
const num = parseInt(hex, 16);
|
|
59
|
+
return hex.length > 6 ? { r: num >> 24 & 255, g: num >> 16 & 255, b: num >> 8 & 255, a: Math.round((num & 255) / 2.55) } : { r: num >> 16, g: num >> 8 & 255, b: num & 255 };
|
|
60
|
+
}
|
|
61
|
+
function hsvToRgb({ h, s, v, a }) {
|
|
62
|
+
let r, g, b;
|
|
63
|
+
s = s / 100;
|
|
64
|
+
v = v / 100;
|
|
65
|
+
h = h / 360;
|
|
66
|
+
const i = Math.floor(h * 6), f = h * 6 - i, p = v * (1 - s), q = v * (1 - f * s), t = v * (1 - (1 - f) * s);
|
|
67
|
+
switch (i % 6) {
|
|
68
|
+
case 0:
|
|
69
|
+
r = v;
|
|
70
|
+
g = t;
|
|
71
|
+
b = p;
|
|
72
|
+
break;
|
|
73
|
+
case 1:
|
|
74
|
+
r = q;
|
|
75
|
+
g = v;
|
|
76
|
+
b = p;
|
|
77
|
+
break;
|
|
78
|
+
case 2:
|
|
79
|
+
r = p;
|
|
80
|
+
g = v;
|
|
81
|
+
b = t;
|
|
82
|
+
break;
|
|
83
|
+
case 3:
|
|
84
|
+
r = p;
|
|
85
|
+
g = q;
|
|
86
|
+
b = v;
|
|
87
|
+
break;
|
|
88
|
+
case 4:
|
|
89
|
+
r = t;
|
|
90
|
+
g = p;
|
|
91
|
+
b = v;
|
|
92
|
+
break;
|
|
93
|
+
case 5:
|
|
94
|
+
r = v;
|
|
95
|
+
g = p;
|
|
96
|
+
b = q;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
r: Math.round(r * 255),
|
|
101
|
+
g: Math.round(g * 255),
|
|
102
|
+
b: Math.round(b * 255),
|
|
103
|
+
a
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function rgbToHsv({ r, g, b, a }) {
|
|
107
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b), d = max - min, s = max === 0 ? 0 : d / max, v = max / 255;
|
|
108
|
+
let h;
|
|
109
|
+
switch (max) {
|
|
110
|
+
case min:
|
|
111
|
+
h = 0;
|
|
112
|
+
break;
|
|
113
|
+
case r:
|
|
114
|
+
h = g - b + d * (g < b ? 6 : 0);
|
|
115
|
+
h /= 6 * d;
|
|
116
|
+
break;
|
|
117
|
+
case g:
|
|
118
|
+
h = b - r + d * 2;
|
|
119
|
+
h /= 6 * d;
|
|
120
|
+
break;
|
|
121
|
+
case b:
|
|
122
|
+
h = r - g + d * 4;
|
|
123
|
+
h /= 6 * d;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
h: Math.round(h * 360),
|
|
128
|
+
s: Math.round(s * 100),
|
|
129
|
+
v: Math.round(v * 100),
|
|
130
|
+
a
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function textToRgb(str) {
|
|
134
|
+
if (typeof str !== "string") {
|
|
135
|
+
throw new TypeError("Expected a string");
|
|
136
|
+
}
|
|
137
|
+
const color = str.replace(/ /g, "");
|
|
138
|
+
const m = reRGBA.exec(color);
|
|
139
|
+
if (m === null) {
|
|
140
|
+
return hexToRgb(color);
|
|
141
|
+
}
|
|
142
|
+
const rgb = {
|
|
143
|
+
r: Math.min(255, parseInt(m[2], 10)),
|
|
144
|
+
g: Math.min(255, parseInt(m[3], 10)),
|
|
145
|
+
b: Math.min(255, parseInt(m[4], 10))
|
|
146
|
+
};
|
|
147
|
+
if (m[1]) {
|
|
148
|
+
const alpha = parseFloat(m[5]);
|
|
149
|
+
rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100;
|
|
150
|
+
}
|
|
151
|
+
return rgb;
|
|
152
|
+
}
|
|
153
|
+
function lighten(color, percent) {
|
|
154
|
+
if (typeof color !== "string") {
|
|
155
|
+
throw new TypeError("Expected a string as color");
|
|
156
|
+
}
|
|
157
|
+
if (typeof percent !== "number") {
|
|
158
|
+
throw new TypeError("Expected a numeric percent");
|
|
159
|
+
}
|
|
160
|
+
const rgb = textToRgb(color), t = percent < 0 ? 0 : 255, p = Math.abs(percent) / 100, R = rgb.r, G = rgb.g, B = rgb.b;
|
|
161
|
+
return "#" + (16777216 + (Math.round((t - R) * p) + R) * 65536 + (Math.round((t - G) * p) + G) * 256 + (Math.round((t - B) * p) + B)).toString(16).slice(1);
|
|
162
|
+
}
|
|
163
|
+
function luminosity(color) {
|
|
164
|
+
if (typeof color !== "string" && (!color || color.r === void 0)) {
|
|
165
|
+
throw new TypeError("Expected a string or a {r, g, b} object as color");
|
|
166
|
+
}
|
|
167
|
+
const rgb = typeof color === "string" ? textToRgb(color) : color, r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255, R = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4), G = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4), B = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
|
|
168
|
+
return 0.2126 * R + 0.7152 * G + 0.0722 * B;
|
|
169
|
+
}
|
|
170
|
+
function brightness(color) {
|
|
171
|
+
if (typeof color !== "string" && (!color || color.r === void 0)) {
|
|
172
|
+
throw new TypeError("Expected a string or a {r, g, b} object as color");
|
|
173
|
+
}
|
|
174
|
+
const rgb = typeof color === "string" ? textToRgb(color) : color;
|
|
175
|
+
return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1e3;
|
|
176
|
+
}
|
|
177
|
+
function blend(fgColor, bgColor) {
|
|
178
|
+
if (typeof fgColor !== "string" && (!fgColor || fgColor.r === void 0)) {
|
|
179
|
+
throw new TypeError("Expected a string or a {r, g, b[, a]} object as fgColor");
|
|
180
|
+
}
|
|
181
|
+
if (typeof bgColor !== "string" && (!bgColor || bgColor.r === void 0)) {
|
|
182
|
+
throw new TypeError("Expected a string or a {r, g, b[, a]} object as bgColor");
|
|
183
|
+
}
|
|
184
|
+
const rgb1 = typeof fgColor === "string" ? textToRgb(fgColor) : fgColor, r1 = rgb1.r / 255, g1 = rgb1.g / 255, b1 = rgb1.b / 255, a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1, rgb2 = typeof bgColor === "string" ? textToRgb(bgColor) : bgColor, r2 = rgb2.r / 255, g2 = rgb2.g / 255, b2 = rgb2.b / 255, a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1, a = a1 + a2 * (1 - a1), r = Math.round((r1 * a1 + r2 * a2 * (1 - a1)) / a * 255), g = Math.round((g1 * a1 + g2 * a2 * (1 - a1)) / a * 255), b = Math.round((b1 * a1 + b2 * a2 * (1 - a1)) / a * 255);
|
|
185
|
+
const ret = { r, g, b, a: Math.round(a * 100) };
|
|
186
|
+
return typeof fgColor === "string" ? rgbToHex(ret) : ret;
|
|
187
|
+
}
|
|
188
|
+
function changeAlpha(color, offset) {
|
|
189
|
+
if (typeof color !== "string") {
|
|
190
|
+
throw new TypeError("Expected a string as color");
|
|
191
|
+
}
|
|
192
|
+
if (offset === void 0 || offset < -1 || offset > 1) {
|
|
193
|
+
throw new TypeError("Expected offset to be between -1 and 1");
|
|
194
|
+
}
|
|
195
|
+
const { r, g, b, a } = textToRgb(color);
|
|
196
|
+
const alpha = a !== void 0 ? a / 100 : 0;
|
|
197
|
+
return rgbToHex({
|
|
198
|
+
r,
|
|
199
|
+
g,
|
|
200
|
+
b,
|
|
201
|
+
a: Math.round(Math.min(1, Math.max(0, alpha + offset)) * 100)
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
205
|
+
0 && (module.exports = {
|
|
206
|
+
blend,
|
|
207
|
+
brightness,
|
|
208
|
+
changeAlpha,
|
|
209
|
+
hexToRgb,
|
|
210
|
+
hsvToRgb,
|
|
211
|
+
lighten,
|
|
212
|
+
luminosity,
|
|
213
|
+
rgbToHex,
|
|
214
|
+
rgbToHsv,
|
|
215
|
+
textToRgb
|
|
216
|
+
});
|
|
217
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/index.cjs.js.map
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../index.ts"],
|
|
4
|
+
"sourcesContent": ["export type RGBA = Record<'r' | 'g' | 'b' | 'a', number>\r\nexport type RGB = Record<'r' | 'g' | 'b', number> & { a?: number }\r\nexport type HEX = string\r\nexport type HSVA = Record<'h' | 's' | 'v' | 'a', number>\r\n\r\nexport type RGBA_TEXT = string\r\nexport type HEX_TEXT = string\r\nexport type HSVA_TEXT = string\r\nexport type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT\r\n\r\nconst reRGBA = /^rgb(a)?\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3}),?([01]?\\.?\\d*?)?\\)$/\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HEX/A representation as a\r\n *\r\n * String (`#RRGGBB<AA>`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHex({ r, g, b, a }: RGBA): HEX {\r\n const alpha = a !== void 0\r\n\r\n r = Math.round(r)\r\n g = Math.round(g)\r\n b = Math.round(b)\r\n\r\n if (r > 255 || g > 255 || b > 255 || (alpha && a > 100)) {\r\n throw new TypeError('Expected 3 numbers below 256 (and optionally one below 100)')\r\n }\r\n\r\n if (alpha) {\r\n a = Number((Math.round((255 * a) / 100) | (1 << 8)).toString(16).slice(1))\r\n }\r\n\r\n return '#' + (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1) + a\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hexToRgb(hex: HEX): RGB {\r\n if (typeof hex !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n hex = hex.replace(/^#/, '')\r\n\r\n if (hex.length === 3) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]\r\n } else if (hex.length === 4) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3]\r\n }\r\n\r\n const num = parseInt(hex, 16)\r\n\r\n return hex.length > 6\r\n ? { r: (num >> 24) & 255, g: (num >> 16) & 255, b: (num >> 8) & 255, a: Math.round((num & 255) / 2.55) }\r\n : { r: num >> 16, g: (num >> 8) & 255, b: num & 255 }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hsvToRgb({ h, s, v, a }: HSVA): RGBA {\r\n let r: any, g: any, b: any\r\n s = s / 100\r\n v = v / 100\r\n\r\n h = h / 360\r\n const i = Math.floor(h * 6),\r\n f = h * 6 - i,\r\n p = v * (1 - s),\r\n q = v * (1 - f * s),\r\n t = v * (1 - (1 - f) * s)\r\n\r\n switch (i % 6) {\r\n case 0:\r\n r = v\r\n g = t\r\n b = p\r\n break\r\n case 1:\r\n r = q\r\n g = v\r\n b = p\r\n break\r\n case 2:\r\n r = p\r\n g = v\r\n b = t\r\n break\r\n case 3:\r\n r = p\r\n g = q\r\n b = v\r\n break\r\n case 4:\r\n r = t\r\n g = p\r\n b = v\r\n break\r\n case 5:\r\n r = v\r\n g = p\r\n b = q\r\n break\r\n }\r\n\r\n return {\r\n r: Math.round(r * 255),\r\n g: Math.round(g * 255),\r\n b: Math.round(b * 255),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HSV/A representation as an\r\n *\r\n * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-100]}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHsv({ r, g, b, a }: RGBA): HSVA {\r\n const max = Math.max(r, g, b),\r\n min = Math.min(r, g, b),\r\n d = max - min,\r\n s = max === 0 ? 0 : d / max,\r\n v = max / 255\r\n let h: any\r\n\r\n switch (max) {\r\n case min:\r\n h = 0\r\n break\r\n case r:\r\n h = g - b + d * (g < b ? 6 : 0)\r\n h /= 6 * d\r\n break\r\n case g:\r\n h = b - r + d * 2\r\n h /= 6 * d\r\n break\r\n case b:\r\n h = r - g + d * 4\r\n h /= 6 * d\r\n break\r\n }\r\n\r\n return {\r\n h: Math.round(h * 360),\r\n s: Math.round(s * 100),\r\n v: Math.round(v * 100),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function textToRgb(str: COLOR): RGB {\r\n if (typeof str !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n const color = str.replace(/ /g, '')\r\n\r\n const m = reRGBA.exec(color)\r\n\r\n if (m === null) {\r\n return hexToRgb(color)\r\n }\r\n\r\n const rgb: RGB = {\r\n r: Math.min(255, parseInt(m[2], 10)),\r\n g: Math.min(255, parseInt(m[3], 10)),\r\n b: Math.min(255, parseInt(m[4], 10))\r\n }\r\n\r\n if (m[1]) {\r\n const alpha = parseFloat(m[5])\r\n rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100\r\n }\r\n\r\n return rgb\r\n}\r\n\r\n/**\r\n * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).\r\n *\r\n * Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 100 or -100 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.\r\n */\r\nexport function lighten(color: COLOR, percent: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n if (typeof percent !== 'number') {\r\n throw new TypeError('Expected a numeric percent')\r\n }\r\n\r\n const rgb = textToRgb(color),\r\n t = percent < 0 ? 0 : 255,\r\n p = Math.abs(percent) / 100,\r\n R = rgb.r,\r\n G = rgb.g,\r\n B = rgb.b\r\n\r\n return (\r\n '#' +\r\n (\r\n 0x1_00_00_00 +\r\n (Math.round((t - R) * p) + R) * 0x1_00_00 +\r\n (Math.round((t - G) * p) + G) * 0x1_00 +\r\n (Math.round((t - B) * p) + B)\r\n )\r\n .toString(16)\r\n .slice(1)\r\n )\r\n}\r\n/**\r\n * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function luminosity(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color,\r\n r = rgb.r / 255,\r\n g = rgb.g / 255,\r\n b = rgb.b / 255,\r\n R = r <= 0.039_28 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4),\r\n G = g <= 0.039_28 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4),\r\n B = b <= 0.039_28 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4)\r\n return 0.2126 * R + 0.7152 * G + 0.0722 * B\r\n}\r\n/**\r\n * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function brightness(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color\r\n\r\n return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000\r\n}\r\n\r\n/**\r\n * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.\r\n *\r\n * Accepts a HEX/A String or a RGB/A Object as `fgColor`/`bgColor`. If the alpha channel of the `fgColor` is completely opaque, then the result will be the `fgColor`. If the alpha channel of the `bgColor` is completely opaque, then the resulting blended color will also be opaque. Returns the same type as input for fgColor.\r\n */\r\nexport function blend(fgColor: COLOR | RGB, bgColor: COLOR | RGB) {\r\n if (typeof fgColor !== 'string' && (!fgColor || fgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as fgColor')\r\n }\r\n\r\n if (typeof bgColor !== 'string' && (!bgColor || bgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as bgColor')\r\n }\r\n\r\n const rgb1 = typeof fgColor === 'string' ? textToRgb(fgColor) : fgColor,\r\n r1 = rgb1.r / 255,\r\n g1 = rgb1.g / 255,\r\n b1 = rgb1.b / 255,\r\n a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1,\r\n rgb2 = typeof bgColor === 'string' ? textToRgb(bgColor) : bgColor,\r\n r2 = rgb2.r / 255,\r\n g2 = rgb2.g / 255,\r\n b2 = rgb2.b / 255,\r\n a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1,\r\n a = a1 + a2 * (1 - a1),\r\n r = Math.round(((r1 * a1 + r2 * a2 * (1 - a1)) / a) * 255),\r\n g = Math.round(((g1 * a1 + g2 * a2 * (1 - a1)) / a) * 255),\r\n b = Math.round(((b1 * a1 + b2 * a2 * (1 - a1)) / a) * 255)\r\n\r\n const ret = { r, g, b, a: Math.round(a * 100) }\r\n return typeof fgColor === 'string' ? rgbToHex(ret) : ret\r\n}\r\n\r\n/**\r\n * Increments or decrements the alpha of a string color.\r\n *\r\n * Accepts a HEX/A String as color and a number between -1 and 1 (including edges) as offset. Use a negative value to decrement and a positive number to increment (ex: changeAlpha('#ff0000', -0.1) to decrement alpha by 10%). Returns HEX/A String.\r\n */\r\nexport function changeAlpha(color: COLOR, offset: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n\r\n if (offset === void 0 || offset < -1 || offset > 1) {\r\n throw new TypeError('Expected offset to be between -1 and 1')\r\n }\r\n\r\n const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,IACA,GAAG,KAAK,MAAM,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,QAAQ,MAAM,CAAC,IAAI,GAAG;AAAA,EAC9D,CAAC;AACH;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
declare type RGBA = Record<'r' | 'g' | 'b' | 'a', number>;
|
|
2
|
+
declare type RGB = Record<'r' | 'g' | 'b', number> & {
|
|
3
|
+
a?: number;
|
|
4
|
+
};
|
|
5
|
+
declare type HEX = string;
|
|
6
|
+
declare type HSVA = Record<'h' | 's' | 'v' | 'a', number>;
|
|
7
|
+
declare type RGBA_TEXT = string;
|
|
8
|
+
declare type HEX_TEXT = string;
|
|
9
|
+
declare type HSVA_TEXT = string;
|
|
10
|
+
declare type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT;
|
|
11
|
+
/**
|
|
12
|
+
* Converts a RGB/A color
|
|
13
|
+
*
|
|
14
|
+
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
|
|
15
|
+
*
|
|
16
|
+
* to its HEX/A representation as a
|
|
17
|
+
*
|
|
18
|
+
* String (`#RRGGBB<AA>`).
|
|
19
|
+
*
|
|
20
|
+
* If Alpha channel is present in the original object it will be present also in the output.
|
|
21
|
+
*/
|
|
22
|
+
declare function rgbToHex({ r, g, b, a }: RGBA): HEX;
|
|
23
|
+
/**
|
|
24
|
+
* Converts a HEX/A color
|
|
25
|
+
*
|
|
26
|
+
* String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
|
|
27
|
+
*
|
|
28
|
+
* to its RGB/A representation as an
|
|
29
|
+
*
|
|
30
|
+
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
|
|
31
|
+
*
|
|
32
|
+
* If Alpha channel is present in the original object it will be present also in the output.
|
|
33
|
+
*/
|
|
34
|
+
declare function hexToRgb(hex: HEX): RGB;
|
|
35
|
+
/**
|
|
36
|
+
* Converts a HEX/A color
|
|
37
|
+
*
|
|
38
|
+
* String (`#RRGGBB<AA>`)
|
|
39
|
+
*
|
|
40
|
+
* to its RGB/A representation as an
|
|
41
|
+
*
|
|
42
|
+
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .
|
|
43
|
+
*
|
|
44
|
+
* If Alpha channel is present in the original object it will be present also in the output.
|
|
45
|
+
*/
|
|
46
|
+
declare function hsvToRgb({ h, s, v, a }: HSVA): RGBA;
|
|
47
|
+
/**
|
|
48
|
+
* Converts a RGB/A color
|
|
49
|
+
*
|
|
50
|
+
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
|
|
51
|
+
*
|
|
52
|
+
* to its HSV/A representation as an
|
|
53
|
+
*
|
|
54
|
+
* Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-100]}`).
|
|
55
|
+
*
|
|
56
|
+
* If Alpha channel is present in the original object it will be present also in the output.
|
|
57
|
+
*/
|
|
58
|
+
declare function rgbToHsv({ r, g, b, a }: RGBA): HSVA;
|
|
59
|
+
/**
|
|
60
|
+
* Converts a HEX/A color
|
|
61
|
+
*
|
|
62
|
+
* String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
|
|
63
|
+
*
|
|
64
|
+
* to its RGB/A representation as an
|
|
65
|
+
*
|
|
66
|
+
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
|
|
67
|
+
*
|
|
68
|
+
* If Alpha channel is present in the original object it will be present also in the output.
|
|
69
|
+
*/
|
|
70
|
+
declare function textToRgb(str: COLOR): RGB;
|
|
71
|
+
/**
|
|
72
|
+
* Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).
|
|
73
|
+
*
|
|
74
|
+
* Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 100 or -100 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.
|
|
75
|
+
*/
|
|
76
|
+
declare function lighten(color: COLOR, percent: number): string;
|
|
77
|
+
/**
|
|
78
|
+
* Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.
|
|
79
|
+
*
|
|
80
|
+
* Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
|
|
81
|
+
*/
|
|
82
|
+
declare function luminosity(color: COLOR | RGB): number;
|
|
83
|
+
/**
|
|
84
|
+
* Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.
|
|
85
|
+
*
|
|
86
|
+
* Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
|
|
87
|
+
*/
|
|
88
|
+
declare function brightness(color: COLOR | RGB): number;
|
|
89
|
+
/**
|
|
90
|
+
* Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.
|
|
91
|
+
*
|
|
92
|
+
* Accepts a HEX/A String or a RGB/A Object as `fgColor`/`bgColor`. If the alpha channel of the `fgColor` is completely opaque, then the result will be the `fgColor`. If the alpha channel of the `bgColor` is completely opaque, then the resulting blended color will also be opaque. Returns the same type as input for fgColor.
|
|
93
|
+
*/
|
|
94
|
+
declare function blend(fgColor: COLOR | RGB, bgColor: COLOR | RGB): string | {
|
|
95
|
+
r: number;
|
|
96
|
+
g: number;
|
|
97
|
+
b: number;
|
|
98
|
+
a: number;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Increments or decrements the alpha of a string color.
|
|
102
|
+
*
|
|
103
|
+
* Accepts a HEX/A String as color and a number between -1 and 1 (including edges) as offset. Use a negative value to decrement and a positive number to increment (ex: changeAlpha('#ff0000', -0.1) to decrement alpha by 10%). Returns HEX/A String.
|
|
104
|
+
*/
|
|
105
|
+
declare function changeAlpha(color: COLOR, offset: number): string;
|
|
106
|
+
|
|
107
|
+
export { COLOR, HEX, HEX_TEXT, HSVA, HSVA_TEXT, RGB, RGBA, RGBA_TEXT, blend, brightness, changeAlpha, hexToRgb, hsvToRgb, lighten, luminosity, rgbToHex, rgbToHsv, textToRgb };
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
var reRGBA = /^rgb(a)?\((\d{1,3}),(\d{1,3}),(\d{1,3}),?([01]?\.?\d*?)?\)$/;
|
|
3
|
+
function rgbToHex({ r, g, b, a }) {
|
|
4
|
+
const alpha = a !== void 0;
|
|
5
|
+
r = Math.round(r);
|
|
6
|
+
g = Math.round(g);
|
|
7
|
+
b = Math.round(b);
|
|
8
|
+
if (r > 255 || g > 255 || b > 255 || alpha && a > 100) {
|
|
9
|
+
throw new TypeError("Expected 3 numbers below 256 (and optionally one below 100)");
|
|
10
|
+
}
|
|
11
|
+
if (alpha) {
|
|
12
|
+
a = Number((Math.round(255 * a / 100) | 1 << 8).toString(16).slice(1));
|
|
13
|
+
}
|
|
14
|
+
return "#" + (b | g << 8 | r << 16 | 1 << 24).toString(16).slice(1) + a;
|
|
15
|
+
}
|
|
16
|
+
function hexToRgb(hex) {
|
|
17
|
+
if (typeof hex !== "string") {
|
|
18
|
+
throw new TypeError("Expected a string");
|
|
19
|
+
}
|
|
20
|
+
hex = hex.replace(/^#/, "");
|
|
21
|
+
if (hex.length === 3) {
|
|
22
|
+
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
|
|
23
|
+
} else if (hex.length === 4) {
|
|
24
|
+
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
|
|
25
|
+
}
|
|
26
|
+
const num = parseInt(hex, 16);
|
|
27
|
+
return hex.length > 6 ? { r: num >> 24 & 255, g: num >> 16 & 255, b: num >> 8 & 255, a: Math.round((num & 255) / 2.55) } : { r: num >> 16, g: num >> 8 & 255, b: num & 255 };
|
|
28
|
+
}
|
|
29
|
+
function hsvToRgb({ h, s, v, a }) {
|
|
30
|
+
let r, g, b;
|
|
31
|
+
s = s / 100;
|
|
32
|
+
v = v / 100;
|
|
33
|
+
h = h / 360;
|
|
34
|
+
const i = Math.floor(h * 6), f = h * 6 - i, p = v * (1 - s), q = v * (1 - f * s), t = v * (1 - (1 - f) * s);
|
|
35
|
+
switch (i % 6) {
|
|
36
|
+
case 0:
|
|
37
|
+
r = v;
|
|
38
|
+
g = t;
|
|
39
|
+
b = p;
|
|
40
|
+
break;
|
|
41
|
+
case 1:
|
|
42
|
+
r = q;
|
|
43
|
+
g = v;
|
|
44
|
+
b = p;
|
|
45
|
+
break;
|
|
46
|
+
case 2:
|
|
47
|
+
r = p;
|
|
48
|
+
g = v;
|
|
49
|
+
b = t;
|
|
50
|
+
break;
|
|
51
|
+
case 3:
|
|
52
|
+
r = p;
|
|
53
|
+
g = q;
|
|
54
|
+
b = v;
|
|
55
|
+
break;
|
|
56
|
+
case 4:
|
|
57
|
+
r = t;
|
|
58
|
+
g = p;
|
|
59
|
+
b = v;
|
|
60
|
+
break;
|
|
61
|
+
case 5:
|
|
62
|
+
r = v;
|
|
63
|
+
g = p;
|
|
64
|
+
b = q;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
r: Math.round(r * 255),
|
|
69
|
+
g: Math.round(g * 255),
|
|
70
|
+
b: Math.round(b * 255),
|
|
71
|
+
a
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function rgbToHsv({ r, g, b, a }) {
|
|
75
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b), d = max - min, s = max === 0 ? 0 : d / max, v = max / 255;
|
|
76
|
+
let h;
|
|
77
|
+
switch (max) {
|
|
78
|
+
case min:
|
|
79
|
+
h = 0;
|
|
80
|
+
break;
|
|
81
|
+
case r:
|
|
82
|
+
h = g - b + d * (g < b ? 6 : 0);
|
|
83
|
+
h /= 6 * d;
|
|
84
|
+
break;
|
|
85
|
+
case g:
|
|
86
|
+
h = b - r + d * 2;
|
|
87
|
+
h /= 6 * d;
|
|
88
|
+
break;
|
|
89
|
+
case b:
|
|
90
|
+
h = r - g + d * 4;
|
|
91
|
+
h /= 6 * d;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
h: Math.round(h * 360),
|
|
96
|
+
s: Math.round(s * 100),
|
|
97
|
+
v: Math.round(v * 100),
|
|
98
|
+
a
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function textToRgb(str) {
|
|
102
|
+
if (typeof str !== "string") {
|
|
103
|
+
throw new TypeError("Expected a string");
|
|
104
|
+
}
|
|
105
|
+
const color = str.replace(/ /g, "");
|
|
106
|
+
const m = reRGBA.exec(color);
|
|
107
|
+
if (m === null) {
|
|
108
|
+
return hexToRgb(color);
|
|
109
|
+
}
|
|
110
|
+
const rgb = {
|
|
111
|
+
r: Math.min(255, parseInt(m[2], 10)),
|
|
112
|
+
g: Math.min(255, parseInt(m[3], 10)),
|
|
113
|
+
b: Math.min(255, parseInt(m[4], 10))
|
|
114
|
+
};
|
|
115
|
+
if (m[1]) {
|
|
116
|
+
const alpha = parseFloat(m[5]);
|
|
117
|
+
rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100;
|
|
118
|
+
}
|
|
119
|
+
return rgb;
|
|
120
|
+
}
|
|
121
|
+
function lighten(color, percent) {
|
|
122
|
+
if (typeof color !== "string") {
|
|
123
|
+
throw new TypeError("Expected a string as color");
|
|
124
|
+
}
|
|
125
|
+
if (typeof percent !== "number") {
|
|
126
|
+
throw new TypeError("Expected a numeric percent");
|
|
127
|
+
}
|
|
128
|
+
const rgb = textToRgb(color), t = percent < 0 ? 0 : 255, p = Math.abs(percent) / 100, R = rgb.r, G = rgb.g, B = rgb.b;
|
|
129
|
+
return "#" + (16777216 + (Math.round((t - R) * p) + R) * 65536 + (Math.round((t - G) * p) + G) * 256 + (Math.round((t - B) * p) + B)).toString(16).slice(1);
|
|
130
|
+
}
|
|
131
|
+
function luminosity(color) {
|
|
132
|
+
if (typeof color !== "string" && (!color || color.r === void 0)) {
|
|
133
|
+
throw new TypeError("Expected a string or a {r, g, b} object as color");
|
|
134
|
+
}
|
|
135
|
+
const rgb = typeof color === "string" ? textToRgb(color) : color, r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255, R = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4), G = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4), B = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
|
|
136
|
+
return 0.2126 * R + 0.7152 * G + 0.0722 * B;
|
|
137
|
+
}
|
|
138
|
+
function brightness(color) {
|
|
139
|
+
if (typeof color !== "string" && (!color || color.r === void 0)) {
|
|
140
|
+
throw new TypeError("Expected a string or a {r, g, b} object as color");
|
|
141
|
+
}
|
|
142
|
+
const rgb = typeof color === "string" ? textToRgb(color) : color;
|
|
143
|
+
return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1e3;
|
|
144
|
+
}
|
|
145
|
+
function blend(fgColor, bgColor) {
|
|
146
|
+
if (typeof fgColor !== "string" && (!fgColor || fgColor.r === void 0)) {
|
|
147
|
+
throw new TypeError("Expected a string or a {r, g, b[, a]} object as fgColor");
|
|
148
|
+
}
|
|
149
|
+
if (typeof bgColor !== "string" && (!bgColor || bgColor.r === void 0)) {
|
|
150
|
+
throw new TypeError("Expected a string or a {r, g, b[, a]} object as bgColor");
|
|
151
|
+
}
|
|
152
|
+
const rgb1 = typeof fgColor === "string" ? textToRgb(fgColor) : fgColor, r1 = rgb1.r / 255, g1 = rgb1.g / 255, b1 = rgb1.b / 255, a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1, rgb2 = typeof bgColor === "string" ? textToRgb(bgColor) : bgColor, r2 = rgb2.r / 255, g2 = rgb2.g / 255, b2 = rgb2.b / 255, a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1, a = a1 + a2 * (1 - a1), r = Math.round((r1 * a1 + r2 * a2 * (1 - a1)) / a * 255), g = Math.round((g1 * a1 + g2 * a2 * (1 - a1)) / a * 255), b = Math.round((b1 * a1 + b2 * a2 * (1 - a1)) / a * 255);
|
|
153
|
+
const ret = { r, g, b, a: Math.round(a * 100) };
|
|
154
|
+
return typeof fgColor === "string" ? rgbToHex(ret) : ret;
|
|
155
|
+
}
|
|
156
|
+
function changeAlpha(color, offset) {
|
|
157
|
+
if (typeof color !== "string") {
|
|
158
|
+
throw new TypeError("Expected a string as color");
|
|
159
|
+
}
|
|
160
|
+
if (offset === void 0 || offset < -1 || offset > 1) {
|
|
161
|
+
throw new TypeError("Expected offset to be between -1 and 1");
|
|
162
|
+
}
|
|
163
|
+
const { r, g, b, a } = textToRgb(color);
|
|
164
|
+
const alpha = a !== void 0 ? a / 100 : 0;
|
|
165
|
+
return rgbToHex({
|
|
166
|
+
r,
|
|
167
|
+
g,
|
|
168
|
+
b,
|
|
169
|
+
a: Math.round(Math.min(1, Math.max(0, alpha + offset)) * 100)
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
export {
|
|
173
|
+
blend,
|
|
174
|
+
brightness,
|
|
175
|
+
changeAlpha,
|
|
176
|
+
hexToRgb,
|
|
177
|
+
hsvToRgb,
|
|
178
|
+
lighten,
|
|
179
|
+
luminosity,
|
|
180
|
+
rgbToHex,
|
|
181
|
+
rgbToHsv,
|
|
182
|
+
textToRgb
|
|
183
|
+
};
|
|
184
|
+
//# sourceMappingURL=index.esm.js.map
|
package/index.esm.js.map
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../index.ts"],
|
|
4
|
+
"sourcesContent": ["export type RGBA = Record<'r' | 'g' | 'b' | 'a', number>\r\nexport type RGB = Record<'r' | 'g' | 'b', number> & { a?: number }\r\nexport type HEX = string\r\nexport type HSVA = Record<'h' | 's' | 'v' | 'a', number>\r\n\r\nexport type RGBA_TEXT = string\r\nexport type HEX_TEXT = string\r\nexport type HSVA_TEXT = string\r\nexport type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT\r\n\r\nconst reRGBA = /^rgb(a)?\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3}),?([01]?\\.?\\d*?)?\\)$/\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HEX/A representation as a\r\n *\r\n * String (`#RRGGBB<AA>`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHex({ r, g, b, a }: RGBA): HEX {\r\n const alpha = a !== void 0\r\n\r\n r = Math.round(r)\r\n g = Math.round(g)\r\n b = Math.round(b)\r\n\r\n if (r > 255 || g > 255 || b > 255 || (alpha && a > 100)) {\r\n throw new TypeError('Expected 3 numbers below 256 (and optionally one below 100)')\r\n }\r\n\r\n if (alpha) {\r\n a = Number((Math.round((255 * a) / 100) | (1 << 8)).toString(16).slice(1))\r\n }\r\n\r\n return '#' + (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1) + a\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hexToRgb(hex: HEX): RGB {\r\n if (typeof hex !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n hex = hex.replace(/^#/, '')\r\n\r\n if (hex.length === 3) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]\r\n } else if (hex.length === 4) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3]\r\n }\r\n\r\n const num = parseInt(hex, 16)\r\n\r\n return hex.length > 6\r\n ? { r: (num >> 24) & 255, g: (num >> 16) & 255, b: (num >> 8) & 255, a: Math.round((num & 255) / 2.55) }\r\n : { r: num >> 16, g: (num >> 8) & 255, b: num & 255 }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hsvToRgb({ h, s, v, a }: HSVA): RGBA {\r\n let r: any, g: any, b: any\r\n s = s / 100\r\n v = v / 100\r\n\r\n h = h / 360\r\n const i = Math.floor(h * 6),\r\n f = h * 6 - i,\r\n p = v * (1 - s),\r\n q = v * (1 - f * s),\r\n t = v * (1 - (1 - f) * s)\r\n\r\n switch (i % 6) {\r\n case 0:\r\n r = v\r\n g = t\r\n b = p\r\n break\r\n case 1:\r\n r = q\r\n g = v\r\n b = p\r\n break\r\n case 2:\r\n r = p\r\n g = v\r\n b = t\r\n break\r\n case 3:\r\n r = p\r\n g = q\r\n b = v\r\n break\r\n case 4:\r\n r = t\r\n g = p\r\n b = v\r\n break\r\n case 5:\r\n r = v\r\n g = p\r\n b = q\r\n break\r\n }\r\n\r\n return {\r\n r: Math.round(r * 255),\r\n g: Math.round(g * 255),\r\n b: Math.round(b * 255),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HSV/A representation as an\r\n *\r\n * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-100]}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHsv({ r, g, b, a }: RGBA): HSVA {\r\n const max = Math.max(r, g, b),\r\n min = Math.min(r, g, b),\r\n d = max - min,\r\n s = max === 0 ? 0 : d / max,\r\n v = max / 255\r\n let h: any\r\n\r\n switch (max) {\r\n case min:\r\n h = 0\r\n break\r\n case r:\r\n h = g - b + d * (g < b ? 6 : 0)\r\n h /= 6 * d\r\n break\r\n case g:\r\n h = b - r + d * 2\r\n h /= 6 * d\r\n break\r\n case b:\r\n h = r - g + d * 4\r\n h /= 6 * d\r\n break\r\n }\r\n\r\n return {\r\n h: Math.round(h * 360),\r\n s: Math.round(s * 100),\r\n v: Math.round(v * 100),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function textToRgb(str: COLOR): RGB {\r\n if (typeof str !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n const color = str.replace(/ /g, '')\r\n\r\n const m = reRGBA.exec(color)\r\n\r\n if (m === null) {\r\n return hexToRgb(color)\r\n }\r\n\r\n const rgb: RGB = {\r\n r: Math.min(255, parseInt(m[2], 10)),\r\n g: Math.min(255, parseInt(m[3], 10)),\r\n b: Math.min(255, parseInt(m[4], 10))\r\n }\r\n\r\n if (m[1]) {\r\n const alpha = parseFloat(m[5])\r\n rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100\r\n }\r\n\r\n return rgb\r\n}\r\n\r\n/**\r\n * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).\r\n *\r\n * Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 100 or -100 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.\r\n */\r\nexport function lighten(color: COLOR, percent: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n if (typeof percent !== 'number') {\r\n throw new TypeError('Expected a numeric percent')\r\n }\r\n\r\n const rgb = textToRgb(color),\r\n t = percent < 0 ? 0 : 255,\r\n p = Math.abs(percent) / 100,\r\n R = rgb.r,\r\n G = rgb.g,\r\n B = rgb.b\r\n\r\n return (\r\n '#' +\r\n (\r\n 0x1_00_00_00 +\r\n (Math.round((t - R) * p) + R) * 0x1_00_00 +\r\n (Math.round((t - G) * p) + G) * 0x1_00 +\r\n (Math.round((t - B) * p) + B)\r\n )\r\n .toString(16)\r\n .slice(1)\r\n )\r\n}\r\n/**\r\n * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function luminosity(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color,\r\n r = rgb.r / 255,\r\n g = rgb.g / 255,\r\n b = rgb.b / 255,\r\n R = r <= 0.039_28 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4),\r\n G = g <= 0.039_28 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4),\r\n B = b <= 0.039_28 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4)\r\n return 0.2126 * R + 0.7152 * G + 0.0722 * B\r\n}\r\n/**\r\n * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function brightness(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color\r\n\r\n return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000\r\n}\r\n\r\n/**\r\n * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.\r\n *\r\n * Accepts a HEX/A String or a RGB/A Object as `fgColor`/`bgColor`. If the alpha channel of the `fgColor` is completely opaque, then the result will be the `fgColor`. If the alpha channel of the `bgColor` is completely opaque, then the resulting blended color will also be opaque. Returns the same type as input for fgColor.\r\n */\r\nexport function blend(fgColor: COLOR | RGB, bgColor: COLOR | RGB) {\r\n if (typeof fgColor !== 'string' && (!fgColor || fgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as fgColor')\r\n }\r\n\r\n if (typeof bgColor !== 'string' && (!bgColor || bgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as bgColor')\r\n }\r\n\r\n const rgb1 = typeof fgColor === 'string' ? textToRgb(fgColor) : fgColor,\r\n r1 = rgb1.r / 255,\r\n g1 = rgb1.g / 255,\r\n b1 = rgb1.b / 255,\r\n a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1,\r\n rgb2 = typeof bgColor === 'string' ? textToRgb(bgColor) : bgColor,\r\n r2 = rgb2.r / 255,\r\n g2 = rgb2.g / 255,\r\n b2 = rgb2.b / 255,\r\n a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1,\r\n a = a1 + a2 * (1 - a1),\r\n r = Math.round(((r1 * a1 + r2 * a2 * (1 - a1)) / a) * 255),\r\n g = Math.round(((g1 * a1 + g2 * a2 * (1 - a1)) / a) * 255),\r\n b = Math.round(((b1 * a1 + b2 * a2 * (1 - a1)) / a) * 255)\r\n\r\n const ret = { r, g, b, a: Math.round(a * 100) }\r\n return typeof fgColor === 'string' ? rgbToHex(ret) : ret\r\n}\r\n\r\n/**\r\n * Increments or decrements the alpha of a string color.\r\n *\r\n * Accepts a HEX/A String as color and a number between -1 and 1 (including edges) as offset. Use a negative value to decrement and a positive number to increment (ex: changeAlpha('#ff0000', -0.1) to decrement alpha by 10%). Returns HEX/A String.\r\n */\r\nexport function changeAlpha(color: COLOR, offset: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n\r\n if (offset === void 0 || offset < -1 || offset > 1) {\r\n throw new TypeError('Expected offset to be between -1 and 1')\r\n }\r\n\r\n const { r, g, b, a } = textToRgb(color)\r\n const alpha = a !== void 0 ? a / 100 : 0\r\n\r\n return rgbToHex({\r\n r,\r\n g,\r\n b,\r\n a: Math.round(Math.min(1, Math.max(0, alpha + offset)) * 100)\r\n })\r\n}\r\n"],
|
|
5
|
+
"mappings": ";AAUA,IAAM,SAAS;AAaR,kBAAkB,EAAE,GAAG,GAAG,GAAG,KAAgB;AAClD,QAAM,QAAQ,MAAM;AAEpB,MAAI,KAAK,MAAM,CAAC;AAChB,MAAI,KAAK,MAAM,CAAC;AAChB,MAAI,KAAK,MAAM,CAAC;AAEhB,MAAI,IAAI,OAAO,IAAI,OAAO,IAAI,OAAQ,SAAS,IAAI,KAAM;AACvD,UAAM,IAAI,UAAU,6DAA6D;AAAA,EACnF;AAEA,MAAI,OAAO;AACT,QAAI,OAAQ,MAAK,MAAO,MAAM,IAAK,GAAG,IAAK,KAAK,GAAI,SAAS,EAAE,EAAE,MAAM,CAAC,CAAC;AAAA,EAC3E;AAEA,SAAO,MAAO,KAAK,KAAK,IAAM,KAAK,KAAO,KAAK,IAAK,SAAS,EAAE,EAAE,MAAM,CAAC,IAAI;AAC9E;AAaO,kBAAkB,KAAe;AACtC,MAAI,OAAO,QAAQ,UAAU;AAC3B,UAAM,IAAI,UAAU,mBAAmB;AAAA,EACzC;AAEA,QAAM,IAAI,QAAQ,MAAM,EAAE;AAE1B,MAAI,IAAI,WAAW,GAAG;AACpB,UAAM,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI;AAAA,EACzD,WAAW,IAAI,WAAW,GAAG;AAC3B,UAAM,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,I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SAAO,SAAS;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA,GAAG,KAAK,MAAM,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,QAAQ,MAAM,CAAC,IAAI,GAAG;AAAA,EAC9D,CAAC;AACH;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/index.iife.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
var HairyPalette = (() => {
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// index.ts
|
|
21
|
+
var util_color_exports = {};
|
|
22
|
+
__export(util_color_exports, {
|
|
23
|
+
blend: () => blend,
|
|
24
|
+
brightness: () => brightness,
|
|
25
|
+
changeAlpha: () => changeAlpha,
|
|
26
|
+
hexToRgb: () => hexToRgb,
|
|
27
|
+
hsvToRgb: () => hsvToRgb,
|
|
28
|
+
lighten: () => lighten,
|
|
29
|
+
luminosity: () => luminosity,
|
|
30
|
+
rgbToHex: () => rgbToHex,
|
|
31
|
+
rgbToHsv: () => rgbToHsv,
|
|
32
|
+
textToRgb: () => textToRgb
|
|
33
|
+
});
|
|
34
|
+
var reRGBA = /^rgb(a)?\((\d{1,3}),(\d{1,3}),(\d{1,3}),?([01]?\.?\d*?)?\)$/;
|
|
35
|
+
function rgbToHex({ r, g, b, a }) {
|
|
36
|
+
const alpha = a !== void 0;
|
|
37
|
+
r = Math.round(r);
|
|
38
|
+
g = Math.round(g);
|
|
39
|
+
b = Math.round(b);
|
|
40
|
+
if (r > 255 || g > 255 || b > 255 || alpha && a > 100) {
|
|
41
|
+
throw new TypeError("Expected 3 numbers below 256 (and optionally one below 100)");
|
|
42
|
+
}
|
|
43
|
+
if (alpha) {
|
|
44
|
+
a = Number((Math.round(255 * a / 100) | 1 << 8).toString(16).slice(1));
|
|
45
|
+
}
|
|
46
|
+
return "#" + (b | g << 8 | r << 16 | 1 << 24).toString(16).slice(1) + a;
|
|
47
|
+
}
|
|
48
|
+
function hexToRgb(hex) {
|
|
49
|
+
if (typeof hex !== "string") {
|
|
50
|
+
throw new TypeError("Expected a string");
|
|
51
|
+
}
|
|
52
|
+
hex = hex.replace(/^#/, "");
|
|
53
|
+
if (hex.length === 3) {
|
|
54
|
+
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
|
|
55
|
+
} else if (hex.length === 4) {
|
|
56
|
+
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
|
|
57
|
+
}
|
|
58
|
+
const num = parseInt(hex, 16);
|
|
59
|
+
return hex.length > 6 ? { r: num >> 24 & 255, g: num >> 16 & 255, b: num >> 8 & 255, a: Math.round((num & 255) / 2.55) } : { r: num >> 16, g: num >> 8 & 255, b: num & 255 };
|
|
60
|
+
}
|
|
61
|
+
function hsvToRgb({ h, s, v, a }) {
|
|
62
|
+
let r, g, b;
|
|
63
|
+
s = s / 100;
|
|
64
|
+
v = v / 100;
|
|
65
|
+
h = h / 360;
|
|
66
|
+
const i = Math.floor(h * 6), f = h * 6 - i, p = v * (1 - s), q = v * (1 - f * s), t = v * (1 - (1 - f) * s);
|
|
67
|
+
switch (i % 6) {
|
|
68
|
+
case 0:
|
|
69
|
+
r = v;
|
|
70
|
+
g = t;
|
|
71
|
+
b = p;
|
|
72
|
+
break;
|
|
73
|
+
case 1:
|
|
74
|
+
r = q;
|
|
75
|
+
g = v;
|
|
76
|
+
b = p;
|
|
77
|
+
break;
|
|
78
|
+
case 2:
|
|
79
|
+
r = p;
|
|
80
|
+
g = v;
|
|
81
|
+
b = t;
|
|
82
|
+
break;
|
|
83
|
+
case 3:
|
|
84
|
+
r = p;
|
|
85
|
+
g = q;
|
|
86
|
+
b = v;
|
|
87
|
+
break;
|
|
88
|
+
case 4:
|
|
89
|
+
r = t;
|
|
90
|
+
g = p;
|
|
91
|
+
b = v;
|
|
92
|
+
break;
|
|
93
|
+
case 5:
|
|
94
|
+
r = v;
|
|
95
|
+
g = p;
|
|
96
|
+
b = q;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
r: Math.round(r * 255),
|
|
101
|
+
g: Math.round(g * 255),
|
|
102
|
+
b: Math.round(b * 255),
|
|
103
|
+
a
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function rgbToHsv({ r, g, b, a }) {
|
|
107
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b), d = max - min, s = max === 0 ? 0 : d / max, v = max / 255;
|
|
108
|
+
let h;
|
|
109
|
+
switch (max) {
|
|
110
|
+
case min:
|
|
111
|
+
h = 0;
|
|
112
|
+
break;
|
|
113
|
+
case r:
|
|
114
|
+
h = g - b + d * (g < b ? 6 : 0);
|
|
115
|
+
h /= 6 * d;
|
|
116
|
+
break;
|
|
117
|
+
case g:
|
|
118
|
+
h = b - r + d * 2;
|
|
119
|
+
h /= 6 * d;
|
|
120
|
+
break;
|
|
121
|
+
case b:
|
|
122
|
+
h = r - g + d * 4;
|
|
123
|
+
h /= 6 * d;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
h: Math.round(h * 360),
|
|
128
|
+
s: Math.round(s * 100),
|
|
129
|
+
v: Math.round(v * 100),
|
|
130
|
+
a
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function textToRgb(str) {
|
|
134
|
+
if (typeof str !== "string") {
|
|
135
|
+
throw new TypeError("Expected a string");
|
|
136
|
+
}
|
|
137
|
+
const color = str.replace(/ /g, "");
|
|
138
|
+
const m = reRGBA.exec(color);
|
|
139
|
+
if (m === null) {
|
|
140
|
+
return hexToRgb(color);
|
|
141
|
+
}
|
|
142
|
+
const rgb = {
|
|
143
|
+
r: Math.min(255, parseInt(m[2], 10)),
|
|
144
|
+
g: Math.min(255, parseInt(m[3], 10)),
|
|
145
|
+
b: Math.min(255, parseInt(m[4], 10))
|
|
146
|
+
};
|
|
147
|
+
if (m[1]) {
|
|
148
|
+
const alpha = parseFloat(m[5]);
|
|
149
|
+
rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100;
|
|
150
|
+
}
|
|
151
|
+
return rgb;
|
|
152
|
+
}
|
|
153
|
+
function lighten(color, percent) {
|
|
154
|
+
if (typeof color !== "string") {
|
|
155
|
+
throw new TypeError("Expected a string as color");
|
|
156
|
+
}
|
|
157
|
+
if (typeof percent !== "number") {
|
|
158
|
+
throw new TypeError("Expected a numeric percent");
|
|
159
|
+
}
|
|
160
|
+
const rgb = textToRgb(color), t = percent < 0 ? 0 : 255, p = Math.abs(percent) / 100, R = rgb.r, G = rgb.g, B = rgb.b;
|
|
161
|
+
return "#" + (16777216 + (Math.round((t - R) * p) + R) * 65536 + (Math.round((t - G) * p) + G) * 256 + (Math.round((t - B) * p) + B)).toString(16).slice(1);
|
|
162
|
+
}
|
|
163
|
+
function luminosity(color) {
|
|
164
|
+
if (typeof color !== "string" && (!color || color.r === void 0)) {
|
|
165
|
+
throw new TypeError("Expected a string or a {r, g, b} object as color");
|
|
166
|
+
}
|
|
167
|
+
const rgb = typeof color === "string" ? textToRgb(color) : color, r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255, R = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4), G = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4), B = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
|
|
168
|
+
return 0.2126 * R + 0.7152 * G + 0.0722 * B;
|
|
169
|
+
}
|
|
170
|
+
function brightness(color) {
|
|
171
|
+
if (typeof color !== "string" && (!color || color.r === void 0)) {
|
|
172
|
+
throw new TypeError("Expected a string or a {r, g, b} object as color");
|
|
173
|
+
}
|
|
174
|
+
const rgb = typeof color === "string" ? textToRgb(color) : color;
|
|
175
|
+
return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1e3;
|
|
176
|
+
}
|
|
177
|
+
function blend(fgColor, bgColor) {
|
|
178
|
+
if (typeof fgColor !== "string" && (!fgColor || fgColor.r === void 0)) {
|
|
179
|
+
throw new TypeError("Expected a string or a {r, g, b[, a]} object as fgColor");
|
|
180
|
+
}
|
|
181
|
+
if (typeof bgColor !== "string" && (!bgColor || bgColor.r === void 0)) {
|
|
182
|
+
throw new TypeError("Expected a string or a {r, g, b[, a]} object as bgColor");
|
|
183
|
+
}
|
|
184
|
+
const rgb1 = typeof fgColor === "string" ? textToRgb(fgColor) : fgColor, r1 = rgb1.r / 255, g1 = rgb1.g / 255, b1 = rgb1.b / 255, a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1, rgb2 = typeof bgColor === "string" ? textToRgb(bgColor) : bgColor, r2 = rgb2.r / 255, g2 = rgb2.g / 255, b2 = rgb2.b / 255, a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1, a = a1 + a2 * (1 - a1), r = Math.round((r1 * a1 + r2 * a2 * (1 - a1)) / a * 255), g = Math.round((g1 * a1 + g2 * a2 * (1 - a1)) / a * 255), b = Math.round((b1 * a1 + b2 * a2 * (1 - a1)) / a * 255);
|
|
185
|
+
const ret = { r, g, b, a: Math.round(a * 100) };
|
|
186
|
+
return typeof fgColor === "string" ? rgbToHex(ret) : ret;
|
|
187
|
+
}
|
|
188
|
+
function changeAlpha(color, offset) {
|
|
189
|
+
if (typeof color !== "string") {
|
|
190
|
+
throw new TypeError("Expected a string as color");
|
|
191
|
+
}
|
|
192
|
+
if (offset === void 0 || offset < -1 || offset > 1) {
|
|
193
|
+
throw new TypeError("Expected offset to be between -1 and 1");
|
|
194
|
+
}
|
|
195
|
+
const { r, g, b, a } = textToRgb(color);
|
|
196
|
+
const alpha = a !== void 0 ? a / 100 : 0;
|
|
197
|
+
return rgbToHex({
|
|
198
|
+
r,
|
|
199
|
+
g,
|
|
200
|
+
b,
|
|
201
|
+
a: Math.round(Math.min(1, Math.max(0, alpha + offset)) * 100)
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return __toCommonJS(util_color_exports);
|
|
205
|
+
})();
|
|
206
|
+
//# sourceMappingURL=index.iife.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../index.ts"],
|
|
4
|
+
"sourcesContent": ["export type RGBA = Record<'r' | 'g' | 'b' | 'a', number>\r\nexport type RGB = Record<'r' | 'g' | 'b', number> & { a?: number }\r\nexport type HEX = string\r\nexport type HSVA = Record<'h' | 's' | 'v' | 'a', number>\r\n\r\nexport type RGBA_TEXT = string\r\nexport type HEX_TEXT = string\r\nexport type HSVA_TEXT = string\r\nexport type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT\r\n\r\nconst reRGBA = /^rgb(a)?\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3}),?([01]?\\.?\\d*?)?\\)$/\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HEX/A representation as a\r\n *\r\n * String (`#RRGGBB<AA>`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHex({ r, g, b, a }: RGBA): HEX {\r\n const alpha = a !== void 0\r\n\r\n r = Math.round(r)\r\n g = Math.round(g)\r\n b = Math.round(b)\r\n\r\n if (r > 255 || g > 255 || b > 255 || (alpha && a > 100)) {\r\n throw new TypeError('Expected 3 numbers below 256 (and optionally one below 100)')\r\n }\r\n\r\n if (alpha) {\r\n a = Number((Math.round((255 * a) / 100) | (1 << 8)).toString(16).slice(1))\r\n }\r\n\r\n return '#' + (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1) + a\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hexToRgb(hex: HEX): RGB {\r\n if (typeof hex !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n hex = hex.replace(/^#/, '')\r\n\r\n if (hex.length === 3) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]\r\n } else if (hex.length === 4) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3]\r\n }\r\n\r\n const num = parseInt(hex, 16)\r\n\r\n return hex.length > 6\r\n ? { r: (num >> 24) & 255, g: (num >> 16) & 255, b: (num >> 8) & 255, a: Math.round((num & 255) / 2.55) }\r\n : { r: num >> 16, g: (num >> 8) & 255, b: num & 255 }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hsvToRgb({ h, s, v, a }: HSVA): RGBA {\r\n let r: any, g: any, b: any\r\n s = s / 100\r\n v = v / 100\r\n\r\n h = h / 360\r\n const i = Math.floor(h * 6),\r\n f = h * 6 - i,\r\n p = v * (1 - s),\r\n q = v * (1 - f * s),\r\n t = v * (1 - (1 - f) * s)\r\n\r\n switch (i % 6) {\r\n case 0:\r\n r = v\r\n g = t\r\n b = p\r\n break\r\n case 1:\r\n r = q\r\n g = v\r\n b = p\r\n break\r\n case 2:\r\n r = p\r\n g = v\r\n b = t\r\n break\r\n case 3:\r\n r = p\r\n g = q\r\n b = v\r\n break\r\n case 4:\r\n r = t\r\n g = p\r\n b = v\r\n break\r\n case 5:\r\n r = v\r\n g = p\r\n b = q\r\n break\r\n }\r\n\r\n return {\r\n r: Math.round(r * 255),\r\n g: Math.round(g * 255),\r\n b: Math.round(b * 255),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HSV/A representation as an\r\n *\r\n * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-100]}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHsv({ r, g, b, a }: RGBA): HSVA {\r\n const max = Math.max(r, g, b),\r\n min = Math.min(r, g, b),\r\n d = max - min,\r\n s = max === 0 ? 0 : d / max,\r\n v = max / 255\r\n let h: any\r\n\r\n switch (max) {\r\n case min:\r\n h = 0\r\n break\r\n case r:\r\n h = g - b + d * (g < b ? 6 : 0)\r\n h /= 6 * d\r\n break\r\n case g:\r\n h = b - r + d * 2\r\n h /= 6 * d\r\n break\r\n case b:\r\n h = r - g + d * 4\r\n h /= 6 * d\r\n break\r\n }\r\n\r\n return {\r\n h: Math.round(h * 360),\r\n s: Math.round(s * 100),\r\n v: Math.round(v * 100),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function textToRgb(str: COLOR): RGB {\r\n if (typeof str !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n const color = str.replace(/ /g, '')\r\n\r\n const m = reRGBA.exec(color)\r\n\r\n if (m === null) {\r\n return hexToRgb(color)\r\n }\r\n\r\n const rgb: RGB = {\r\n r: Math.min(255, parseInt(m[2], 10)),\r\n g: Math.min(255, parseInt(m[3], 10)),\r\n b: Math.min(255, parseInt(m[4], 10))\r\n }\r\n\r\n if (m[1]) {\r\n const alpha = parseFloat(m[5])\r\n rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100\r\n }\r\n\r\n return rgb\r\n}\r\n\r\n/**\r\n * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).\r\n *\r\n * Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 100 or -100 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.\r\n */\r\nexport function lighten(color: COLOR, percent: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n if (typeof percent !== 'number') {\r\n throw new TypeError('Expected a numeric percent')\r\n }\r\n\r\n const rgb = textToRgb(color),\r\n t = percent < 0 ? 0 : 255,\r\n p = Math.abs(percent) / 100,\r\n R = rgb.r,\r\n G = rgb.g,\r\n B = rgb.b\r\n\r\n return (\r\n '#' +\r\n (\r\n 0x1_00_00_00 +\r\n (Math.round((t - R) * p) + R) * 0x1_00_00 +\r\n (Math.round((t - G) * p) + G) * 0x1_00 +\r\n (Math.round((t - B) * p) + B)\r\n )\r\n .toString(16)\r\n .slice(1)\r\n )\r\n}\r\n/**\r\n * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function luminosity(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color,\r\n r = rgb.r / 255,\r\n g = rgb.g / 255,\r\n b = rgb.b / 255,\r\n R = r <= 0.039_28 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4),\r\n G = g <= 0.039_28 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4),\r\n B = b <= 0.039_28 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4)\r\n return 0.2126 * R + 0.7152 * G + 0.0722 * B\r\n}\r\n/**\r\n * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function brightness(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color\r\n\r\n return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000\r\n}\r\n\r\n/**\r\n * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.\r\n *\r\n * Accepts a HEX/A String or a RGB/A Object as `fgColor`/`bgColor`. If the alpha channel of the `fgColor` is completely opaque, then the result will be the `fgColor`. If the alpha channel of the `bgColor` is completely opaque, then the resulting blended color will also be opaque. Returns the same type as input for fgColor.\r\n */\r\nexport function blend(fgColor: COLOR | RGB, bgColor: COLOR | RGB) {\r\n if (typeof fgColor !== 'string' && (!fgColor || fgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as fgColor')\r\n }\r\n\r\n if (typeof bgColor !== 'string' && (!bgColor || bgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as bgColor')\r\n }\r\n\r\n const rgb1 = typeof fgColor === 'string' ? textToRgb(fgColor) : fgColor,\r\n r1 = rgb1.r / 255,\r\n g1 = rgb1.g / 255,\r\n b1 = rgb1.b / 255,\r\n a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1,\r\n rgb2 = typeof bgColor === 'string' ? textToRgb(bgColor) : bgColor,\r\n r2 = rgb2.r / 255,\r\n g2 = rgb2.g / 255,\r\n b2 = rgb2.b / 255,\r\n a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1,\r\n a = a1 + a2 * (1 - a1),\r\n r = Math.round(((r1 * a1 + r2 * a2 * (1 - a1)) / a) * 255),\r\n g = Math.round(((g1 * a1 + g2 * a2 * (1 - a1)) / a) * 255),\r\n b = Math.round(((b1 * a1 + b2 * a2 * (1 - a1)) / a) * 255)\r\n\r\n const ret = { r, g, b, a: Math.round(a * 100) }\r\n return typeof fgColor === 'string' ? rgbToHex(ret) : ret\r\n}\r\n\r\n/**\r\n * Increments or decrements the alpha of a string color.\r\n *\r\n * Accepts a HEX/A String as color and a number between -1 and 1 (including edges) as offset. Use a negative value to decrement and a positive number to increment (ex: changeAlpha('#ff0000', -0.1) to decrement alpha by 10%). Returns HEX/A String.\r\n */\r\nexport function changeAlpha(color: COLOR, offset: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n\r\n if (offset === void 0 || offset < -1 || offset > 1) {\r\n throw new TypeError('Expected offset to be between -1 and 1')\r\n }\r\n\r\n const { r, g, b, a } = textToRgb(color)\r\n const alpha = a !== void 0 ? a / 100 : 0\r\n\r\n return rgbToHex({\r\n r,\r\n g,\r\n b,\r\n a: Math.round(Math.min(1, Math.max(0, alpha + offset)) * 100)\r\n })\r\n}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C9D,CAAC;AAAA,EACH;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var HairyPalette=(()=>{var y=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var B=(t,r)=>{for(var e in r)y(t,e,{get:r[e],enumerable:!0})},G=(t,r,e,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of m(r))!x.call(t,n)&&n!==e&&y(t,n,{get:()=>r[n],enumerable:!(o=T(r,n))||o.enumerable});return t};var O=t=>G(y({},"__esModule",{value:!0}),t);var V={};B(V,{blend:()=>L,brightness:()=>l,changeAlpha:()=>S,hexToRgb:()=>E,hsvToRgb:()=>A,lighten:()=>X,luminosity:()=>k,rgbToHex:()=>h,rgbToHsv:()=>H,textToRgb:()=>u});var _=/^rgb(a)?\((\d{1,3}),(\d{1,3}),(\d{1,3}),?([01]?\.?\d*?)?\)$/;function h({r:t,g:r,b:e,a:o}){let n=o!==void 0;if(t=Math.round(t),r=Math.round(r),e=Math.round(e),t>255||r>255||e>255||n&&o>100)throw new TypeError("Expected 3 numbers below 256 (and optionally one below 100)");return n&&(o=Number((Math.round(255*o/100)|1<<8).toString(16).slice(1))),"#"+(e|r<<8|t<<16|1<<24).toString(16).slice(1)+o}function E(t){if(typeof t!="string")throw new TypeError("Expected a string");t=t.replace(/^#/,""),t.length===3?t=t[0]+t[0]+t[1]+t[1]+t[2]+t[2]:t.length===4&&(t=t[0]+t[0]+t[1]+t[1]+t[2]+t[2]+t[3]+t[3]);let r=parseInt(t,16);return t.length>6?{r:r>>24&255,g:r>>16&255,b:r>>8&255,a:Math.round((r&255)/2.55)}:{r:r>>16,g:r>>8&255,b:r&255}}function A({h:t,s:r,v:e,a:o}){let n,i,a;r=r/100,e=e/100,t=t/360;let s=Math.floor(t*6),b=t*6-s,p=e*(1-r),g=e*(1-b*r),c=e*(1-(1-b)*r);switch(s%6){case 0:n=e,i=c,a=p;break;case 1:n=g,i=e,a=p;break;case 2:n=p,i=e,a=c;break;case 3:n=p,i=g,a=e;break;case 4:n=c,i=p,a=e;break;case 5:n=e,i=p,a=g;break}return{r:Math.round(n*255),g:Math.round(i*255),b:Math.round(a*255),a:o}}function H({r:t,g:r,b:e,a:o}){let n=Math.max(t,r,e),i=Math.min(t,r,e),a=n-i,s=n===0?0:a/n,b=n/255,p;switch(n){case i:p=0;break;case t:p=r-e+a*(r<e?6:0),p/=6*a;break;case r:p=e-t+a*2,p/=6*a;break;case e:p=t-r+a*4,p/=6*a;break}return{h:Math.round(p*360),s:Math.round(s*100),v:Math.round(b*100),a:o}}function u(t){if(typeof t!="string")throw new TypeError("Expected a string");let r=t.replace(/ /g,""),e=_.exec(r);if(e===null)return E(r);let o={r:Math.min(255,parseInt(e[2],10)),g:Math.min(255,parseInt(e[3],10)),b:Math.min(255,parseInt(e[4],10))};if(e[1]){let n=parseFloat(e[5]);o.a=Math.min(1,isNaN(n)===!0?1:n)*100}return o}function X(t,r){if(typeof t!="string")throw new TypeError("Expected a string as color");if(typeof r!="number")throw new TypeError("Expected a numeric percent");let e=u(t),o=r<0?0:255,n=Math.abs(r)/100,i=e.r,a=e.g,s=e.b;return"#"+(16777216+(Math.round((o-i)*n)+i)*65536+(Math.round((o-a)*n)+a)*256+(Math.round((o-s)*n)+s)).toString(16).slice(1)}function k(t){if(typeof t!="string"&&(!t||t.r===void 0))throw new TypeError("Expected a string or a {r, g, b} object as color");let r=typeof t=="string"?u(t):t,e=r.r/255,o=r.g/255,n=r.b/255,i=e<=.03928?e/12.92:Math.pow((e+.055)/1.055,2.4),a=o<=.03928?o/12.92:Math.pow((o+.055)/1.055,2.4),s=n<=.03928?n/12.92:Math.pow((n+.055)/1.055,2.4);return .2126*i+.7152*a+.0722*s}function l(t){if(typeof t!="string"&&(!t||t.r===void 0))throw new TypeError("Expected a string or a {r, g, b} object as color");let r=typeof t=="string"?u(t):t;return(r.r*299+r.g*587+r.b*114)/1e3}function L(t,r){if(typeof t!="string"&&(!t||t.r===void 0))throw new TypeError("Expected a string or a {r, g, b[, a]} object as fgColor");if(typeof r!="string"&&(!r||r.r===void 0))throw new TypeError("Expected a string or a {r, g, b[, a]} object as bgColor");let e=typeof t=="string"?u(t):t,o=e.r/255,n=e.g/255,i=e.b/255,a=e.a!==void 0?e.a/100:1,s=typeof r=="string"?u(r):r,b=s.r/255,p=s.g/255,g=s.b/255,c=s.a!==void 0?s.a/100:1,d=a+c*(1-a),M=Math.round((o*a+b*c*(1-a))/d*255),R=Math.round((n*a+p*c*(1-a))/d*255),w=Math.round((i*a+g*c*(1-a))/d*255),f={r:M,g:R,b:w,a:Math.round(d*100)};return typeof t=="string"?h(f):f}function S(t,r){if(typeof t!="string")throw new TypeError("Expected a string as color");if(r===void 0||r<-1||r>1)throw new TypeError("Expected offset to be between -1 and 1");let{r:e,g:o,b:n,a:i}=u(t),a=i!==void 0?i/100:0;return h({r:e,g:o,b:n,a:Math.round(Math.min(1,Math.max(0,a+r))*100)})}return O(V);})();
|
|
2
|
+
//# sourceMappingURL=index.iife.min.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../index.ts"],
|
|
4
|
+
"sourcesContent": ["export type RGBA = Record<'r' | 'g' | 'b' | 'a', number>\r\nexport type RGB = Record<'r' | 'g' | 'b', number> & { a?: number }\r\nexport type HEX = string\r\nexport type HSVA = Record<'h' | 's' | 'v' | 'a', number>\r\n\r\nexport type RGBA_TEXT = string\r\nexport type HEX_TEXT = string\r\nexport type HSVA_TEXT = string\r\nexport type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT\r\n\r\nconst reRGBA = /^rgb(a)?\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3}),?([01]?\\.?\\d*?)?\\)$/\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HEX/A representation as a\r\n *\r\n * String (`#RRGGBB<AA>`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHex({ r, g, b, a }: RGBA): HEX {\r\n const alpha = a !== void 0\r\n\r\n r = Math.round(r)\r\n g = Math.round(g)\r\n b = Math.round(b)\r\n\r\n if (r > 255 || g > 255 || b > 255 || (alpha && a > 100)) {\r\n throw new TypeError('Expected 3 numbers below 256 (and optionally one below 100)')\r\n }\r\n\r\n if (alpha) {\r\n a = Number((Math.round((255 * a) / 100) | (1 << 8)).toString(16).slice(1))\r\n }\r\n\r\n return '#' + (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1) + a\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hexToRgb(hex: HEX): RGB {\r\n if (typeof hex !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n hex = hex.replace(/^#/, '')\r\n\r\n if (hex.length === 3) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]\r\n } else if (hex.length === 4) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3]\r\n }\r\n\r\n const num = parseInt(hex, 16)\r\n\r\n return hex.length > 6\r\n ? { r: (num >> 24) & 255, g: (num >> 16) & 255, b: (num >> 8) & 255, a: Math.round((num & 255) / 2.55) }\r\n : { r: num >> 16, g: (num >> 8) & 255, b: num & 255 }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hsvToRgb({ h, s, v, a }: HSVA): RGBA {\r\n let r: any, g: any, b: any\r\n s = s / 100\r\n v = v / 100\r\n\r\n h = h / 360\r\n const i = Math.floor(h * 6),\r\n f = h * 6 - i,\r\n p = v * (1 - s),\r\n q = v * (1 - f * s),\r\n t = v * (1 - (1 - f) * s)\r\n\r\n switch (i % 6) {\r\n case 0:\r\n r = v\r\n g = t\r\n b = p\r\n break\r\n case 1:\r\n r = q\r\n g = v\r\n b = p\r\n break\r\n case 2:\r\n r = p\r\n g = v\r\n b = t\r\n break\r\n case 3:\r\n r = p\r\n g = q\r\n b = v\r\n break\r\n case 4:\r\n r = t\r\n g = p\r\n b = v\r\n break\r\n case 5:\r\n r = v\r\n g = p\r\n b = q\r\n break\r\n }\r\n\r\n return {\r\n r: Math.round(r * 255),\r\n g: Math.round(g * 255),\r\n b: Math.round(b * 255),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HSV/A representation as an\r\n *\r\n * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-100]}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHsv({ r, g, b, a }: RGBA): HSVA {\r\n const max = Math.max(r, g, b),\r\n min = Math.min(r, g, b),\r\n d = max - min,\r\n s = max === 0 ? 0 : d / max,\r\n v = max / 255\r\n let h: any\r\n\r\n switch (max) {\r\n case min:\r\n h = 0\r\n break\r\n case r:\r\n h = g - b + d * (g < b ? 6 : 0)\r\n h /= 6 * d\r\n break\r\n case g:\r\n h = b - r + d * 2\r\n h /= 6 * d\r\n break\r\n case b:\r\n h = r - g + d * 4\r\n h /= 6 * d\r\n break\r\n }\r\n\r\n return {\r\n h: Math.round(h * 360),\r\n s: Math.round(s * 100),\r\n v: Math.round(v * 100),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function textToRgb(str: COLOR): RGB {\r\n if (typeof str !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n const color = str.replace(/ /g, '')\r\n\r\n const m = reRGBA.exec(color)\r\n\r\n if (m === null) {\r\n return hexToRgb(color)\r\n }\r\n\r\n const rgb: RGB = {\r\n r: Math.min(255, parseInt(m[2], 10)),\r\n g: Math.min(255, parseInt(m[3], 10)),\r\n b: Math.min(255, parseInt(m[4], 10))\r\n }\r\n\r\n if (m[1]) {\r\n const alpha = parseFloat(m[5])\r\n rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100\r\n }\r\n\r\n return rgb\r\n}\r\n\r\n/**\r\n * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).\r\n *\r\n * Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 100 or -100 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.\r\n */\r\nexport function lighten(color: COLOR, percent: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n if (typeof percent !== 'number') {\r\n throw new TypeError('Expected a numeric percent')\r\n }\r\n\r\n const rgb = textToRgb(color),\r\n t = percent < 0 ? 0 : 255,\r\n p = Math.abs(percent) / 100,\r\n R = rgb.r,\r\n G = rgb.g,\r\n B = rgb.b\r\n\r\n return (\r\n '#' +\r\n (\r\n 0x1_00_00_00 +\r\n (Math.round((t - R) * p) + R) * 0x1_00_00 +\r\n (Math.round((t - G) * p) + G) * 0x1_00 +\r\n (Math.round((t - B) * p) + B)\r\n )\r\n .toString(16)\r\n .slice(1)\r\n )\r\n}\r\n/**\r\n * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function luminosity(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color,\r\n r = rgb.r / 255,\r\n g = rgb.g / 255,\r\n b = rgb.b / 255,\r\n R = r <= 0.039_28 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4),\r\n G = g <= 0.039_28 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4),\r\n B = b <= 0.039_28 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4)\r\n return 0.2126 * R + 0.7152 * G + 0.0722 * B\r\n}\r\n/**\r\n * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function brightness(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color\r\n\r\n return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000\r\n}\r\n\r\n/**\r\n * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.\r\n *\r\n * Accepts a HEX/A String or a RGB/A Object as `fgColor`/`bgColor`. If the alpha channel of the `fgColor` is completely opaque, then the result will be the `fgColor`. If the alpha channel of the `bgColor` is completely opaque, then the resulting blended color will also be opaque. Returns the same type as input for fgColor.\r\n */\r\nexport function blend(fgColor: COLOR | RGB, bgColor: COLOR | RGB) {\r\n if (typeof fgColor !== 'string' && (!fgColor || fgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as fgColor')\r\n }\r\n\r\n if (typeof bgColor !== 'string' && (!bgColor || bgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as bgColor')\r\n }\r\n\r\n const rgb1 = typeof fgColor === 'string' ? textToRgb(fgColor) : fgColor,\r\n r1 = rgb1.r / 255,\r\n g1 = rgb1.g / 255,\r\n b1 = rgb1.b / 255,\r\n a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1,\r\n rgb2 = typeof bgColor === 'string' ? textToRgb(bgColor) : bgColor,\r\n r2 = rgb2.r / 255,\r\n g2 = rgb2.g / 255,\r\n b2 = rgb2.b / 255,\r\n a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1,\r\n a = a1 + a2 * (1 - a1),\r\n r = Math.round(((r1 * a1 + r2 * a2 * (1 - a1)) / a) * 255),\r\n g = Math.round(((g1 * a1 + g2 * a2 * (1 - a1)) / a) * 255),\r\n b = Math.round(((b1 * a1 + b2 * a2 * (1 - a1)) / a) * 255)\r\n\r\n const ret = { r, g, b, a: Math.round(a * 100) }\r\n return typeof fgColor === 'string' ? rgbToHex(ret) : ret\r\n}\r\n\r\n/**\r\n * Increments or decrements the alpha of a string color.\r\n *\r\n * Accepts a HEX/A String as color and a number between -1 and 1 (including edges) as offset. Use a negative value to decrement and a positive number to increment (ex: changeAlpha('#ff0000', -0.1) to decrement alpha by 10%). Returns HEX/A String.\r\n */\r\nexport function changeAlpha(color: COLOR, offset: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n\r\n if (offset === void 0 || offset < -1 || offset > 1) {\r\n throw new TypeError('Expected offset to be between -1 and 1')\r\n }\r\n\r\n const { r, g, b, a } = textToRgb(color)\r\n const alpha = a !== void 0 ? a / 100 : 0\r\n\r\n return rgbToHex({\r\n r,\r\n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zD,EAAI,KAAK,MAAQ,GAAK,EAAK,EAAK,EAAM,GAAI,IAAO,EAAK,GAAG,EACzD,EAAI,KAAK,MAAQ,GAAK,EAAK,EAAK,EAAM,GAAI,IAAO,EAAK,GAAG,EAErD,EAAM,CAAE,IAAG,IAAG,IAAG,EAAG,KAAK,MAAM,EAAI,GAAG,CAAE,EAC9C,MAAO,OAAO,IAAY,SAAW,EAAS,CAAG,EAAI,CACvD,CAOO,WAAqB,EAAc,EAAgB,CACxD,GAAI,MAAO,IAAU,SACnB,KAAM,IAAI,WAAU,4BAA4B,EAGlD,GAAI,IAAW,QAAU,EAAS,IAAM,EAAS,EAC/C,KAAM,IAAI,WAAU,wCAAwC,EAG9D,GAAM,CAAE,IAAG,IAAG,IAAG,KAAM,EAAU,CAAK,EAChC,EAAQ,IAAM,OAAS,EAAI,IAAM,EAEvC,MAAO,GAAS,CACd,IACA,IACA,IACA,EAAG,KAAK,MAAM,KAAK,IAAI,EAAG,KAAK,IAAI,EAAG,EAAQ,CAAM,CAAC,EAAI,GAAG,CAC9D,CAAC,CACH",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/index.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
category: 'Universal Utils'
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Color Palette
|
|
6
|
+
|
|
7
|
+
颜色的处理、转换、计算
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
pnpm add -D @hairy/palette
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage Functions
|
|
16
|
+
|
|
17
|
+
### rgbToHex({ r, g, b, a })
|
|
18
|
+
Converts a RGB/A color
|
|
19
|
+
|
|
20
|
+
Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
|
|
21
|
+
|
|
22
|
+
to its HEX/A representation as a
|
|
23
|
+
|
|
24
|
+
If Alpha channel is present in the original object it will be present also in the output.
|
|
25
|
+
|
|
26
|
+
### hexToRgb(hex)
|
|
27
|
+
Converts a HEX/A color
|
|
28
|
+
|
|
29
|
+
String (`#RRGGBB<AA>`)
|
|
30
|
+
|
|
31
|
+
to its RGB/A representation as an
|
|
32
|
+
|
|
33
|
+
Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .
|
|
34
|
+
|
|
35
|
+
If Alpha channel is present in the original object it will be present also in the output.
|
|
36
|
+
|
|
37
|
+
### rgbToHsv({ r, g, b, a })
|
|
38
|
+
Converts a RGB/A color
|
|
39
|
+
|
|
40
|
+
Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
|
|
41
|
+
|
|
42
|
+
to its HSV/A representation as an
|
|
43
|
+
|
|
44
|
+
Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-100]}`).
|
|
45
|
+
|
|
46
|
+
If Alpha channel is present in the original object it will be present also in the output.
|
|
47
|
+
|
|
48
|
+
### textToRgb(str)
|
|
49
|
+
Converts a HEX/A color
|
|
50
|
+
|
|
51
|
+
String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
|
|
52
|
+
|
|
53
|
+
to its RGB/A representation as an
|
|
54
|
+
|
|
55
|
+
Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
|
|
56
|
+
|
|
57
|
+
If Alpha channel is present in the original object it will be present also in the output.
|
|
58
|
+
|
|
59
|
+
### lighten(str, percent)
|
|
60
|
+
Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).
|
|
61
|
+
|
|
62
|
+
Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 100 or -100 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.
|
|
63
|
+
|
|
64
|
+
### luminosity(str)
|
|
65
|
+
Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.
|
|
66
|
+
|
|
67
|
+
Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
|
|
68
|
+
|
|
69
|
+
### brightness(str)
|
|
70
|
+
Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.
|
|
71
|
+
|
|
72
|
+
Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
|
|
73
|
+
|
|
74
|
+
### blend(fgColor, bgColor)
|
|
75
|
+
Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.
|
|
76
|
+
|
|
77
|
+
Accepts a HEX/A String or a RGB/A Object as `fgColor`/`bgColor`. If the alpha channel of the `fgColor` is completely opaque, then the result will be the `fgColor`. If the alpha channel of the `bgColor` is completely opaque, then the resulting blended color will also be opaque. Returns the same type as input for fgColor.
|
|
78
|
+
|
|
79
|
+
### changeAlpha(color, offset)
|
|
80
|
+
Increments or decrements the alpha of a string color.
|
|
81
|
+
|
|
82
|
+
Accepts a HEX/A String as color and a number between -1 and 1 (including edges) as offset. Use a negative value to decrement and a positive number to increment (ex: changeAlpha('#ff0000', -0.1) to decrement alpha by 10%). Returns HEX/A String.
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hairy/palette",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"main": "index.cjs.js",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"jsdelivr": "./index.iife.min.js"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "hairy build --esllpkg --type"
|
|
11
|
+
},
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"module": "./index.esm.js",
|
|
14
|
+
"unpkg": "./index.iife.min.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./index.esm.js",
|
|
18
|
+
"require": "./index.cjs.js"
|
|
19
|
+
},
|
|
20
|
+
"./*": "./*"
|
|
21
|
+
}
|
|
22
|
+
}
|