css-color-parser-h 3.0.4 → 3.0.5

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