css-color-parser-h 3.0.4 → 3.0.6

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.

Potentially problematic release.


This version of css-color-parser-h might be problematic. Click here for more details.

@@ -0,0 +1,933 @@
1
+ import convert from 'color-convert';
2
+
3
+ var Check = /** @class */ (function () {
4
+ function Check() {
5
+ }
6
+ Check.type = function (paramName, value, type) {
7
+ var valueType = typeof value;
8
+ if (valueType !== type) {
9
+ throw new Error("Expected ".concat(paramName, " to be typeof ").concat(type, ", actual typeof was ").concat(valueType));
10
+ }
11
+ };
12
+ Check.types = function (paramName, value, types) {
13
+ var valueType = typeof value;
14
+ var isContained = types.includes(valueType);
15
+ if (!isContained) {
16
+ throw new Error("Expected ".concat(paramName, " to be typeof ").concat(types.join('|'), ", actual typeof was ").concat(valueType));
17
+ }
18
+ };
19
+ Check.numValue = function (paramName, value, min, max) {
20
+ Check.numMinValue(paramName, value, min);
21
+ Check.numMaxValue(paramName, value, max);
22
+ };
23
+ Check.numMinValue = function (paramName, value, min) {
24
+ if (value < min) {
25
+ throw new Error("Expected ".concat(paramName, " to > ").concat(min, ", actual value was ").concat(value));
26
+ }
27
+ };
28
+ Check.numMaxValue = function (paramName, value, max) {
29
+ if (value > max) {
30
+ throw new Error("Expected ".concat(paramName, " to < ").concat(max, ", actual value was ").concat(value));
31
+ }
32
+ };
33
+ Check.numIsInt = function (paramName, value, expectIsInt) {
34
+ var isInt = Math.floor(value) === value;
35
+ if (isInt !== expectIsInt) {
36
+ throw new Error("Expected ".concat(paramName, " to ").concat(expectIsInt ? 'Integer' : 'Float', ", actual value was ").concat(isInt ? 'Integer' : 'Float'));
37
+ }
38
+ };
39
+ return Check;
40
+ }());
41
+ function defaultValue(v, defaultV) {
42
+ if (v === undefined || v === null) {
43
+ return defaultV;
44
+ }
45
+ if (isNaN(v) && typeof defaultV === 'number') {
46
+ return defaultV;
47
+ }
48
+ return v;
49
+ }
50
+ function limitNumber(min, max, v) {
51
+ if (v > max) {
52
+ v = max;
53
+ }
54
+ else if (v < min) {
55
+ v = min;
56
+ }
57
+ return v;
58
+ }
59
+ function setNumberPrecision(number, precision) {
60
+ if (number === void 0) { number = 0; }
61
+ var prec = Math.pow(10, precision);
62
+ return Math.round(number * prec) / prec;
63
+ }
64
+
65
+ /*
66
+ * @Author: roman_123
67
+ * @Description: 部分核心代码修改自cesium
68
+ * @Date: 2022-11-28 17:32:57
69
+ * @LastEditTime: 2022-12-06 00:02:21
70
+ */
71
+ var CssColorStringParser = /** @class */ (function () {
72
+ function CssColorStringParser() {
73
+ }
74
+ // 去除字符串内所有空格
75
+ CssColorStringParser.clearStrSpace = function (v) {
76
+ return v.replace(/\s/g, '');
77
+ };
78
+ // 去除字符串两边空格,将中间多个空格合并为一个
79
+ CssColorStringParser.trimStr = function (v) {
80
+ v = v.replace(/\s+/g, ' ');
81
+ return v.trim();
82
+ };
83
+ // 解析3位16进制 #fff #fff0
84
+ CssColorStringParser.parse3BitsHEX = function (v) {
85
+ var regx = /^#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i;
86
+ var res = regx.exec(v);
87
+ if (res) {
88
+ var res4 = defaultValue(res[4], 'f');
89
+ var r = CssColorStringParser._parseResStrForRgb(parseInt(res[1] + res[1], 16));
90
+ var g = CssColorStringParser._parseResStrForRgb(parseInt(res[2] + res[2], 16));
91
+ var b = CssColorStringParser._parseResStrForRgb(parseInt(res[3] + res[3], 16));
92
+ var a = CssColorStringParser._parsePercent(parseInt(res4 + res4, 16) / 255);
93
+ return [r, g, b, a];
94
+ }
95
+ return null;
96
+ };
97
+ // 解析6位16进制 #ffffff #ffffff00
98
+ CssColorStringParser.parse6BitsHEX = function (v) {
99
+ var regx = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i;
100
+ var res = regx.exec(v);
101
+ if (res) {
102
+ var res4 = defaultValue(res[4], 'ff');
103
+ var r = CssColorStringParser._parseResStrForRgb(parseInt(res[1], 16));
104
+ var g = CssColorStringParser._parseResStrForRgb(parseInt(res[2], 16));
105
+ var b = CssColorStringParser._parseResStrForRgb(parseInt(res[3], 16));
106
+ var a = CssColorStringParser._parsePercent(parseInt(res4, 16) / 255.0);
107
+ return [r, g, b, a];
108
+ }
109
+ return null;
110
+ };
111
+ // 解析rgb rgba rgb(255,255,255) rgba(255,255,255,1) rgba(100%,100%,100%, 1)
112
+ CssColorStringParser.parseRGBA = function (v) {
113
+ var regx = /^rgba?\(([0-9.]+%?),([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i;
114
+ var res = regx.exec(v);
115
+ if (res) {
116
+ var r = CssColorStringParser._parseResStrForRgb(res[1]);
117
+ var g = CssColorStringParser._parseResStrForRgb(res[2]);
118
+ var b = CssColorStringParser._parseResStrForRgb(res[3]);
119
+ var a = CssColorStringParser._parsePercent(res[4]);
120
+ return [r, g, b, a];
121
+ }
122
+ return null;
123
+ };
124
+ // 解析hsl hsla hsl(360,100%,100%) hsla(360,100%,100%,1)
125
+ CssColorStringParser.parseHSLA = function (v) {
126
+ var regx = /^hsla?\(([0-9.]+)(?:deg)?,([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i;
127
+ var res = regx.exec(v);
128
+ if (res) {
129
+ var h = CssColorStringParser._parseResStrForHue(res[1]);
130
+ var s = CssColorStringParser._parsePercent(res[2]);
131
+ var l = CssColorStringParser._parsePercent(res[3]);
132
+ var a = CssColorStringParser._parsePercent(res[4]);
133
+ return [h, s, l, a];
134
+ }
135
+ return null;
136
+ };
137
+ // 解析hwb
138
+ CssColorStringParser.parseHWB = function (v) {
139
+ var regx = /^hwb\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i;
140
+ var res = regx.exec(v);
141
+ if (res) {
142
+ var h = CssColorStringParser._parseResStrForHue(res[1]);
143
+ var w = CssColorStringParser._parsePercent(res[2]);
144
+ var b = CssColorStringParser._parsePercent(res[3]);
145
+ var a = CssColorStringParser._parsePercent(res[4]);
146
+ return [h, w, b, a];
147
+ }
148
+ return null;
149
+ };
150
+ // 解析rgb rgb(178 57 57 / 44%) 字符串中存在空格,因此使用清除两侧空格的原始字符串
151
+ CssColorStringParser.parseRGBA2 = function (v) {
152
+ var regx = /^rgba?\s?\(\s?([0-9.]+%?)\s?([0-9.]+%?)\s?([0-9.]+%?)(?:\s?\/\s?([0-9.]+%?))?\s?\)$/i;
153
+ var res = regx.exec(v);
154
+ if (res) {
155
+ var r = CssColorStringParser._parseResStrForRgb(res[1]);
156
+ var g = CssColorStringParser._parseResStrForRgb(res[2]);
157
+ var b = CssColorStringParser._parseResStrForRgb(res[3]);
158
+ var a = CssColorStringParser._parsePercent(res[4]);
159
+ return [r, g, b, a];
160
+ }
161
+ return null;
162
+ };
163
+ // 解析hsl hsl(215 85% 62% / 1)
164
+ CssColorStringParser.parseHSLA2 = function (v) {
165
+ var regx = /^hsla?\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i;
166
+ var res = regx.exec(v);
167
+ if (res) {
168
+ var h = CssColorStringParser._parseResStrForHue(res[1]);
169
+ var s = CssColorStringParser._parsePercent(res[2]);
170
+ var l = CssColorStringParser._parsePercent(res[3]);
171
+ var a = CssColorStringParser._parsePercent(res[4]);
172
+ return [h, s, l, a];
173
+ }
174
+ return null;
175
+ };
176
+ // 将结果解析为数字
177
+ CssColorStringParser._parseResStrForRgb = function (v) {
178
+ if (typeof v === 'string') {
179
+ v = parseFloat(v) / ('%' === v.substr(-1) ? 100.0 / 255 : 1);
180
+ }
181
+ if (isNaN(v)) {
182
+ v = 1;
183
+ }
184
+ return limitNumber(0, 255, v);
185
+ };
186
+ CssColorStringParser._parseResStrForHue = function (v) {
187
+ if (typeof v === 'string') {
188
+ v = parseFloat(v);
189
+ }
190
+ if (isNaN(v)) {
191
+ v = 0;
192
+ }
193
+ return limitNumber(0, 360, v);
194
+ };
195
+ CssColorStringParser._parsePercent = function (v) {
196
+ if (typeof v === 'string') {
197
+ v = parseFloat(v) / ('%' === v.substr(-1) ? 100.0 : 1);
198
+ }
199
+ if (isNaN(v)) {
200
+ v = 1;
201
+ }
202
+ return limitNumber(0, 1, v);
203
+ };
204
+ return CssColorStringParser;
205
+ }());
206
+
207
+ /*
208
+ * @Descripttion: 颜色解析器(轻量)
209
+ * @version: 1.0.0
210
+ * @Author: roman_123
211
+ * @Date: 2021-01-19 09:22:11
212
+ * @LastEditors: Please set LastEditors
213
+ * @LastEditTime: 2023-06-27 19:01:50
214
+ */
215
+ var CssColorParser = /** @class */ (function () {
216
+ function CssColorParser(red, green, blue, alpha) {
217
+ this.r = 255;
218
+ this.g = 255;
219
+ this.b = 255;
220
+ this.a = 1;
221
+ this._outColorPrecision = 2;
222
+ this._outAlphaPrecision = 2;
223
+ this.setColor(red, green, blue, alpha);
224
+ }
225
+ /**
226
+ * @description: 设置CssColorParser实例输出的精度
227
+ * @param {number} colorPrecision 输出颜色的精度
228
+ * @param {number} outAlphaPrecision 输出透明度的精度
229
+ * @return {CssColorParser}
230
+ */
231
+ CssColorParser.prototype.setOutPrecision = function (colorPrecision, outAlphaPrecision) {
232
+ Check.type('colorPrecision', colorPrecision, 'number');
233
+ Check.type('outAlphaPrecision', outAlphaPrecision, 'number');
234
+ Check.numMinValue('colorPrecision', colorPrecision, 0);
235
+ Check.numMinValue('outAlphaPrecision', outAlphaPrecision, 0);
236
+ Check.numIsInt('colorPrecision', colorPrecision, true);
237
+ Check.numIsInt('outAlphaPrecision', outAlphaPrecision, true);
238
+ this._outColorPrecision = colorPrecision;
239
+ this._outAlphaPrecision = outAlphaPrecision;
240
+ return this;
241
+ };
242
+ /**
243
+ * 设置颜色
244
+ * @param red
245
+ * @param green
246
+ * @param blue
247
+ * @param alpha
248
+ * @example: this.setColor(255,255,255,1)
249
+ */
250
+ CssColorParser.prototype.setColor = function (red, green, blue, alpha, isLimit) {
251
+ if (isLimit === void 0) { isLimit = true; }
252
+ if (isLimit) {
253
+ this.r = limitNumber(0, 255, defaultValue(Number(red), 0));
254
+ this.g = limitNumber(0, 255, defaultValue(Number(green), 0));
255
+ this.b = limitNumber(0, 255, defaultValue(Number(blue), 0));
256
+ this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1));
257
+ }
258
+ else {
259
+ this.r = defaultValue(Number(red), 0);
260
+ this.g = defaultValue(Number(green), 0);
261
+ this.b = defaultValue(Number(blue), 0);
262
+ this.a = defaultValue(Number(alpha), 1);
263
+ }
264
+ return this;
265
+ };
266
+ /**
267
+ * 设置颜色到范围内
268
+ * @example: this.limitColor()
269
+ */
270
+ CssColorParser.prototype.limitColor = function () {
271
+ return this.setColor(this.r, this.g, this.b, this.a, true);
272
+ };
273
+ /**
274
+ * @description: 设置透明度
275
+ * @param {number} alpha
276
+ * @return {CssColorParser}
277
+ */
278
+ CssColorParser.prototype.setAlpha = function (alpha) {
279
+ this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1));
280
+ return this;
281
+ };
282
+ /**
283
+ * @description: 设置红色值
284
+ * @param {number} red
285
+ * @return {CssColorParser}
286
+ */
287
+ CssColorParser.prototype.setRed = function (red) {
288
+ this.r = limitNumber(0, 255, defaultValue(Number(red), 0));
289
+ return this;
290
+ };
291
+ /**
292
+ * @description: 设置绿色值
293
+ * @param {number} green
294
+ * @return {CssColorParser}
295
+ */
296
+ CssColorParser.prototype.setGreen = function (green) {
297
+ this.g = limitNumber(0, 255, defaultValue(Number(green), 0));
298
+ return this;
299
+ };
300
+ /**
301
+ * @description: 设置蓝色值
302
+ * @param {number} blue
303
+ * @return {CssColorParser}
304
+ */
305
+ CssColorParser.prototype.setBlue = function (blue) {
306
+ this.b = limitNumber(0, 255, defaultValue(Number(blue), 0));
307
+ return this;
308
+ };
309
+ /**
310
+ * @description: 返回rgba格式的css字符串
311
+ * @return {string}
312
+ */
313
+ CssColorParser.prototype.toRGBA = function () {
314
+ var color = this.toJson();
315
+ if (color.a === 1) {
316
+ return "rgb(".concat(color.r, ",").concat(color.g, ",").concat(color.b, ")");
317
+ }
318
+ return "rgba(".concat(color.r, ",").concat(color.g, ",").concat(color.b, ",").concat(color.a, ")");
319
+ };
320
+ /**
321
+ * @description: 返回字符串
322
+ * @return {string}
323
+ */
324
+ CssColorParser.prototype.toString = function () {
325
+ return this.toRGBA();
326
+ };
327
+ /**
328
+ * @description: 归一化
329
+ * @return {array}
330
+ */
331
+ CssColorParser.prototype.toNormalize = function () {
332
+ var r = setNumberPrecision(this.r / 255, this._outColorPrecision);
333
+ var g = setNumberPrecision(this.g / 255, this._outColorPrecision);
334
+ var b = setNumberPrecision(this.b / 255, this._outColorPrecision);
335
+ var a = setNumberPrecision(this.a, this._outAlphaPrecision);
336
+ return [r, g, b, a];
337
+ };
338
+ /**
339
+ * @description: 返回16进制格式的css字符串
340
+ * @return {string}
341
+ */
342
+ CssColorParser.prototype.toHEX = function () {
343
+ var color = this.toJson();
344
+ var r = color.r.toString(16);
345
+ if (r.length < 2) {
346
+ r = "0".concat(r);
347
+ }
348
+ var g = color.g.toString(16);
349
+ if (g.length < 2) {
350
+ g = "0".concat(g);
351
+ }
352
+ var b = color.b.toString(16);
353
+ if (b.length < 2) {
354
+ b = "0".concat(b);
355
+ }
356
+ // 由于tojson后a会丢失精度,此处使用this.a
357
+ if (this.a < 1) {
358
+ var hexAlpha = parseInt((this.a * 255).toFixed()).toString(16);
359
+ if (hexAlpha.length < 2) {
360
+ hexAlpha = "0".concat(hexAlpha);
361
+ }
362
+ return "#".concat(r).concat(g).concat(b).concat(hexAlpha);
363
+ }
364
+ return "#".concat(r).concat(g).concat(b);
365
+ };
366
+ /**
367
+ * @description: 返回rgba数组
368
+ * @return {array}
369
+ */
370
+ CssColorParser.prototype.toArray = function () {
371
+ var color = this.toJson();
372
+ return [color.r, color.g, color.b, color.a];
373
+ };
374
+ /**
375
+ * @description: 返回ColorJson
376
+ * @return {ColorJson}
377
+ */
378
+ CssColorParser.prototype.toJson = function () {
379
+ return {
380
+ r: setNumberPrecision(this.r, this._outColorPrecision),
381
+ g: setNumberPrecision(this.g, this._outColorPrecision),
382
+ b: setNumberPrecision(this.b, this._outColorPrecision),
383
+ a: setNumberPrecision(this.a, this._outAlphaPrecision),
384
+ };
385
+ };
386
+ /**
387
+ * @description: 返回取反色后的新的实例
388
+ * @return {CssColorParser}
389
+ */
390
+ CssColorParser.prototype.toInvert = function () {
391
+ var r = 255 - this.r;
392
+ var g = 255 - this.g;
393
+ var b = 255 - this.b;
394
+ var a = 1 - this.a;
395
+ return new CssColorParser(r, g, b, a);
396
+ };
397
+ /**
398
+ * @description: 拷贝
399
+ * @return {CssColorParser}
400
+ */
401
+ CssColorParser.prototype.clone = function () {
402
+ return new CssColorParser(this.r, this.g, this.b, this.a);
403
+ };
404
+ /**
405
+ * @description: 比较两个解析对象的数据是否相等
406
+ * @param {string} color
407
+ * @return {boolean}
408
+ */
409
+ CssColorParser.prototype.equals = function (color) {
410
+ if (this === color) {
411
+ return true;
412
+ }
413
+ else {
414
+ var json1 = this.toJson();
415
+ var json2 = color.toJson();
416
+ return (json1.r === json2.r &&
417
+ json1.g === json2.g &&
418
+ json1.b === json2.g &&
419
+ json1.a === json2.a);
420
+ }
421
+ };
422
+ /**
423
+ * @description: 反色
424
+ * @return {CssColorParser}
425
+ */
426
+ CssColorParser.prototype.setInvert = function () {
427
+ this.r = 255 - this.r;
428
+ this.g = 255 - this.g;
429
+ this.b = 255 - this.b;
430
+ this.a = 1 - this.a;
431
+ return this;
432
+ };
433
+ /**
434
+ * @description: 乘以倍数
435
+ * @param {number} scalar
436
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
437
+ * @return {CssColorParser}
438
+ */
439
+ CssColorParser.prototype.multiplyByScalar = function (scalar, isSetAlpha) {
440
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
441
+ var r = this.r * scalar;
442
+ var g = this.g * scalar;
443
+ var b = this.b * scalar;
444
+ var a = isSetAlpha ? this.a * scalar : this.a;
445
+ return this.setColor(r, g, b, a, false);
446
+ };
447
+ /**
448
+ * @description: 除以倍数
449
+ * @param {number} scalar
450
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
451
+ * @return {CssColorParser}
452
+ */
453
+ CssColorParser.prototype.divideByScalar = function (scalar, isSetAlpha) {
454
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
455
+ var r = this.r / scalar;
456
+ var g = this.g / scalar;
457
+ var b = this.b / scalar;
458
+ var a = isSetAlpha ? this.a / scalar : this.a;
459
+ return this.setColor(r, g, b, a, false);
460
+ };
461
+ /**
462
+ * @description: 实例相加
463
+ * @param {CssColorParser} colorParser
464
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
465
+ * @return {CssColorParser}
466
+ */
467
+ CssColorParser.prototype.add = function (colorParser, isSetAlpha) {
468
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
469
+ var r = this.r + colorParser.r;
470
+ var g = this.g + colorParser.g;
471
+ var b = this.b + colorParser.b;
472
+ var a = isSetAlpha ? this.a + colorParser.a : this.a;
473
+ return this.setColor(r, g, b, a, false);
474
+ };
475
+ /**
476
+ * @description: 实例相减
477
+ * @param {CssColorParser} colorParser
478
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
479
+ * @return {CssColorParser}
480
+ */
481
+ CssColorParser.prototype.subtract = function (colorParser, isSetAlpha) {
482
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
483
+ var r = this.r - colorParser.r;
484
+ var g = this.g - colorParser.g;
485
+ var b = this.b - colorParser.b;
486
+ var a = isSetAlpha ? this.a - colorParser.a : this.a;
487
+ return this.setColor(r, g, b, a, false);
488
+ };
489
+ /**
490
+ * @description: 实例相乘
491
+ * @param {CssColorParser} colorParser
492
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
493
+ * @return {CssColorParser}
494
+ */
495
+ CssColorParser.prototype.multiply = function (colorParser, isSetAlpha) {
496
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
497
+ var r = this.r * colorParser.r;
498
+ var g = this.g * colorParser.g;
499
+ var b = this.b * colorParser.b;
500
+ var a = isSetAlpha ? this.a * colorParser.a : this.a;
501
+ return this.setColor(r, g, b, a, false);
502
+ };
503
+ /**
504
+ * @description: 实例相除
505
+ * @param {CssColorParser} colorParser
506
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
507
+ * @return {CssColorParser}
508
+ */
509
+ CssColorParser.prototype.divide = function (colorParser, isSetAlpha) {
510
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
511
+ var r = this.r / colorParser.r;
512
+ var g = this.g / colorParser.g;
513
+ var b = this.b / colorParser.b;
514
+ var a = isSetAlpha ? this.a / colorParser.a : this.a;
515
+ return this.setColor(r, g, b, a, false);
516
+ };
517
+ /**
518
+ * @description: 颜色RGB加上数字
519
+ * @param {number} num
520
+ * @return {CssColorParser}
521
+ */
522
+ CssColorParser.prototype.addNumberForRGB = function (num) {
523
+ this.r = this.r + num;
524
+ this.g = this.g + num;
525
+ this.b = this.b + num;
526
+ return this;
527
+ };
528
+ /**
529
+ * @description: 透明度加上数字
530
+ * @param {number} num
531
+ * @return {CssColorParser}
532
+ */
533
+ CssColorParser.prototype.addNumberForAlpha = function (num) {
534
+ this.a = this.a + num;
535
+ return this;
536
+ };
537
+ /**
538
+ * @description: 解析16进制颜色
539
+ * @param {string} v
540
+ * @return {CssColorParser}
541
+ * @example: CssColorParser.parseHEX('#FFF')
542
+ */
543
+ CssColorParser.parseHEX = function (v) {
544
+ var cssStr = CssColorStringParser.clearStrSpace(v);
545
+ var res = CssColorStringParser.parse3BitsHEX(cssStr);
546
+ if (!res) {
547
+ res = CssColorStringParser.parse6BitsHEX(cssStr);
548
+ }
549
+ return res && CssColorParser.fromArray(res);
550
+ };
551
+ /**
552
+ * @description: 解析rgba、rgb颜色
553
+ * @param {string} v
554
+ * @return {CssColorParser}
555
+ * @example: CssColorParser.parseRGBA('rgba(255,255,255,1)')
556
+ */
557
+ CssColorParser.parseRGBA = function (v) {
558
+ var cssStr = CssColorStringParser.clearStrSpace(v);
559
+ var res = CssColorStringParser.parseRGBA(cssStr);
560
+ if (!res) {
561
+ var cssStr2 = CssColorStringParser.trimStr(v);
562
+ res = CssColorStringParser.parseRGBA2(cssStr2);
563
+ }
564
+ return res && CssColorParser.fromArray(res);
565
+ };
566
+ /**
567
+ * @description: 将ColorJson格式的json数据转换为解析对象
568
+ * @param {ColorJson} json
569
+ * @return {CssColorParser}
570
+ * @example: CssColorParser.fromJson({r: 255, g: 255, b: 255, a: 1})
571
+ */
572
+ CssColorParser.fromJson = function (json) {
573
+ return new CssColorParser(json.r, json.g, json.b, json.a);
574
+ };
575
+ /**
576
+ * @description: 将RGBA数组转换为解析对象
577
+ * @param {Array} color
578
+ * @return {CssColorParser}
579
+ * @example: CssColorParser.fromArray([255,255,255,1])
580
+ */
581
+ CssColorParser.fromArray = function (color) {
582
+ return new CssColorParser(color[0], color[1], color[2], color[3]);
583
+ };
584
+ /**
585
+ * @description: 产生随机颜色
586
+ * @return {CssColorParser}
587
+ * @example: CssColorParser.fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
588
+ */
589
+ CssColorParser.fromRandom = function (color1, color2) {
590
+ var r = Math.random() * Math.abs(color2.r - color1.r) +
591
+ Math.min(color1.r, color2.r);
592
+ var g = Math.random() * Math.abs(color2.g - color1.g) +
593
+ Math.min(color1.g, color2.g);
594
+ var b = Math.random() * Math.abs(color2.b - color1.b) +
595
+ Math.min(color1.b, color2.b);
596
+ var a = Math.random() * Math.abs(color2.a - color1.a) +
597
+ Math.min(color1.a, color2.a);
598
+ return new CssColorParser(r, g, b, a);
599
+ };
600
+ /**
601
+ * @description: 颜色序列化数组转换为CssColorParser对象实例
602
+ * @param {array} colorArr
603
+ * @example: CssColorParser.fromNormaliz([1, 0, 0, 1])
604
+ */
605
+ CssColorParser.fromNormalize = function (colorArr) {
606
+ var r = colorArr[0] * 255;
607
+ var g = colorArr[1] * 255;
608
+ var b = colorArr[2] * 255;
609
+ var a = colorArr[3];
610
+ return CssColorParser.fromArray([r, g, b, a]);
611
+ };
612
+ return CssColorParser;
613
+ }());
614
+
615
+ /******************************************************************************
616
+ Copyright (c) Microsoft Corporation.
617
+
618
+ Permission to use, copy, modify, and/or distribute this software for any
619
+ purpose with or without fee is hereby granted.
620
+
621
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
622
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
623
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
624
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
625
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
626
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
627
+ PERFORMANCE OF THIS SOFTWARE.
628
+ ***************************************************************************** */
629
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
630
+
631
+ var extendStatics = function(d, b) {
632
+ extendStatics = Object.setPrototypeOf ||
633
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
634
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
635
+ return extendStatics(d, b);
636
+ };
637
+
638
+ function __extends(d, b) {
639
+ if (typeof b !== "function" && b !== null)
640
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
641
+ extendStatics(d, b);
642
+ function __() { this.constructor = d; }
643
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
644
+ }
645
+
646
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
647
+ var e = new Error(message);
648
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
649
+ };
650
+
651
+ var CssColorParserPlus = /** @class */ (function (_super) {
652
+ __extends(CssColorParserPlus, _super);
653
+ function CssColorParserPlus() {
654
+ return _super !== null && _super.apply(this, arguments) || this;
655
+ }
656
+ /**
657
+ * @description: 返回取反色后的新的实例
658
+ * @return {CssColorParserPlus}
659
+ */
660
+ CssColorParserPlus.prototype.toInvert = function () {
661
+ var r = 255 - this.r;
662
+ var g = 255 - this.g;
663
+ var b = 255 - this.b;
664
+ var a = 1 - this.a;
665
+ return new CssColorParserPlus(r, g, b, a);
666
+ };
667
+ /**
668
+ * @description: 拷贝
669
+ * @return {CssColorParserPlus}
670
+ */
671
+ CssColorParserPlus.prototype.clone = function () {
672
+ return new CssColorParserPlus(this.r, this.g, this.b, this.a);
673
+ };
674
+ /**
675
+ * @description: 比较两个解析对象的数据是否相等
676
+ * @param {string} color
677
+ * @return {boolean}
678
+ */
679
+ CssColorParserPlus.prototype.equals = function (color) {
680
+ color = CssColorParserPlus.parseColor(color);
681
+ if (this === color) {
682
+ return true;
683
+ }
684
+ else {
685
+ var json1 = this.toJson();
686
+ var json2 = color.toJson();
687
+ return (json1.r === json2.r &&
688
+ json1.g === json2.g &&
689
+ json1.b === json2.g &&
690
+ json1.a === json2.a);
691
+ }
692
+ };
693
+ /**
694
+ * @description: 实例相加
695
+ * @param {CssColorParser} colorParser
696
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
697
+ * @return {CssColorParser}
698
+ */
699
+ CssColorParserPlus.prototype.add = function (color, isSetAlpha) {
700
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
701
+ var colorParser = CssColorParserPlus.parseColor(color);
702
+ return _super.prototype.add.call(this, colorParser, isSetAlpha);
703
+ };
704
+ /**
705
+ * @description: 实例相减
706
+ * @param {CssColorParser} colorParser
707
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
708
+ * @return {CssColorParser}
709
+ */
710
+ CssColorParserPlus.prototype.subtract = function (color, isSetAlpha) {
711
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
712
+ var colorParser = CssColorParserPlus.parseColor(color);
713
+ return _super.prototype.subtract.call(this, colorParser, isSetAlpha);
714
+ };
715
+ /**
716
+ * @description: 实例相乘
717
+ * @param {CssColorParser} colorParser
718
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
719
+ * @return {CssColorParser}
720
+ */
721
+ CssColorParserPlus.prototype.multiply = function (color, isSetAlpha) {
722
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
723
+ var colorParser = CssColorParserPlus.parseColor(color);
724
+ return _super.prototype.multiply.call(this, colorParser, isSetAlpha);
725
+ };
726
+ /**
727
+ * @description: 实例相除
728
+ * @param {CssColorParser} colorParser
729
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
730
+ * @return {CssColorParser}
731
+ */
732
+ CssColorParserPlus.prototype.divide = function (color, isSetAlpha) {
733
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
734
+ var colorParser = CssColorParserPlus.parseColor(color);
735
+ return _super.prototype.divide.call(this, colorParser, isSetAlpha);
736
+ };
737
+ /**
738
+ * @description: 解析css颜色
739
+ * @param {string} v
740
+ * @return {CssColorParserPlus}
741
+ * @example: parseCssColorStr('rgba(255,255,255,1)')
742
+ */
743
+ CssColorParserPlus.parseColor = function (v) {
744
+ if (v instanceof CssColorParser) {
745
+ return v;
746
+ }
747
+ return CssColorParserPlus.parseCssColorStr(v);
748
+ };
749
+ /**
750
+ * @description: 将css字符串转换为解析对象
751
+ * @param {string} v
752
+ * @return {CssColorParserPlus}
753
+ * @example: parseCssColorStr('rgba(255,255,255,1)')
754
+ */
755
+ CssColorParserPlus.parseCssColorStr = function (v) {
756
+ Check.type('color', v, 'string');
757
+ return (CssColorParserPlus.parseHEX(v) ||
758
+ CssColorParserPlus.parseRGBA(v) ||
759
+ CssColorParserPlus.parseKeyWord(v) ||
760
+ CssColorParserPlus.parseHSLA(v) ||
761
+ CssColorParserPlus.parseHWB(v));
762
+ };
763
+ /**
764
+ * @description: 解析颜色关键字
765
+ * @param {string} v
766
+ * @return {CssColorParserPlus}
767
+ * @example: parseKeyWord('red')
768
+ */
769
+ CssColorParserPlus.parseKeyWord = function (v) {
770
+ var cssStr = CssColorStringParser.clearStrSpace(v);
771
+ var res = convert.keyword.rgb(cssStr);
772
+ return res && CssColorParserPlus.fromArray(res);
773
+ };
774
+ /**
775
+ * @description: 解析HSLA
776
+ * @param {string} v
777
+ * @return {CssColorParserPlus}
778
+ * @example: parseHSLA('hsla(215,85%,62%,0.8)')
779
+ */
780
+ CssColorParserPlus.parseHSLA = function (v) {
781
+ var cssStr = CssColorStringParser.clearStrSpace(v);
782
+ var res = CssColorStringParser.parseHSLA(cssStr);
783
+ if (!res) {
784
+ var cssStr2 = CssColorStringParser.trimStr(v);
785
+ res = CssColorStringParser.parseHSLA2(cssStr2);
786
+ }
787
+ return res && CssColorParserPlus.fromHSL(res[0], res[1], res[2], res[3]);
788
+ };
789
+ /**
790
+ * @description: 解析HWB
791
+ * @param {string} v
792
+ * @return {CssColorParserPlus}
793
+ * @example: parseHWB('hwb(215deg 30% 6% / 80%)')
794
+ */
795
+ CssColorParserPlus.parseHWB = function (v) {
796
+ var cssStr2 = CssColorStringParser.trimStr(v);
797
+ var res = CssColorStringParser.parseHWB(cssStr2);
798
+ return res && CssColorParserPlus.fromHWB(res[0], res[1], res[2], res[3]);
799
+ };
800
+ /**
801
+ * @description: 将HSL色彩模式转换为解析对象
802
+ * @param {number} hue 色相
803
+ * @param {number} saturation 饱和度
804
+ * @param {number} lightness 亮度
805
+ * @param {number} alpha 不透明度
806
+ * @return {CssColorParserPlus}
807
+ * @example: fromHSL(0,1,1,1)
808
+ */
809
+ CssColorParserPlus.fromHSL = function (h, s, l, a) {
810
+ var res = convert.hsl.rgb(limitNumber(0, 360, h), limitNumber(0, 100, s * 100), limitNumber(0, 100, l * 100));
811
+ return new CssColorParserPlus(res[0], res[1], res[2], defaultValue(Number(a), 1));
812
+ };
813
+ /**
814
+ * @description: 将HWB色彩模式转换为解析对象
815
+ * @param {number} h 色调
816
+ * @param {number} w 白度
817
+ * @param {number} b 黑度
818
+ * @param {number} a 不透明度
819
+ * @return {CssColorParserPlus}
820
+ * @example: fromHSL(0,1,1,1)
821
+ */
822
+ CssColorParserPlus.fromHWB = function (h, w, b, a) {
823
+ var res = convert.hwb.rgb(limitNumber(0, 360, h), limitNumber(0, 100, w * 100), limitNumber(0, 100, b * 100));
824
+ return new CssColorParserPlus(res[0], res[1], res[2], defaultValue(Number(a), 1));
825
+ };
826
+ /**
827
+ * @description: 解析16进制颜色
828
+ * @param {string} v
829
+ * @return {CssColorParserPlus}
830
+ * @example: CssColorParserPlus.parseHEX('#FFF')
831
+ */
832
+ CssColorParserPlus.parseHEX = function (v) {
833
+ var cssStr = CssColorStringParser.clearStrSpace(v);
834
+ var res = CssColorStringParser.parse3BitsHEX(cssStr);
835
+ if (!res) {
836
+ res = CssColorStringParser.parse6BitsHEX(cssStr);
837
+ }
838
+ return res && CssColorParserPlus.fromArray(res);
839
+ };
840
+ /**
841
+ * @description: 解析rgba、rgb颜色
842
+ * @param {string} v
843
+ * @return {CssColorParserPlus}
844
+ * @example: CssColorParserPlus.parseRGBA('rgba(255,255,255,1)')
845
+ */
846
+ CssColorParserPlus.parseRGBA = function (v) {
847
+ var cssStr = CssColorStringParser.clearStrSpace(v);
848
+ var res = CssColorStringParser.parseRGBA(cssStr);
849
+ if (!res) {
850
+ var cssStr2 = CssColorStringParser.trimStr(v);
851
+ res = CssColorStringParser.parseRGBA2(cssStr2);
852
+ }
853
+ return res && CssColorParserPlus.fromArray(res);
854
+ };
855
+ /**
856
+ * @description: 将ColorJson格式的json数据转换为解析对象
857
+ * @param {ColorJson} json
858
+ * @return {CssColorParserPlus}
859
+ * @example: CssColorParserPlus.fromJson({r: 255, g: 255, b: 255, a: 1})
860
+ */
861
+ CssColorParserPlus.fromJson = function (json) {
862
+ return new CssColorParserPlus(json.r, json.g, json.b, json.a);
863
+ };
864
+ /**
865
+ * @description: 将RGBA数组转换为解析对象
866
+ * @param {Array} color
867
+ * @return {CssColorParserPlus}
868
+ * @example: CssColorParserPlus.fromArray([255,255,255,1])
869
+ */
870
+ CssColorParserPlus.fromArray = function (color) {
871
+ return new CssColorParserPlus(color[0], color[1], color[2], color[3]);
872
+ };
873
+ /**
874
+ * @description: 产生随机颜色
875
+ * @return {CssColorParserPlus}
876
+ * @example: CssColorParserPlus.fromRandom('black', new CssColorParserPlus(255,255,255,1))
877
+ */
878
+ CssColorParserPlus.fromRandom = function (color1, color2) {
879
+ color1 = CssColorParserPlus.parseColor(color1);
880
+ color2 = CssColorParserPlus.parseColor(color2);
881
+ var r = Math.random() * Math.abs(color2.r - color1.r) +
882
+ Math.min(color1.r, color2.r);
883
+ var g = Math.random() * Math.abs(color2.g - color1.g) +
884
+ Math.min(color1.g, color2.g);
885
+ var b = Math.random() * Math.abs(color2.b - color1.b) +
886
+ Math.min(color1.b, color2.b);
887
+ var a = Math.random() * Math.abs(color2.a - color1.a) +
888
+ Math.min(color1.a, color2.a);
889
+ return new CssColorParserPlus(r, g, b, a);
890
+ };
891
+ /**
892
+ * @description: 颜色序列化数组转换为CssColorParserPlus对象实例
893
+ * @param {array} colorArr
894
+ * @example: CssColorParserPlus.fromNormaliz([1, 0, 0, 1])
895
+ */
896
+ CssColorParserPlus.fromNormalize = function (colorArr) {
897
+ var r = colorArr[0] * 255;
898
+ var g = colorArr[1] * 255;
899
+ var b = colorArr[2] * 255;
900
+ var a = colorArr[3];
901
+ return CssColorParserPlus.fromArray([r, g, b, a]);
902
+ };
903
+ /**
904
+ * @description: 获取颜色插值
905
+ * @param opt ColorLerpOpt
906
+ * @param value number
907
+ * @returns {CssColorParserPlus}
908
+ * @example: CssColorParserPlus.lerp({0: "rgba(255, 255, 0, 1)",0.5: "rgba(0, 255, 0, 1)",1: "rgba(255, 0, 0, 1)"}, 0.8)
909
+ */
910
+ CssColorParserPlus.lerp = function (opt, value) {
911
+ var keys = Object.keys(opt).map(function (k) { return Number(k); });
912
+ keys.sort(function (a, b) { return a - b; });
913
+ for (var i = 0; i < keys.length; i++) {
914
+ if (value <= keys[i]) {
915
+ var last = keys[i - 1];
916
+ var next = keys[i];
917
+ if (last === undefined) {
918
+ return CssColorParserPlus.parseColor(opt[next]);
919
+ }
920
+ else {
921
+ var ratio = (value - last) / (next - last);
922
+ var color1 = CssColorParserPlus.parseColor(opt[last]);
923
+ var color2 = CssColorParserPlus.parseColor(opt[next]);
924
+ return color2.subtract(color1).multiplyByScalar(ratio).add(color1);
925
+ }
926
+ }
927
+ }
928
+ return CssColorParserPlus.parseColor(opt[keys[keys.length - 1]]).limitColor();
929
+ };
930
+ return CssColorParserPlus;
931
+ }(CssColorParser));
932
+
933
+ export { CssColorParser, CssColorParserPlus };