css-color-parser-h 3.0.3 → 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,890 @@
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) {
251
+ this.r = limitNumber(0, 255, defaultValue(Number(red), 0));
252
+ this.g = limitNumber(0, 255, defaultValue(Number(green), 0));
253
+ this.b = limitNumber(0, 255, defaultValue(Number(blue), 0));
254
+ this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1));
255
+ return this;
256
+ };
257
+ /**
258
+ * @description: 设置透明度
259
+ * @param {number} alpha
260
+ * @return {CssColorParser}
261
+ */
262
+ CssColorParser.prototype.setAlpha = function (alpha) {
263
+ this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1));
264
+ return this;
265
+ };
266
+ /**
267
+ * @description: 设置红色值
268
+ * @param {number} red
269
+ * @return {CssColorParser}
270
+ */
271
+ CssColorParser.prototype.setRed = function (red) {
272
+ this.r = limitNumber(0, 255, defaultValue(Number(red), 0));
273
+ return this;
274
+ };
275
+ /**
276
+ * @description: 设置绿色值
277
+ * @param {number} green
278
+ * @return {CssColorParser}
279
+ */
280
+ CssColorParser.prototype.setGreen = function (green) {
281
+ this.g = limitNumber(0, 255, defaultValue(Number(green), 0));
282
+ return this;
283
+ };
284
+ /**
285
+ * @description: 设置蓝色值
286
+ * @param {number} blue
287
+ * @return {CssColorParser}
288
+ */
289
+ CssColorParser.prototype.setBlue = function (blue) {
290
+ this.b = limitNumber(0, 255, defaultValue(Number(blue), 0));
291
+ return this;
292
+ };
293
+ /**
294
+ * @description: 返回rgba格式的css字符串
295
+ * @return {string}
296
+ */
297
+ CssColorParser.prototype.toRGBA = function () {
298
+ var color = this.toJson();
299
+ if (color.a === 1) {
300
+ return "rgb(".concat(color.r, ",").concat(color.g, ",").concat(color.b, ")");
301
+ }
302
+ return "rgba(".concat(color.r, ",").concat(color.g, ",").concat(color.b, ",").concat(color.a, ")");
303
+ };
304
+ /**
305
+ * @description: 返回字符串
306
+ * @return {string}
307
+ */
308
+ CssColorParser.prototype.toString = function () {
309
+ return this.toRGBA();
310
+ };
311
+ /**
312
+ * @description: 归一化
313
+ * @return {array}
314
+ */
315
+ CssColorParser.prototype.toNormalize = function () {
316
+ var r = setNumberPrecision(this.r / 255, this._outColorPrecision);
317
+ var g = setNumberPrecision(this.g / 255, this._outColorPrecision);
318
+ var b = setNumberPrecision(this.b / 255, this._outColorPrecision);
319
+ var a = setNumberPrecision(this.a, this._outAlphaPrecision);
320
+ return [r, g, b, a];
321
+ };
322
+ /**
323
+ * @description: 返回16进制格式的css字符串
324
+ * @return {string}
325
+ */
326
+ CssColorParser.prototype.toHEX = function () {
327
+ var color = this.toJson();
328
+ var r = color.r.toString(16);
329
+ if (r.length < 2) {
330
+ r = "0".concat(r);
331
+ }
332
+ var g = color.g.toString(16);
333
+ if (g.length < 2) {
334
+ g = "0".concat(g);
335
+ }
336
+ var b = color.b.toString(16);
337
+ if (b.length < 2) {
338
+ b = "0".concat(b);
339
+ }
340
+ // 由于tojson后a会丢失精度,此处使用this.a
341
+ if (this.a < 1) {
342
+ var hexAlpha = parseInt((this.a * 255).toFixed()).toString(16);
343
+ if (hexAlpha.length < 2) {
344
+ hexAlpha = "0".concat(hexAlpha);
345
+ }
346
+ return "#".concat(r).concat(g).concat(b).concat(hexAlpha);
347
+ }
348
+ return "#".concat(r).concat(g).concat(b);
349
+ };
350
+ /**
351
+ * @description: 返回rgba数组
352
+ * @return {array}
353
+ */
354
+ CssColorParser.prototype.toArray = function () {
355
+ var color = this.toJson();
356
+ return [color.r, color.g, color.b, color.a];
357
+ };
358
+ /**
359
+ * @description: 返回ColorJson
360
+ * @return {ColorJson}
361
+ */
362
+ CssColorParser.prototype.toJson = function () {
363
+ return {
364
+ r: setNumberPrecision(this.r, this._outColorPrecision),
365
+ g: setNumberPrecision(this.g, this._outColorPrecision),
366
+ b: setNumberPrecision(this.b, this._outColorPrecision),
367
+ a: setNumberPrecision(this.a, this._outAlphaPrecision),
368
+ };
369
+ };
370
+ /**
371
+ * @description: 返回取反色后的新的实例
372
+ * @return {CssColorParser}
373
+ */
374
+ CssColorParser.prototype.toInvert = function () {
375
+ var r = 255 - this.r;
376
+ var g = 255 - this.g;
377
+ var b = 255 - this.b;
378
+ var a = 1 - this.a;
379
+ return new CssColorParser(r, g, b, a);
380
+ };
381
+ /**
382
+ * @description: 拷贝
383
+ * @return {CssColorParser}
384
+ */
385
+ CssColorParser.prototype.clone = function () {
386
+ return new CssColorParser(this.r, this.g, this.b, this.a);
387
+ };
388
+ /**
389
+ * @description: 比较两个解析对象的数据是否相等
390
+ * @param {string} color
391
+ * @return {boolean}
392
+ */
393
+ CssColorParser.prototype.equals = function (color) {
394
+ if (this === color) {
395
+ return true;
396
+ }
397
+ else {
398
+ var json1 = this.toJson();
399
+ var json2 = color.toJson();
400
+ return (json1.r === json2.r &&
401
+ json1.g === json2.g &&
402
+ json1.b === json2.g &&
403
+ json1.a === json2.a);
404
+ }
405
+ };
406
+ /**
407
+ * @description: 反色
408
+ * @return {CssColorParser}
409
+ */
410
+ CssColorParser.prototype.setInvert = function () {
411
+ this.r = 255 - this.r;
412
+ this.g = 255 - this.g;
413
+ this.b = 255 - this.b;
414
+ this.a = 1 - this.a;
415
+ return this;
416
+ };
417
+ /**
418
+ * @description: 乘以倍数
419
+ * @param {number} scalar
420
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
421
+ * @return {CssColorParser}
422
+ */
423
+ CssColorParser.prototype.multiplyByScalar = function (scalar, isSetAlpha) {
424
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
425
+ var r = this.r * scalar;
426
+ var g = this.g * scalar;
427
+ var b = this.b * scalar;
428
+ var a = isSetAlpha ? this.a * scalar : this.a;
429
+ return this.setColor(r, g, b, a);
430
+ };
431
+ /**
432
+ * @description: 除以倍数
433
+ * @param {number} scalar
434
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
435
+ * @return {CssColorParser}
436
+ */
437
+ CssColorParser.prototype.divideByScalar = function (scalar, isSetAlpha) {
438
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
439
+ var r = this.r / scalar;
440
+ var g = this.g / scalar;
441
+ var b = this.b / scalar;
442
+ var a = isSetAlpha ? this.a / scalar : this.a;
443
+ return this.setColor(r, g, b, a);
444
+ };
445
+ /**
446
+ * @description: 实例相加
447
+ * @param {CssColorParser} colorParser
448
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
449
+ * @return {CssColorParser}
450
+ */
451
+ CssColorParser.prototype.add = function (colorParser, isSetAlpha) {
452
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
453
+ var r = this.r + colorParser.r;
454
+ var g = this.g + colorParser.g;
455
+ var b = this.b + colorParser.b;
456
+ var a = isSetAlpha ? this.a + colorParser.a : this.a;
457
+ return this.setColor(r, g, b, a);
458
+ };
459
+ /**
460
+ * @description: 实例相减
461
+ * @param {CssColorParser} colorParser
462
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
463
+ * @return {CssColorParser}
464
+ */
465
+ CssColorParser.prototype.subtract = function (colorParser, isSetAlpha) {
466
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
467
+ var r = this.r - colorParser.r;
468
+ var g = this.g - colorParser.g;
469
+ var b = this.b - colorParser.b;
470
+ var a = isSetAlpha ? this.a - colorParser.a : this.a;
471
+ return this.setColor(r, g, b, a);
472
+ };
473
+ /**
474
+ * @description: 实例相乘
475
+ * @param {CssColorParser} colorParser
476
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
477
+ * @return {CssColorParser}
478
+ */
479
+ CssColorParser.prototype.multiply = function (colorParser, isSetAlpha) {
480
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
481
+ var r = this.r * colorParser.r;
482
+ var g = this.g * colorParser.g;
483
+ var b = this.b * colorParser.b;
484
+ var a = isSetAlpha ? this.a * colorParser.a : this.a;
485
+ return this.setColor(r, g, b, a);
486
+ };
487
+ /**
488
+ * @description: 实例相除
489
+ * @param {CssColorParser} colorParser
490
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
491
+ * @return {CssColorParser}
492
+ */
493
+ CssColorParser.prototype.divide = function (colorParser, isSetAlpha) {
494
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
495
+ var r = this.r / colorParser.r;
496
+ var g = this.g / colorParser.g;
497
+ var b = this.b / colorParser.b;
498
+ var a = isSetAlpha ? this.a / colorParser.a : this.a;
499
+ return this.setColor(r, g, b, a);
500
+ };
501
+ /**
502
+ * @description: 颜色RGB加上数字
503
+ * @param {number} num
504
+ * @return {CssColorParser}
505
+ */
506
+ CssColorParser.prototype.addNumberForRGB = function (num) {
507
+ this.r = this.r + num;
508
+ this.g = this.g + num;
509
+ this.b = this.b + num;
510
+ return this;
511
+ };
512
+ /**
513
+ * @description: 透明度加上数字
514
+ * @param {number} num
515
+ * @return {CssColorParser}
516
+ */
517
+ CssColorParser.prototype.addNumberForAlpha = function (num) {
518
+ this.a = this.a + num;
519
+ return this;
520
+ };
521
+ /**
522
+ * @description: 解析16进制颜色
523
+ * @param {string} v
524
+ * @return {CssColorParser}
525
+ * @example: CssColorParser.parseHEX('#FFF')
526
+ */
527
+ CssColorParser.parseHEX = function (v) {
528
+ var cssStr = CssColorStringParser.clearStrSpace(v);
529
+ var res = CssColorStringParser.parse3BitsHEX(cssStr);
530
+ if (!res) {
531
+ res = CssColorStringParser.parse6BitsHEX(cssStr);
532
+ }
533
+ return res && CssColorParser.fromArray(res);
534
+ };
535
+ /**
536
+ * @description: 解析rgba、rgb颜色
537
+ * @param {string} v
538
+ * @return {CssColorParser}
539
+ * @example: CssColorParser.parseRGBA('rgba(255,255,255,1)')
540
+ */
541
+ CssColorParser.parseRGBA = function (v) {
542
+ var cssStr = CssColorStringParser.clearStrSpace(v);
543
+ var res = CssColorStringParser.parseRGBA(cssStr);
544
+ if (!res) {
545
+ var cssStr2 = CssColorStringParser.trimStr(v);
546
+ res = CssColorStringParser.parseRGBA2(cssStr2);
547
+ }
548
+ return res && CssColorParser.fromArray(res);
549
+ };
550
+ /**
551
+ * @description: 将ColorJson格式的json数据转换为解析对象
552
+ * @param {ColorJson} json
553
+ * @return {CssColorParser}
554
+ * @example: CssColorParser.fromJson({r: 255, g: 255, b: 255, a: 1})
555
+ */
556
+ CssColorParser.fromJson = function (json) {
557
+ return new CssColorParser(json.r, json.g, json.b, json.a);
558
+ };
559
+ /**
560
+ * @description: 将RGBA数组转换为解析对象
561
+ * @param {Array} color
562
+ * @return {CssColorParser}
563
+ * @example: CssColorParser.fromArray([255,255,255,1])
564
+ */
565
+ CssColorParser.fromArray = function (color) {
566
+ return new CssColorParser(color[0], color[1], color[2], color[3]);
567
+ };
568
+ /**
569
+ * @description: 产生随机颜色
570
+ * @return {CssColorParser}
571
+ * @example: CssColorParser.fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
572
+ */
573
+ CssColorParser.fromRandom = function (color1, color2) {
574
+ var r = Math.random() * Math.abs(color2.r - color1.r) +
575
+ Math.min(color1.r, color2.r);
576
+ var g = Math.random() * Math.abs(color2.g - color1.g) +
577
+ Math.min(color1.g, color2.g);
578
+ var b = Math.random() * Math.abs(color2.b - color1.b) +
579
+ Math.min(color1.b, color2.b);
580
+ var a = Math.random() * Math.abs(color2.a - color1.a) +
581
+ Math.min(color1.a, color2.a);
582
+ return new CssColorParser(r, g, b, a);
583
+ };
584
+ /**
585
+ * @description: 颜色序列化数组转换为CssColorParser对象实例
586
+ * @param {array} colorArr
587
+ * @example: CssColorParser.fromNormaliz([1, 0, 0, 1])
588
+ */
589
+ CssColorParser.fromNormalize = function (colorArr) {
590
+ var r = colorArr[0] * 255;
591
+ var g = colorArr[1] * 255;
592
+ var b = colorArr[2] * 255;
593
+ var a = colorArr[3];
594
+ return CssColorParser.fromArray([r, g, b, a]);
595
+ };
596
+ return CssColorParser;
597
+ }());
598
+
599
+ /******************************************************************************
600
+ Copyright (c) Microsoft Corporation.
601
+
602
+ Permission to use, copy, modify, and/or distribute this software for any
603
+ purpose with or without fee is hereby granted.
604
+
605
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
606
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
607
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
608
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
609
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
610
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
611
+ PERFORMANCE OF THIS SOFTWARE.
612
+ ***************************************************************************** */
613
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
614
+
615
+ var extendStatics = function(d, b) {
616
+ extendStatics = Object.setPrototypeOf ||
617
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
618
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
619
+ return extendStatics(d, b);
620
+ };
621
+
622
+ function __extends(d, b) {
623
+ if (typeof b !== "function" && b !== null)
624
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
625
+ extendStatics(d, b);
626
+ function __() { this.constructor = d; }
627
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
628
+ }
629
+
630
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
631
+ var e = new Error(message);
632
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
633
+ };
634
+
635
+ var CssColorParserPlus = /** @class */ (function (_super) {
636
+ __extends(CssColorParserPlus, _super);
637
+ function CssColorParserPlus() {
638
+ return _super !== null && _super.apply(this, arguments) || this;
639
+ }
640
+ /**
641
+ * @description: 返回取反色后的新的实例
642
+ * @return {CssColorParserPlus}
643
+ */
644
+ CssColorParserPlus.prototype.toInvert = function () {
645
+ var r = 255 - this.r;
646
+ var g = 255 - this.g;
647
+ var b = 255 - this.b;
648
+ var a = 1 - this.a;
649
+ return new CssColorParserPlus(r, g, b, a);
650
+ };
651
+ /**
652
+ * @description: 拷贝
653
+ * @return {CssColorParserPlus}
654
+ */
655
+ CssColorParserPlus.prototype.clone = function () {
656
+ return new CssColorParserPlus(this.r, this.g, this.b, this.a);
657
+ };
658
+ /**
659
+ * @description: 比较两个解析对象的数据是否相等
660
+ * @param {string} color
661
+ * @return {boolean}
662
+ */
663
+ CssColorParserPlus.prototype.equals = function (color) {
664
+ color = CssColorParserPlus.parseColor(color);
665
+ if (this === color) {
666
+ return true;
667
+ }
668
+ else {
669
+ var json1 = this.toJson();
670
+ var json2 = color.toJson();
671
+ return (json1.r === json2.r &&
672
+ json1.g === json2.g &&
673
+ json1.b === json2.g &&
674
+ json1.a === json2.a);
675
+ }
676
+ };
677
+ /**
678
+ * @description: 实例相加
679
+ * @param {CssColorParser} colorParser
680
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
681
+ * @return {CssColorParser}
682
+ */
683
+ CssColorParserPlus.prototype.add = function (color, isSetAlpha) {
684
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
685
+ var colorParser = CssColorParserPlus.parseColor(color);
686
+ return _super.prototype.add.call(this, colorParser, isSetAlpha);
687
+ };
688
+ /**
689
+ * @description: 实例相减
690
+ * @param {CssColorParser} colorParser
691
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
692
+ * @return {CssColorParser}
693
+ */
694
+ CssColorParserPlus.prototype.subtract = function (color, isSetAlpha) {
695
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
696
+ var colorParser = CssColorParserPlus.parseColor(color);
697
+ return _super.prototype.subtract.call(this, colorParser, isSetAlpha);
698
+ };
699
+ /**
700
+ * @description: 实例相乘
701
+ * @param {CssColorParser} colorParser
702
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
703
+ * @return {CssColorParser}
704
+ */
705
+ CssColorParserPlus.prototype.multiply = function (color, isSetAlpha) {
706
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
707
+ var colorParser = CssColorParserPlus.parseColor(color);
708
+ return _super.prototype.multiply.call(this, colorParser, isSetAlpha);
709
+ };
710
+ /**
711
+ * @description: 实例相除
712
+ * @param {CssColorParser} colorParser
713
+ * @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
714
+ * @return {CssColorParser}
715
+ */
716
+ CssColorParserPlus.prototype.divide = function (color, isSetAlpha) {
717
+ if (isSetAlpha === void 0) { isSetAlpha = true; }
718
+ var colorParser = CssColorParserPlus.parseColor(color);
719
+ return _super.prototype.divide.call(this, colorParser, isSetAlpha);
720
+ };
721
+ /**
722
+ * @description: 解析css颜色
723
+ * @param {string} v
724
+ * @return {CssColorParserPlus}
725
+ * @example: parseCssColorStr('rgba(255,255,255,1)')
726
+ */
727
+ CssColorParserPlus.parseColor = function (v) {
728
+ if (v instanceof CssColorParser) {
729
+ return v;
730
+ }
731
+ return CssColorParserPlus.parseCssColorStr(v);
732
+ };
733
+ /**
734
+ * @description: 将css字符串转换为解析对象
735
+ * @param {string} v
736
+ * @return {CssColorParserPlus}
737
+ * @example: parseCssColorStr('rgba(255,255,255,1)')
738
+ */
739
+ CssColorParserPlus.parseCssColorStr = function (v) {
740
+ Check.type('color', v, 'string');
741
+ return (CssColorParserPlus.parseHEX(v) ||
742
+ CssColorParserPlus.parseRGBA(v) ||
743
+ CssColorParserPlus.parseKeyWord(v) ||
744
+ CssColorParserPlus.parseHSLA(v) ||
745
+ CssColorParserPlus.parseHWB(v));
746
+ };
747
+ /**
748
+ * @description: 解析颜色关键字
749
+ * @param {string} v
750
+ * @return {CssColorParserPlus}
751
+ * @example: parseKeyWord('red')
752
+ */
753
+ CssColorParserPlus.parseKeyWord = function (v) {
754
+ var cssStr = CssColorStringParser.clearStrSpace(v);
755
+ var res = convert.keyword.rgb(cssStr);
756
+ return res && CssColorParserPlus.fromArray(res);
757
+ };
758
+ /**
759
+ * @description: 解析HSLA
760
+ * @param {string} v
761
+ * @return {CssColorParserPlus}
762
+ * @example: parseHSLA('hsla(215,85%,62%,0.8)')
763
+ */
764
+ CssColorParserPlus.parseHSLA = function (v) {
765
+ var cssStr = CssColorStringParser.clearStrSpace(v);
766
+ var res = CssColorStringParser.parseHSLA(cssStr);
767
+ if (!res) {
768
+ var cssStr2 = CssColorStringParser.trimStr(v);
769
+ res = CssColorStringParser.parseHSLA2(cssStr2);
770
+ }
771
+ return res && CssColorParserPlus.fromHSL(res[0], res[1], res[2], res[3]);
772
+ };
773
+ /**
774
+ * @description: 解析HWB
775
+ * @param {string} v
776
+ * @return {CssColorParserPlus}
777
+ * @example: parseHWB('hwb(215deg 30% 6% / 80%)')
778
+ */
779
+ CssColorParserPlus.parseHWB = function (v) {
780
+ var cssStr2 = CssColorStringParser.trimStr(v);
781
+ var res = CssColorStringParser.parseHWB(cssStr2);
782
+ return res && CssColorParserPlus.fromHWB(res[0], res[1], res[2], res[3]);
783
+ };
784
+ /**
785
+ * @description: 将HSL色彩模式转换为解析对象
786
+ * @param {number} hue 色相
787
+ * @param {number} saturation 饱和度
788
+ * @param {number} lightness 亮度
789
+ * @param {number} alpha 不透明度
790
+ * @return {CssColorParserPlus}
791
+ * @example: fromHSL(0,1,1,1)
792
+ */
793
+ CssColorParserPlus.fromHSL = function (h, s, l, a) {
794
+ var res = convert.hsl.rgb(limitNumber(0, 360, h), limitNumber(0, 100, s * 100), limitNumber(0, 100, l * 100));
795
+ return new CssColorParserPlus(res[0], res[1], res[2], defaultValue(Number(a), 1));
796
+ };
797
+ /**
798
+ * @description: 将HWB色彩模式转换为解析对象
799
+ * @param {number} h 色调
800
+ * @param {number} w 白度
801
+ * @param {number} b 黑度
802
+ * @param {number} a 不透明度
803
+ * @return {CssColorParserPlus}
804
+ * @example: fromHSL(0,1,1,1)
805
+ */
806
+ CssColorParserPlus.fromHWB = function (h, w, b, a) {
807
+ var res = convert.hwb.rgb(limitNumber(0, 360, h), limitNumber(0, 100, w * 100), limitNumber(0, 100, b * 100));
808
+ return new CssColorParserPlus(res[0], res[1], res[2], defaultValue(Number(a), 1));
809
+ };
810
+ /**
811
+ * @description: 解析16进制颜色
812
+ * @param {string} v
813
+ * @return {CssColorParserPlus}
814
+ * @example: CssColorParserPlus.parseHEX('#FFF')
815
+ */
816
+ CssColorParserPlus.parseHEX = function (v) {
817
+ var cssStr = CssColorStringParser.clearStrSpace(v);
818
+ var res = CssColorStringParser.parse3BitsHEX(cssStr);
819
+ if (!res) {
820
+ res = CssColorStringParser.parse6BitsHEX(cssStr);
821
+ }
822
+ return res && CssColorParserPlus.fromArray(res);
823
+ };
824
+ /**
825
+ * @description: 解析rgba、rgb颜色
826
+ * @param {string} v
827
+ * @return {CssColorParserPlus}
828
+ * @example: CssColorParserPlus.parseRGBA('rgba(255,255,255,1)')
829
+ */
830
+ CssColorParserPlus.parseRGBA = function (v) {
831
+ var cssStr = CssColorStringParser.clearStrSpace(v);
832
+ var res = CssColorStringParser.parseRGBA(cssStr);
833
+ if (!res) {
834
+ var cssStr2 = CssColorStringParser.trimStr(v);
835
+ res = CssColorStringParser.parseRGBA2(cssStr2);
836
+ }
837
+ return res && CssColorParserPlus.fromArray(res);
838
+ };
839
+ /**
840
+ * @description: 将ColorJson格式的json数据转换为解析对象
841
+ * @param {ColorJson} json
842
+ * @return {CssColorParserPlus}
843
+ * @example: CssColorParserPlus.fromJson({r: 255, g: 255, b: 255, a: 1})
844
+ */
845
+ CssColorParserPlus.fromJson = function (json) {
846
+ return new CssColorParserPlus(json.r, json.g, json.b, json.a);
847
+ };
848
+ /**
849
+ * @description: 将RGBA数组转换为解析对象
850
+ * @param {Array} color
851
+ * @return {CssColorParserPlus}
852
+ * @example: CssColorParserPlus.fromArray([255,255,255,1])
853
+ */
854
+ CssColorParserPlus.fromArray = function (color) {
855
+ return new CssColorParserPlus(color[0], color[1], color[2], color[3]);
856
+ };
857
+ /**
858
+ * @description: 产生随机颜色
859
+ * @return {CssColorParserPlus}
860
+ * @example: CssColorParserPlus.fromRandom('black', new CssColorParserPlus(255,255,255,1))
861
+ */
862
+ CssColorParserPlus.fromRandom = function (color1, color2) {
863
+ color1 = CssColorParserPlus.parseColor(color1);
864
+ color2 = CssColorParserPlus.parseColor(color2);
865
+ var r = Math.random() * Math.abs(color2.r - color1.r) +
866
+ Math.min(color1.r, color2.r);
867
+ var g = Math.random() * Math.abs(color2.g - color1.g) +
868
+ Math.min(color1.g, color2.g);
869
+ var b = Math.random() * Math.abs(color2.b - color1.b) +
870
+ Math.min(color1.b, color2.b);
871
+ var a = Math.random() * Math.abs(color2.a - color1.a) +
872
+ Math.min(color1.a, color2.a);
873
+ return new CssColorParserPlus(r, g, b, a);
874
+ };
875
+ /**
876
+ * @description: 颜色序列化数组转换为CssColorParserPlus对象实例
877
+ * @param {array} colorArr
878
+ * @example: CssColorParserPlus.fromNormaliz([1, 0, 0, 1])
879
+ */
880
+ CssColorParserPlus.fromNormalize = function (colorArr) {
881
+ var r = colorArr[0] * 255;
882
+ var g = colorArr[1] * 255;
883
+ var b = colorArr[2] * 255;
884
+ var a = colorArr[3];
885
+ return CssColorParserPlus.fromArray([r, g, b, a]);
886
+ };
887
+ return CssColorParserPlus;
888
+ }(CssColorParser));
889
+
890
+ export { CssColorParser, CssColorParserPlus };