@etsoo/shared 1.2.52 → 1.2.55
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/.github/workflows/main.yml +6 -5
- package/__tests__/ArrayUtils.ts +126 -126
- package/__tests__/ColorUtils.ts +24 -24
- package/__tests__/ContentDisposition.ts +18 -18
- package/__tests__/DataTypes.ts +222 -204
- package/__tests__/DateUtils.ts +82 -82
- package/__tests__/DomUtils.ts +352 -352
- package/__tests__/EHistory.ts +62 -62
- package/__tests__/ExtendUtils.ts +40 -40
- package/__tests__/Keyboard.ts +14 -14
- package/__tests__/NumberUtils.ts +37 -39
- package/__tests__/StorageUtils.ts +27 -27
- package/__tests__/Utils.ts +361 -363
- package/__tests__/tsconfig.json +15 -15
- package/lib/cjs/ActionResult.d.ts +1 -1
- package/lib/cjs/ActionResult.js +3 -3
- package/lib/cjs/ArrayUtils.d.ts +1 -1
- package/lib/cjs/ArrayUtils.js +4 -4
- package/lib/cjs/ColorUtils.d.ts +1 -1
- package/lib/cjs/ColorUtils.js +2 -2
- package/lib/cjs/DataTypes.d.ts +6 -6
- package/lib/cjs/DataTypes.js +50 -51
- package/lib/cjs/DateUtils.d.ts +1 -1
- package/lib/cjs/DateUtils.js +27 -28
- package/lib/cjs/DomUtils.d.ts +3 -3
- package/lib/cjs/DomUtils.js +64 -73
- package/lib/cjs/ExtendUtils.d.ts +1 -1
- package/lib/cjs/ExtendUtils.js +6 -6
- package/lib/cjs/IActionResult.d.ts +1 -1
- package/lib/cjs/NumberUtils.d.ts +1 -1
- package/lib/cjs/NumberUtils.js +9 -9
- package/lib/cjs/StorageUtils.js +2 -2
- package/lib/cjs/Utils.d.ts +4 -4
- package/lib/cjs/Utils.js +58 -62
- package/lib/cjs/index.d.ts +22 -22
- package/lib/cjs/storage/WindowStorage.d.ts +1 -1
- package/lib/cjs/types/ContentDisposition.d.ts +2 -2
- package/lib/cjs/types/ContentDisposition.js +11 -13
- package/lib/cjs/types/EColor.js +5 -7
- package/lib/cjs/types/EHistory.d.ts +3 -3
- package/lib/cjs/types/EHistory.js +4 -4
- package/lib/cjs/types/ErrorData.d.ts +1 -1
- package/lib/cjs/types/EventClass.js +1 -1
- package/lib/mjs/ActionResult.d.ts +1 -1
- package/lib/mjs/ActionResult.js +3 -3
- package/lib/mjs/ArrayUtils.d.ts +1 -1
- package/lib/mjs/ArrayUtils.js +5 -5
- package/lib/mjs/ColorUtils.d.ts +1 -1
- package/lib/mjs/ColorUtils.js +3 -3
- package/lib/mjs/DataTypes.d.ts +6 -6
- package/lib/mjs/DataTypes.js +50 -51
- package/lib/mjs/DateUtils.d.ts +1 -1
- package/lib/mjs/DateUtils.js +27 -28
- package/lib/mjs/DomUtils.d.ts +3 -3
- package/lib/mjs/DomUtils.js +67 -76
- package/lib/mjs/ExtendUtils.d.ts +1 -1
- package/lib/mjs/ExtendUtils.js +6 -6
- package/lib/mjs/IActionResult.d.ts +1 -1
- package/lib/mjs/NumberUtils.d.ts +1 -1
- package/lib/mjs/NumberUtils.js +9 -9
- package/lib/mjs/StorageUtils.js +4 -4
- package/lib/mjs/Utils.d.ts +4 -4
- package/lib/mjs/Utils.js +61 -65
- package/lib/mjs/index.d.ts +22 -22
- package/lib/mjs/index.js +22 -22
- package/lib/mjs/storage/WindowStorage.d.ts +1 -1
- package/lib/mjs/storage/WindowStorage.js +2 -2
- package/lib/mjs/types/ContentDisposition.d.ts +2 -2
- package/lib/mjs/types/ContentDisposition.js +12 -14
- package/lib/mjs/types/EColor.js +5 -7
- package/lib/mjs/types/EHistory.d.ts +3 -3
- package/lib/mjs/types/EHistory.js +5 -5
- package/lib/mjs/types/ErrorData.d.ts +1 -1
- package/lib/mjs/types/EventClass.js +1 -1
- package/package.json +61 -63
- package/src/ActionResult.ts +23 -23
- package/src/ArrayUtils.ts +164 -172
- package/src/ColorUtils.ts +80 -82
- package/src/DataTypes.ts +745 -754
- package/src/DateUtils.ts +266 -268
- package/src/DomUtils.ts +806 -831
- package/src/ExtendUtils.ts +191 -191
- package/src/IActionResult.ts +42 -42
- package/src/Keyboard.ts +258 -258
- package/src/NumberUtils.ts +135 -135
- package/src/StorageUtils.ts +117 -117
- package/src/Utils.ts +908 -930
- package/src/index.ts +22 -22
- package/src/node/Storage.ts +53 -53
- package/src/storage/IStorage.ts +62 -62
- package/src/storage/WindowStorage.ts +140 -140
- package/src/types/ContentDisposition.ts +59 -63
- package/src/types/DataError.ts +15 -15
- package/src/types/DelayedExecutorType.ts +15 -15
- package/src/types/EColor.ts +241 -248
- package/src/types/EHistory.ts +151 -151
- package/src/types/ErrorData.ts +11 -11
- package/src/types/EventClass.ts +220 -220
- package/src/types/FormData.ts +25 -25
- package/src/types/ParsedPath.ts +5 -5
- package/tsconfig.cjs.json +16 -16
- package/tsconfig.json +16 -16
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -29
- package/.prettierignore +0 -5
- package/.prettierrc +0 -6
package/src/types/EColor.ts
CHANGED
|
@@ -2,259 +2,252 @@
|
|
|
2
2
|
* Etsoo implmented Color
|
|
3
3
|
*/
|
|
4
4
|
export class EColor {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Adjust value
|
|
7
|
+
* @param value Current value
|
|
8
|
+
* @param adjust Adjust value
|
|
9
|
+
* @returns Adjusted value
|
|
10
|
+
*/
|
|
11
|
+
static adjust(value: number, adjust?: number) {
|
|
12
|
+
if (adjust == null) return value;
|
|
13
|
+
value = Math.abs(value + adjust);
|
|
14
|
+
if (value > 255) return value % 255;
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Format color
|
|
20
|
+
* @param input Input color text
|
|
21
|
+
* @param hexColor Format as Hex color
|
|
22
|
+
* @returns Result
|
|
23
|
+
*/
|
|
24
|
+
static format(input: string | undefined | null, hexColor?: boolean) {
|
|
25
|
+
// Null
|
|
26
|
+
if (input == null) return undefined;
|
|
27
|
+
|
|
28
|
+
// Like transparent, black, red
|
|
29
|
+
if (/^[a-zA-Z]+$/gi.test(input)) return input.toLowerCase();
|
|
30
|
+
|
|
31
|
+
const color = EColor.parse(input);
|
|
32
|
+
if (color == null) return undefined;
|
|
33
|
+
|
|
34
|
+
return hexColor ? color.toHEXColor() : color.toRGBColor();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* HEX string to integer value
|
|
39
|
+
* @param hex HEX string
|
|
40
|
+
* @returns Integer value
|
|
41
|
+
*/
|
|
42
|
+
static hexTo(hex: string) {
|
|
43
|
+
return parseInt(hex, 16);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Format value to 16 radix string
|
|
48
|
+
* @param num Int value
|
|
49
|
+
* @returns Result
|
|
50
|
+
*/
|
|
51
|
+
static toHex(num: number) {
|
|
52
|
+
return num.toString(16).padStart(2, "0");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Parse HTML color to EColor
|
|
57
|
+
* @param htmlColor HTML color
|
|
58
|
+
* @returns EColor
|
|
59
|
+
*/
|
|
60
|
+
static parse(htmlColor?: string | null): EColor | undefined {
|
|
61
|
+
// Null
|
|
62
|
+
if (htmlColor == null) return undefined;
|
|
63
|
+
htmlColor = htmlColor.trim().toUpperCase();
|
|
64
|
+
|
|
65
|
+
// HEX color
|
|
66
|
+
if (htmlColor.startsWith("#")) {
|
|
67
|
+
htmlColor = htmlColor.substring(1);
|
|
68
|
+
if (htmlColor.length === 3)
|
|
69
|
+
htmlColor = Array.from(htmlColor)
|
|
70
|
+
.map((c) => c + c)
|
|
71
|
+
.join("");
|
|
72
|
+
|
|
73
|
+
if (htmlColor.length === 6) {
|
|
74
|
+
return new EColor(
|
|
75
|
+
EColor.hexTo(htmlColor.substring(0, 2)),
|
|
76
|
+
EColor.hexTo(htmlColor.substring(2, 4)),
|
|
77
|
+
EColor.hexTo(htmlColor.substring(4, 6))
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return undefined;
|
|
16
82
|
}
|
|
17
83
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (color == null) return undefined;
|
|
33
|
-
|
|
34
|
-
return hexColor ? color.toHEXColor() : color.toRGBColor();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* HEX string to integer value
|
|
39
|
-
* @param hex HEX string
|
|
40
|
-
* @returns Integer value
|
|
41
|
-
*/
|
|
42
|
-
static hexTo(hex: string) {
|
|
43
|
-
return parseInt(hex, 16);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Format value to 16 radix string
|
|
48
|
-
* @param num Int value
|
|
49
|
-
* @returns Result
|
|
50
|
-
*/
|
|
51
|
-
static toHex(num: number) {
|
|
52
|
-
return num.toString(16).padStart(2, '0');
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Parse HTML color to EColor
|
|
57
|
-
* @param htmlColor HTML color
|
|
58
|
-
* @returns EColor
|
|
59
|
-
*/
|
|
60
|
-
static parse(htmlColor?: string | null): EColor | undefined {
|
|
61
|
-
// Null
|
|
62
|
-
if (htmlColor == null) return undefined;
|
|
63
|
-
htmlColor = htmlColor.trim().toUpperCase();
|
|
64
|
-
|
|
65
|
-
// HEX color
|
|
66
|
-
if (htmlColor.startsWith('#')) {
|
|
67
|
-
htmlColor = htmlColor.substring(1);
|
|
68
|
-
if (htmlColor.length === 3)
|
|
69
|
-
htmlColor = Array.from(htmlColor)
|
|
70
|
-
.map((c) => c + c)
|
|
71
|
-
.join('');
|
|
72
|
-
|
|
73
|
-
if (htmlColor.length === 6) {
|
|
74
|
-
return new EColor(
|
|
75
|
-
EColor.hexTo(htmlColor.substring(0, 2)),
|
|
76
|
-
EColor.hexTo(htmlColor.substring(2, 4)),
|
|
77
|
-
EColor.hexTo(htmlColor.substring(4, 6))
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return undefined;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// For RGB and RGBA
|
|
85
|
-
const reg = /^RGBA?\(([0-9,\s\.]+)\)$/;
|
|
86
|
-
const result = htmlColor.match(reg);
|
|
87
|
-
if (result != null && result.length == 2) {
|
|
88
|
-
const parts = result[1].split(/\s*,\s*/);
|
|
89
|
-
if (parts.length === 3 || parts.length === 4) {
|
|
90
|
-
const alpha = parts[3];
|
|
91
|
-
return new EColor(
|
|
92
|
-
parseInt(parts[0]),
|
|
93
|
-
parseInt(parts[1]),
|
|
94
|
-
parseInt(parts[2]),
|
|
95
|
-
alpha == null ? undefined : parseFloat(alpha)
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return undefined;
|
|
84
|
+
// For RGB and RGBA
|
|
85
|
+
const reg = /^RGBA?\(([0-9,\s\.]+)\)$/;
|
|
86
|
+
const result = htmlColor.match(reg);
|
|
87
|
+
if (result != null && result.length == 2) {
|
|
88
|
+
const parts = result[1].split(/\s*,\s*/);
|
|
89
|
+
if (parts.length === 3 || parts.length === 4) {
|
|
90
|
+
const alpha = parts[3];
|
|
91
|
+
return new EColor(
|
|
92
|
+
parseInt(parts[0]),
|
|
93
|
+
parseInt(parts[1]),
|
|
94
|
+
parseInt(parts[2]),
|
|
95
|
+
alpha == null ? undefined : parseFloat(alpha)
|
|
96
|
+
);
|
|
97
|
+
}
|
|
101
98
|
}
|
|
102
99
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Constructor
|
|
105
|
+
* @param r Reg
|
|
106
|
+
* @param g Green
|
|
107
|
+
* @param b Blue
|
|
108
|
+
* @param alpha Alpha
|
|
109
|
+
*/
|
|
110
|
+
constructor(
|
|
111
|
+
public readonly r: number,
|
|
112
|
+
public readonly g: number,
|
|
113
|
+
public readonly b: number,
|
|
114
|
+
public readonly alpha?: number
|
|
115
|
+
) {}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Clone color with adjustments
|
|
119
|
+
* @param adjustR Adjust R value
|
|
120
|
+
* @param adjustG Adjust G value
|
|
121
|
+
* @param adjustB Adjust B value
|
|
122
|
+
* @param alpha New alpha value
|
|
123
|
+
*/
|
|
124
|
+
clone(adjustR?: number, adjustG?: number, adjustB?: number, alpha?: number) {
|
|
125
|
+
const r = EColor.adjust(this.r, adjustR);
|
|
126
|
+
const g = EColor.adjust(this.g, adjustG);
|
|
127
|
+
const b = EColor.adjust(this.b, adjustB);
|
|
128
|
+
if (
|
|
129
|
+
r === this.r &&
|
|
130
|
+
g === this.g &&
|
|
131
|
+
b === this.b &&
|
|
132
|
+
(alpha == null || alpha === this.alpha)
|
|
133
|
+
)
|
|
134
|
+
return undefined;
|
|
135
|
+
return new EColor(r, g, b, alpha);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Get contrast ratio, a value between 0 and 1
|
|
140
|
+
* @param color Contrast color
|
|
141
|
+
*/
|
|
142
|
+
getContrastRatio(color: EColor) {
|
|
143
|
+
const lum1 = this.getLuminance();
|
|
144
|
+
const lum2 = color.getLuminance();
|
|
145
|
+
const brightest = Math.max(lum1, lum2);
|
|
146
|
+
const darkest = Math.min(lum1, lum2);
|
|
147
|
+
return (brightest + 0.05) / (darkest + 0.05);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Get Delta value (perceptible by human eyes)
|
|
152
|
+
* <= 1, Not perceptible by human eyes
|
|
153
|
+
* 1 - 2, Perceptible through close observation
|
|
154
|
+
* 2 - 10, Perceptible at a glance
|
|
155
|
+
* 11 - 49, Colors are more similar than opposite
|
|
156
|
+
* 100+, Colors are exact opposite
|
|
157
|
+
* @param color Contrast color
|
|
158
|
+
* @returns Value
|
|
159
|
+
*/
|
|
160
|
+
getDeltaValue(color: EColor) {
|
|
161
|
+
const labA = this.toLabValue();
|
|
162
|
+
const labB = color.toLabValue();
|
|
163
|
+
const deltaL = labA[0] - labB[0];
|
|
164
|
+
const deltaA = labA[1] - labB[1];
|
|
165
|
+
const deltaB = labA[2] - labB[2];
|
|
166
|
+
const c1 = Math.sqrt(labA[1] * labA[1] + labA[2] * labA[2]);
|
|
167
|
+
const c2 = Math.sqrt(labB[1] * labB[1] + labB[2] * labB[2]);
|
|
168
|
+
const deltaC = c1 - c2;
|
|
169
|
+
let deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC;
|
|
170
|
+
deltaH = deltaH < 0 ? 0 : Math.sqrt(deltaH);
|
|
171
|
+
const sc = 1.0 + 0.045 * c1;
|
|
172
|
+
const sh = 1.0 + 0.015 * c1;
|
|
173
|
+
const deltaLKlsl = deltaL / 1.0;
|
|
174
|
+
const deltaCkcsc = deltaC / sc;
|
|
175
|
+
const deltaHkhsh = deltaH / sh;
|
|
176
|
+
const i =
|
|
177
|
+
deltaLKlsl * deltaLKlsl +
|
|
178
|
+
deltaCkcsc * deltaCkcsc +
|
|
179
|
+
deltaHkhsh * deltaHkhsh;
|
|
180
|
+
return i < 0 ? 0 : Math.sqrt(i);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Get luminance
|
|
185
|
+
* Darker one has higher luminance
|
|
186
|
+
* https://dev.to/alvaromontoro/building-your-own-color-contrast-checker-4j7o
|
|
187
|
+
*/
|
|
188
|
+
getLuminance() {
|
|
189
|
+
const a = [this.r, this.g, this.b].map((v) => {
|
|
190
|
+
v /= 255;
|
|
191
|
+
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
|
|
192
|
+
});
|
|
193
|
+
return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* To HEX color string
|
|
198
|
+
* @returns HEX color string
|
|
199
|
+
*/
|
|
200
|
+
toHEXColor() {
|
|
201
|
+
return `#${EColor.toHex(this.r)}${EColor.toHex(this.g)}${EColor.toHex(
|
|
202
|
+
this.b
|
|
203
|
+
)}`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* To Lab value
|
|
208
|
+
* @returns Lab value
|
|
209
|
+
*/
|
|
210
|
+
toLabValue(): [number, number, number] {
|
|
211
|
+
let r = this.r / 255,
|
|
212
|
+
g = this.g / 255,
|
|
213
|
+
b = this.b / 255,
|
|
214
|
+
x,
|
|
215
|
+
y,
|
|
216
|
+
z;
|
|
217
|
+
r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
|
|
218
|
+
g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
|
|
219
|
+
b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
|
|
220
|
+
x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
|
|
221
|
+
y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1.0;
|
|
222
|
+
z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
|
|
223
|
+
x = x > 0.008856 ? Math.pow(x, 1 / 3) : 7.787 * x + 16 / 116;
|
|
224
|
+
y = y > 0.008856 ? Math.pow(y, 1 / 3) : 7.787 * y + 16 / 116;
|
|
225
|
+
z = z > 0.008856 ? Math.pow(z, 1 / 3) : 7.787 * z + 16 / 116;
|
|
226
|
+
return [116 * y - 16, 500 * (x - y), 200 * (y - z)];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* To RGB color string
|
|
231
|
+
* @param alpha Alpha value, false means ignore it
|
|
232
|
+
* @returns RGB color string
|
|
233
|
+
*/
|
|
234
|
+
toRGBColor(alpha?: boolean | number) {
|
|
235
|
+
// Decide
|
|
236
|
+
let includeAlpha: boolean,
|
|
237
|
+
alphaValue: number | undefined = this.alpha;
|
|
238
|
+
|
|
239
|
+
if (typeof alpha === "number") {
|
|
240
|
+
alphaValue = alpha;
|
|
241
|
+
includeAlpha = true;
|
|
242
|
+
} else if (alpha == null) {
|
|
243
|
+
includeAlpha = this.alpha != null;
|
|
244
|
+
} else {
|
|
245
|
+
includeAlpha = alpha;
|
|
201
246
|
}
|
|
202
247
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
* @returns HEX color string
|
|
206
|
-
*/
|
|
207
|
-
toHEXColor() {
|
|
208
|
-
return `#${EColor.toHex(this.r)}${EColor.toHex(this.g)}${EColor.toHex(
|
|
209
|
-
this.b
|
|
210
|
-
)}`;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* To Lab value
|
|
215
|
-
* @returns Lab value
|
|
216
|
-
*/
|
|
217
|
-
toLabValue(): [number, number, number] {
|
|
218
|
-
let r = this.r / 255,
|
|
219
|
-
g = this.g / 255,
|
|
220
|
-
b = this.b / 255,
|
|
221
|
-
x,
|
|
222
|
-
y,
|
|
223
|
-
z;
|
|
224
|
-
r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
|
|
225
|
-
g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
|
|
226
|
-
b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
|
|
227
|
-
x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
|
|
228
|
-
y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1.0;
|
|
229
|
-
z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
|
|
230
|
-
x = x > 0.008856 ? Math.pow(x, 1 / 3) : 7.787 * x + 16 / 116;
|
|
231
|
-
y = y > 0.008856 ? Math.pow(y, 1 / 3) : 7.787 * y + 16 / 116;
|
|
232
|
-
z = z > 0.008856 ? Math.pow(z, 1 / 3) : 7.787 * z + 16 / 116;
|
|
233
|
-
return [116 * y - 16, 500 * (x - y), 200 * (y - z)];
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* To RGB color string
|
|
238
|
-
* @param alpha Alpha value, false means ignore it
|
|
239
|
-
* @returns RGB color string
|
|
240
|
-
*/
|
|
241
|
-
toRGBColor(alpha?: boolean | number) {
|
|
242
|
-
// Decide
|
|
243
|
-
let includeAlpha: boolean,
|
|
244
|
-
alphaValue: number | undefined = this.alpha;
|
|
248
|
+
if (includeAlpha)
|
|
249
|
+
return `RGBA(${this.r}, ${this.g}, ${this.b}, ${alphaValue ?? 1})`;
|
|
245
250
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
includeAlpha = true;
|
|
249
|
-
} else if (alpha == null) {
|
|
250
|
-
includeAlpha = this.alpha != null;
|
|
251
|
-
} else {
|
|
252
|
-
includeAlpha = alpha;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (includeAlpha)
|
|
256
|
-
return `RGBA(${this.r}, ${this.g}, ${this.b}, ${alphaValue ?? 1})`;
|
|
257
|
-
|
|
258
|
-
return `RGB(${this.r}, ${this.g}, ${this.b})`;
|
|
259
|
-
}
|
|
251
|
+
return `RGB(${this.r}, ${this.g}, ${this.b})`;
|
|
252
|
+
}
|
|
260
253
|
}
|