dev-classes 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/classes/Color.d.ts +5 -2
- package/dist/classes/Color.js +64 -52
- package/package.json +1 -1
package/dist/classes/Color.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type ColorRgba = [number, number, number, number];
|
|
2
2
|
export type ColorRgb = [number, number, number];
|
|
3
3
|
export type ColorHsla = Record<"h" | "s" | "l" | "a", number>;
|
|
4
|
+
export type TypeBrightness_OR = "BT601" | "BT709" | "BT2020";
|
|
4
5
|
export declare class Color {
|
|
5
6
|
private static componentToHex;
|
|
6
7
|
static getNumberRGB: (getComputedStyleRGB: string) => number[] | RegExpMatchArray;
|
|
@@ -44,16 +45,18 @@ export declare class Color {
|
|
|
44
45
|
* @param weight [0, 1]
|
|
45
46
|
*/
|
|
46
47
|
static mixColors(color1: ColorRgb, color2: ColorRgb, weight: number): ColorRgb;
|
|
47
|
-
static
|
|
48
|
+
static getRgbByTypeBrightness(type: TypeBrightness_OR): number[];
|
|
49
|
+
static computePerceivedBrightness(color: ColorRgb, type?: TypeBrightness_OR): number;
|
|
48
50
|
static getAverageColor(color1: ColorRgb, color2: ColorRgb): ColorRgb;
|
|
49
51
|
static getAccentColor(baseHsv: number[], baseColor: ColorRgb, elementColor: ColorRgb): ColorRgb;
|
|
50
52
|
static changeColorAccent(baseHsv: number[], accentHsv: number[], color: ColorRgb, isDarkTheme: boolean): ColorRgb;
|
|
51
53
|
static changeBrightness(color: ColorRgb, amount: number): ColorRgb;
|
|
54
|
+
static hexBrightness(hex: string, amount: number): any;
|
|
52
55
|
static getHexColorFromTelegramColor(color: number): string;
|
|
53
56
|
static getRgbColorFromTelegramColor(color: number): ColorRgb;
|
|
54
57
|
static getColorsFromWallPaper(wallPaper: any): string;
|
|
55
58
|
static rgbaToRgb(rgba: ColorRgba, bg: ColorRgb): ColorRgb;
|
|
56
|
-
static calculateLuminance(rgb: ColorRgb): number;
|
|
59
|
+
static calculateLuminance(rgb: ColorRgb, type?: TypeBrightness_OR): number;
|
|
57
60
|
static getTextColor(luminance: number): ColorRgb;
|
|
58
61
|
static calculateOpacity(luminance: number, targetContrast: number): number;
|
|
59
62
|
static clamp(v: number, min: number, max: number): number;
|
package/dist/classes/Color.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var
|
|
2
|
-
var b = (l, t, n) => t in l ?
|
|
3
|
-
var
|
|
4
|
-
const
|
|
1
|
+
var M = Object.defineProperty;
|
|
2
|
+
var b = (l, t, n) => t in l ? M(l, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : l[t] = n;
|
|
3
|
+
var p = (l, t, n) => (b(l, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
|
+
const o = class o {
|
|
5
5
|
/**
|
|
6
6
|
* https://stackoverflow.com/a/54070620/6758968
|
|
7
7
|
* r, g, b in [0, 255]
|
|
@@ -9,8 +9,8 @@ const a = class a {
|
|
|
9
9
|
*/
|
|
10
10
|
static rgbToHsv(t, n, e) {
|
|
11
11
|
t /= 255, n /= 255, e /= 255;
|
|
12
|
-
const r = Math.max(t, n, e),
|
|
13
|
-
return [60 * (
|
|
12
|
+
const r = Math.max(t, n, e), s = r - Math.min(t, n, e), a = s && (r === t ? (n - e) / s : r == n ? 2 + (e - t) / s : 4 + (t - n) / s);
|
|
13
|
+
return [60 * (a < 0 ? a + 6 : a), r && s / r, r];
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* https://stackoverflow.com/a/54024653/6758968
|
|
@@ -20,7 +20,7 @@ const a = class a {
|
|
|
20
20
|
* @returns r, g, b in [0, 255]
|
|
21
21
|
*/
|
|
22
22
|
static hsvToRgb(t, n, e) {
|
|
23
|
-
const r = (
|
|
23
|
+
const r = (s, a = (s + t / 60) % 6) => Math.round((e - e * n * Math.max(Math.min(a, 4 - a, 1), 0)) * 255);
|
|
24
24
|
return [r(5), r(3), r(1)];
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
@@ -28,14 +28,14 @@ const a = class a {
|
|
|
28
28
|
*/
|
|
29
29
|
static rgbaToHsla(t, n, e, r = 1) {
|
|
30
30
|
t /= 255, n /= 255, e /= 255;
|
|
31
|
-
const
|
|
31
|
+
const s = Math.max(t, n, e), a = Math.min(t, n, e);
|
|
32
32
|
let i = 0, c;
|
|
33
|
-
const h = (
|
|
34
|
-
if (
|
|
33
|
+
const h = (s + a) / 2;
|
|
34
|
+
if (s === a)
|
|
35
35
|
i = c = 0;
|
|
36
36
|
else {
|
|
37
|
-
const g =
|
|
38
|
-
switch (c = h > 0.5 ? g / (2 -
|
|
37
|
+
const g = s - a;
|
|
38
|
+
switch (c = h > 0.5 ? g / (2 - s - a) : g / (s + a), s) {
|
|
39
39
|
case t:
|
|
40
40
|
i = (n - e) / g + (n < e ? 6 : 0);
|
|
41
41
|
break;
|
|
@@ -67,20 +67,20 @@ const a = class a {
|
|
|
67
67
|
*/
|
|
68
68
|
static hslaToRgba(t, n, e, r) {
|
|
69
69
|
t /= 360, n /= 100, e /= 100;
|
|
70
|
-
let
|
|
70
|
+
let s, a, i;
|
|
71
71
|
if (n === 0)
|
|
72
|
-
|
|
72
|
+
s = a = i = e;
|
|
73
73
|
else {
|
|
74
|
-
const c = function(m,
|
|
75
|
-
return u < 0 && (u += 1), u > 1 && (u -= 1), u < 0.16666666666666666 ? m + (
|
|
74
|
+
const c = function(m, T, u) {
|
|
75
|
+
return u < 0 && (u += 1), u > 1 && (u -= 1), u < 0.16666666666666666 ? m + (T - m) * 6 * u : u < 0.5 ? T : u < 0.6666666666666666 ? m + (T - m) * (0.6666666666666666 - u) * 6 : m;
|
|
76
76
|
}, h = e < 0.5 ? e * (1 + n) : e + n - e * n, g = 2 * e - h;
|
|
77
|
-
|
|
77
|
+
s = c(g, h, t + 1 / 3), a = c(g, h, t), i = c(g, h, t - 1 / 3);
|
|
78
78
|
}
|
|
79
|
-
return [
|
|
79
|
+
return [s, a, i, r].map((c) => Math.round(c * 255));
|
|
80
80
|
}
|
|
81
81
|
static hslaStringToRgba(t) {
|
|
82
|
-
const n = t.slice(5, -1).split(", "), e = Number(n.pop()), r = n.map((
|
|
83
|
-
return
|
|
82
|
+
const n = t.slice(5, -1).split(", "), e = Number(n.pop()), r = n.map((s) => s.endsWith("%") ? +s.slice(0, -1) : +s);
|
|
83
|
+
return o.hslaToRgba(r[0], r[1], r[2], e);
|
|
84
84
|
}
|
|
85
85
|
static hexaToRgba(t) {
|
|
86
86
|
const n = [], e = t[0] === "#" ? 1 : 0;
|
|
@@ -97,66 +97,78 @@ const a = class a {
|
|
|
97
97
|
return n;
|
|
98
98
|
}
|
|
99
99
|
static hexToRgb(t) {
|
|
100
|
-
return
|
|
100
|
+
return o.hexaToRgba(t.slice(0, 7));
|
|
101
101
|
}
|
|
102
102
|
static hexaToHsla(t) {
|
|
103
|
-
const n =
|
|
104
|
-
return
|
|
103
|
+
const n = o.hexaToRgba(t);
|
|
104
|
+
return o.rgbaToHsla(n[0], n[1], n[2], n[3]);
|
|
105
105
|
}
|
|
106
106
|
static rgbaToHexa(t) {
|
|
107
107
|
const n = [...t], e = n.pop(), r = Math.round(Math.min(Math.max(e ?? 1, 0), 1) * 255);
|
|
108
|
-
return "#" + n.map((
|
|
108
|
+
return "#" + n.map((s) => ("0" + s.toString(16)).slice(-2)).join("") + r.toString(16);
|
|
109
109
|
}
|
|
110
110
|
static hslaStringToHexa(t) {
|
|
111
|
-
return
|
|
111
|
+
return o.rgbaToHexa(o.hslaStringToRgba(t));
|
|
112
112
|
}
|
|
113
113
|
static hslaStringToHex(t) {
|
|
114
|
-
return
|
|
114
|
+
return o.hslaStringToHexa(t).slice(0, -2);
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
117
|
* @param weight [0, 1]
|
|
118
118
|
*/
|
|
119
119
|
static mixColors(t, n, e) {
|
|
120
120
|
const r = new Array(3);
|
|
121
|
-
for (let
|
|
122
|
-
const
|
|
123
|
-
r[
|
|
121
|
+
for (let s = 0; s < 3; ++s) {
|
|
122
|
+
const a = t[s], i = n[s];
|
|
123
|
+
r[s] = Math.floor(i + (a - i) * e);
|
|
124
124
|
}
|
|
125
125
|
return r;
|
|
126
126
|
}
|
|
127
|
-
static
|
|
128
|
-
return
|
|
127
|
+
static getRgbByTypeBrightness(t) {
|
|
128
|
+
return {
|
|
129
|
+
BT601: [0.299, 0.587, 0.114],
|
|
130
|
+
BT709: [0.2126, 0.7152, 0.0722],
|
|
131
|
+
BT2020: [0.2627, 0.678, 0.0593]
|
|
132
|
+
}[t];
|
|
133
|
+
}
|
|
134
|
+
static computePerceivedBrightness(t, n = "BT709") {
|
|
135
|
+
const e = o.getRgbByTypeBrightness(n);
|
|
136
|
+
return (t[0] * e[n][0] + t[1] * e[n][1] + t[2] * e[n][2]) / 255;
|
|
129
137
|
}
|
|
130
138
|
static getAverageColor(t, n) {
|
|
131
139
|
return t.map((e, r) => Math.round((e + n[r]) / 2));
|
|
132
140
|
}
|
|
133
141
|
static getAccentColor(t, n, e) {
|
|
134
|
-
const r =
|
|
135
|
-
return r[0] = Math.min(360,
|
|
142
|
+
const r = o.rgbToHsv(...n), s = o.rgbToHsv(...e), a = Math.min(1.5 * r[1] / t[1], 1);
|
|
143
|
+
return r[0] = Math.min(360, s[0] - r[0] + t[0]), r[1] = Math.min(1, s[1] * t[1] / r[1]), r[2] = Math.min(1, (s[2] / r[2] + a - 1) * t[2] / a), r[2] < 0.3 ? e : o.hsvToRgb(...r);
|
|
136
144
|
}
|
|
137
145
|
static changeColorAccent(t, n, e, r) {
|
|
138
|
-
const
|
|
139
|
-
if (Math.min(Math.abs(
|
|
146
|
+
const s = o.rgbToHsv(...e);
|
|
147
|
+
if (Math.min(Math.abs(s[0] - t[0]), Math.abs(s[0] - t[0] - 360)) > 30)
|
|
140
148
|
return e;
|
|
141
|
-
const i = t[1] ? Math.min(1.5 *
|
|
142
|
-
|
|
143
|
-
let c =
|
|
144
|
-
const h =
|
|
149
|
+
const i = t[1] ? Math.min(1.5 * s[1] / t[1], 1) : 0;
|
|
150
|
+
s[0] = Math.min(360, s[0] + n[0] - t[0]), s[1] = t[1] ? Math.min(1, s[1] * n[1] / t[1]) : 0, s[2] = t[2] ? Math.min(1, s[2] * (1 - i + i * n[2] / t[2])) : 0;
|
|
151
|
+
let c = o.hsvToRgb(...s);
|
|
152
|
+
const h = o.computePerceivedBrightness(e), g = o.computePerceivedBrightness(c);
|
|
145
153
|
if (r ? h > g : h < g) {
|
|
146
|
-
const
|
|
147
|
-
c =
|
|
154
|
+
const T = 0.4 * h / g + 0.6;
|
|
155
|
+
c = o.changeBrightness(c, T);
|
|
148
156
|
}
|
|
149
157
|
return c;
|
|
150
158
|
}
|
|
151
159
|
static changeBrightness(t, n) {
|
|
152
|
-
return t.map((e) =>
|
|
160
|
+
return t.map((e) => o.clamp(Math.round(e * n), 0, 255));
|
|
161
|
+
}
|
|
162
|
+
static hexBrightness(t, n) {
|
|
163
|
+
const e = o.hexToRgb(t), r = o.changeBrightness(e, n), [s, a, i] = r;
|
|
164
|
+
return o.rgbToHex(s, a, i);
|
|
153
165
|
}
|
|
154
166
|
static getHexColorFromTelegramColor(t) {
|
|
155
167
|
const n = (t < 0 ? 16777215 + t : t).toString(16);
|
|
156
168
|
return "#" + (n.length >= 6 ? n : "0".repeat(6 - n.length) + n);
|
|
157
169
|
}
|
|
158
170
|
static getRgbColorFromTelegramColor(t) {
|
|
159
|
-
return
|
|
171
|
+
return o.hexToRgb(o.getHexColorFromTelegramColor(t));
|
|
160
172
|
}
|
|
161
173
|
static getColorsFromWallPaper(t) {
|
|
162
174
|
return t.settings ? [
|
|
@@ -164,15 +176,15 @@ const a = class a {
|
|
|
164
176
|
t.settings.second_background_color,
|
|
165
177
|
t.settings.third_background_color,
|
|
166
178
|
t.settings.fourth_background_color
|
|
167
|
-
].filter(Boolean).map(
|
|
179
|
+
].filter(Boolean).map(o.getHexColorFromTelegramColor).join(",") : "";
|
|
168
180
|
}
|
|
169
181
|
static rgbaToRgb(t, n) {
|
|
170
182
|
const e = t[3];
|
|
171
|
-
return t.slice(0, 3).map((r,
|
|
183
|
+
return t.slice(0, 3).map((r, s) => o.clamp(Math.round((e * (r / 255) + e * (n[s] / 255)) * 255), 0, 255));
|
|
172
184
|
}
|
|
173
|
-
static calculateLuminance(t) {
|
|
174
|
-
const [
|
|
175
|
-
return 0
|
|
185
|
+
static calculateLuminance(t, n = "BT709") {
|
|
186
|
+
const [e, r, s] = t, a = o.getRgbByTypeBrightness(n);
|
|
187
|
+
return a[0] * e / 255 + a[1] * r / 255 + a[2] * s / 255;
|
|
176
188
|
}
|
|
177
189
|
static getTextColor(t) {
|
|
178
190
|
return t > 0.5 ? [0, 0, 0] : [255, 255, 255];
|
|
@@ -186,14 +198,14 @@ const a = class a {
|
|
|
186
198
|
}
|
|
187
199
|
};
|
|
188
200
|
/*Проверить свои методы и возможно исключить т.к. функционал возможно повторяется */
|
|
189
|
-
|
|
201
|
+
p(o, "componentToHex", (t) => {
|
|
190
202
|
var n = t.toString(16);
|
|
191
203
|
return n.length == 1 ? "0" + n : n;
|
|
192
|
-
}),
|
|
204
|
+
}), p(o, "getNumberRGB", (t) => {
|
|
193
205
|
let n = t.match(/\d+/gi);
|
|
194
206
|
return n != null && n.length ? n.map((e) => Number(e)) : n || [255, 255, 255];
|
|
195
|
-
}),
|
|
196
|
-
let f =
|
|
207
|
+
}), p(o, "rgbToHex", (t, n, e) => "#" + o.componentToHex(t) + o.componentToHex(n) + o.componentToHex(e));
|
|
208
|
+
let f = o;
|
|
197
209
|
export {
|
|
198
210
|
f as Color
|
|
199
211
|
};
|