css-color-parser-h 2.0.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/@types/index.d.ts +384 -6
- package/dist/css-color-parser-h.common.js +517 -210
- package/dist/css-color-parser-h.common.min.js +1 -1
- package/dist/css-color-parser-h.umd.js +986 -210
- package/dist/css-color-parser-h.umd.min.js +1 -1
- package/example.html +10 -48
- package/package.json +6 -4
- package/readme.md +94 -56
- package/@types/css-color-parser-h.d.ts +0 -230
|
@@ -52,37 +52,46 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
52
52
|
// EXPORTS
|
|
53
53
|
__webpack_require__.d(__webpack_exports__, {
|
|
54
54
|
"CssColorParser": function() { return /* reexport */ src_CssColorParser; },
|
|
55
|
-
"
|
|
56
|
-
"fromColorStr": function() { return /* reexport */ fromColorStr; },
|
|
57
|
-
"fromHSL": function() { return /* reexport */ fromHSL; },
|
|
58
|
-
"fromHWB": function() { return /* reexport */ fromHWB; },
|
|
59
|
-
"fromJson": function() { return /* reexport */ fromJson; },
|
|
60
|
-
"fromRandom": function() { return /* reexport */ fromRandom; },
|
|
61
|
-
"parseCssColorStr": function() { return /* reexport */ parseCssColorStr; },
|
|
62
|
-
"parseHEX": function() { return /* reexport */ parseHEX; },
|
|
63
|
-
"parseHSLA": function() { return /* reexport */ parseHSLA; },
|
|
64
|
-
"parseHWB": function() { return /* reexport */ parseHWB; },
|
|
65
|
-
"parseKeyWord": function() { return /* reexport */ parseKeyWord; },
|
|
66
|
-
"parseRGBA": function() { return /* reexport */ parseRGBA; }
|
|
55
|
+
"CssColorParserPlus": function() { return /* reexport */ src_CssColorParserPlus; }
|
|
67
56
|
});
|
|
68
57
|
|
|
69
58
|
;// CONCATENATED MODULE: ./src/utils/common.ts
|
|
70
59
|
var Check = /** @class */ (function () {
|
|
71
60
|
function Check() {
|
|
72
61
|
}
|
|
73
|
-
Check.type = function (
|
|
62
|
+
Check.type = function (paramName, value, type) {
|
|
74
63
|
var valueType = typeof value;
|
|
75
64
|
if (valueType !== type) {
|
|
76
65
|
throw new Error("Expected ".concat(paramName, " to be typeof ").concat(type, ", actual typeof was ").concat(valueType));
|
|
77
66
|
}
|
|
78
67
|
};
|
|
79
|
-
Check.types = function (
|
|
68
|
+
Check.types = function (paramName, value, types) {
|
|
80
69
|
var valueType = typeof value;
|
|
81
70
|
var isContained = types.includes(valueType);
|
|
82
71
|
if (!isContained) {
|
|
83
72
|
throw new Error("Expected ".concat(paramName, " to be typeof ").concat(types.join('|'), ", actual typeof was ").concat(valueType));
|
|
84
73
|
}
|
|
85
74
|
};
|
|
75
|
+
Check.numValue = function (paramName, value, min, max) {
|
|
76
|
+
Check.numMinValue(paramName, value, min);
|
|
77
|
+
Check.numMaxValue(paramName, value, max);
|
|
78
|
+
};
|
|
79
|
+
Check.numMinValue = function (paramName, value, min) {
|
|
80
|
+
if (value < min) {
|
|
81
|
+
throw new Error("Expected ".concat(paramName, " to > ").concat(min, ", actual value was ").concat(value));
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
Check.numMaxValue = function (paramName, value, max) {
|
|
85
|
+
if (value > max) {
|
|
86
|
+
throw new Error("Expected ".concat(paramName, " to < ").concat(max, ", actual value was ").concat(value));
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
Check.numIsInt = function (paramName, value, expectIsInt) {
|
|
90
|
+
var isInt = Math.floor(value) === value;
|
|
91
|
+
if (isInt !== expectIsInt) {
|
|
92
|
+
throw new Error("Expected ".concat(paramName, " to ").concat(expectIsInt ? 'Integer' : 'Float', ", actual value was ").concat(isInt ? 'Integer' : 'Float'));
|
|
93
|
+
}
|
|
94
|
+
};
|
|
86
95
|
return Check;
|
|
87
96
|
}());
|
|
88
97
|
|
|
@@ -111,7 +120,9 @@ function limitNumber(min, max, v) {
|
|
|
111
120
|
return v;
|
|
112
121
|
}
|
|
113
122
|
function setNumberPrecision(number, precision) {
|
|
114
|
-
|
|
123
|
+
if (number === void 0) { number = 0; }
|
|
124
|
+
var prec = Math.pow(10, precision);
|
|
125
|
+
return Math.round(number * prec) / prec;
|
|
115
126
|
}
|
|
116
127
|
|
|
117
128
|
;// CONCATENATED MODULE: ./src/utils/color-tools.ts
|
|
@@ -261,12 +272,12 @@ var CssColorStringParser = /** @class */ (function () {
|
|
|
261
272
|
|
|
262
273
|
;// CONCATENATED MODULE: ./src/CssColorParser.ts
|
|
263
274
|
/*
|
|
264
|
-
* @Descripttion:
|
|
275
|
+
* @Descripttion: 颜色解析器(轻量)
|
|
265
276
|
* @version: 1.0.0
|
|
266
277
|
* @Author: roman_123
|
|
267
278
|
* @Date: 2021-01-19 09:22:11
|
|
268
279
|
* @LastEditors: Please set LastEditors
|
|
269
|
-
* @LastEditTime: 2023-
|
|
280
|
+
* @LastEditTime: 2023-06-27 19:01:50
|
|
270
281
|
*/
|
|
271
282
|
|
|
272
283
|
|
|
@@ -276,8 +287,27 @@ var CssColorParser = /** @class */ (function () {
|
|
|
276
287
|
this.g = 255;
|
|
277
288
|
this.b = 255;
|
|
278
289
|
this.a = 1;
|
|
290
|
+
this._outColorPrecision = 2;
|
|
291
|
+
this._outAlphaPrecision = 2;
|
|
279
292
|
this.setColor(red, green, blue, alpha);
|
|
280
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* @description: 设置CssColorParser实例输出的精度
|
|
296
|
+
* @param {number} colorPrecision 输出颜色的精度
|
|
297
|
+
* @param {number} outAlphaPrecision 输出透明度的精度
|
|
298
|
+
* @return {CssColorParser}
|
|
299
|
+
*/
|
|
300
|
+
CssColorParser.prototype.setOutPrecision = function (colorPrecision, outAlphaPrecision) {
|
|
301
|
+
Check.type('colorPrecision', colorPrecision, 'number');
|
|
302
|
+
Check.type('outAlphaPrecision', outAlphaPrecision, 'number');
|
|
303
|
+
Check.numMinValue('colorPrecision', colorPrecision, 0);
|
|
304
|
+
Check.numMinValue('outAlphaPrecision', outAlphaPrecision, 0);
|
|
305
|
+
Check.numIsInt('colorPrecision', colorPrecision, true);
|
|
306
|
+
Check.numIsInt('outAlphaPrecision', outAlphaPrecision, true);
|
|
307
|
+
this._outColorPrecision = colorPrecision;
|
|
308
|
+
this._outAlphaPrecision = outAlphaPrecision;
|
|
309
|
+
return this;
|
|
310
|
+
};
|
|
281
311
|
/**
|
|
282
312
|
* 设置颜色
|
|
283
313
|
* @param red
|
|
@@ -291,11 +321,47 @@ var CssColorParser = /** @class */ (function () {
|
|
|
291
321
|
this.g = limitNumber(0, 255, defaultValue(Number(green), 0));
|
|
292
322
|
this.b = limitNumber(0, 255, defaultValue(Number(blue), 0));
|
|
293
323
|
this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1));
|
|
324
|
+
return this;
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* @description: 设置透明度
|
|
328
|
+
* @param {number} alpha
|
|
329
|
+
* @return {CssColorParser}
|
|
330
|
+
*/
|
|
331
|
+
CssColorParser.prototype.setAlpha = function (alpha) {
|
|
332
|
+
this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1));
|
|
333
|
+
return this;
|
|
334
|
+
};
|
|
335
|
+
/**
|
|
336
|
+
* @description: 设置红色值
|
|
337
|
+
* @param {number} red
|
|
338
|
+
* @return {CssColorParser}
|
|
339
|
+
*/
|
|
340
|
+
CssColorParser.prototype.setRed = function (red) {
|
|
341
|
+
this.r = limitNumber(0, 255, defaultValue(Number(red), 0));
|
|
342
|
+
return this;
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* @description: 设置绿色值
|
|
346
|
+
* @param {number} green
|
|
347
|
+
* @return {CssColorParser}
|
|
348
|
+
*/
|
|
349
|
+
CssColorParser.prototype.setGreen = function (green) {
|
|
350
|
+
this.g = limitNumber(0, 255, defaultValue(Number(green), 0));
|
|
351
|
+
return this;
|
|
352
|
+
};
|
|
353
|
+
/**
|
|
354
|
+
* @description: 设置蓝色值
|
|
355
|
+
* @param {number} blue
|
|
356
|
+
* @return {CssColorParser}
|
|
357
|
+
*/
|
|
358
|
+
CssColorParser.prototype.setBlue = function (blue) {
|
|
359
|
+
this.b = limitNumber(0, 255, defaultValue(Number(blue), 0));
|
|
360
|
+
return this;
|
|
294
361
|
};
|
|
295
362
|
/**
|
|
296
363
|
* @description: 返回rgba格式的css字符串
|
|
297
|
-
* @return {
|
|
298
|
-
* @author: roman_123
|
|
364
|
+
* @return {string}
|
|
299
365
|
*/
|
|
300
366
|
CssColorParser.prototype.toRGBA = function () {
|
|
301
367
|
var color = this.toJson();
|
|
@@ -306,29 +372,25 @@ var CssColorParser = /** @class */ (function () {
|
|
|
306
372
|
};
|
|
307
373
|
/**
|
|
308
374
|
* @description: 返回字符串
|
|
309
|
-
* @return {
|
|
310
|
-
* @author: roman_123
|
|
375
|
+
* @return {string}
|
|
311
376
|
*/
|
|
312
377
|
CssColorParser.prototype.toString = function () {
|
|
313
378
|
return this.toRGBA();
|
|
314
379
|
};
|
|
315
380
|
/**
|
|
316
381
|
* @description: 归一化
|
|
317
|
-
* @return {
|
|
318
|
-
* @author: roman_123
|
|
382
|
+
* @return {array}
|
|
319
383
|
*/
|
|
320
384
|
CssColorParser.prototype.toNormalize = function () {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
};
|
|
385
|
+
var r = setNumberPrecision(this.r / 255, this._outColorPrecision);
|
|
386
|
+
var g = setNumberPrecision(this.g / 255, this._outColorPrecision);
|
|
387
|
+
var b = setNumberPrecision(this.b / 255, this._outColorPrecision);
|
|
388
|
+
var a = setNumberPrecision(this.a, this._outAlphaPrecision);
|
|
389
|
+
return [r, g, b, a];
|
|
327
390
|
};
|
|
328
391
|
/**
|
|
329
392
|
* @description: 返回16进制格式的css字符串
|
|
330
|
-
* @return {
|
|
331
|
-
* @author: roman_123
|
|
393
|
+
* @return {string}
|
|
332
394
|
*/
|
|
333
395
|
CssColorParser.prototype.toHEX = function () {
|
|
334
396
|
var color = this.toJson();
|
|
@@ -356,8 +418,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
356
418
|
};
|
|
357
419
|
/**
|
|
358
420
|
* @description: 返回rgba数组
|
|
359
|
-
* @return {
|
|
360
|
-
* @author: roman_123
|
|
421
|
+
* @return {array}
|
|
361
422
|
*/
|
|
362
423
|
CssColorParser.prototype.toArray = function () {
|
|
363
424
|
var color = this.toJson();
|
|
@@ -365,47 +426,172 @@ var CssColorParser = /** @class */ (function () {
|
|
|
365
426
|
};
|
|
366
427
|
/**
|
|
367
428
|
* @description: 返回ColorJson
|
|
368
|
-
* @return {
|
|
369
|
-
* @author: roman_123
|
|
429
|
+
* @return {ColorJson}
|
|
370
430
|
*/
|
|
371
431
|
CssColorParser.prototype.toJson = function () {
|
|
372
432
|
return {
|
|
373
|
-
r:
|
|
374
|
-
g:
|
|
375
|
-
b:
|
|
376
|
-
a:
|
|
433
|
+
r: setNumberPrecision(this.r, this._outColorPrecision),
|
|
434
|
+
g: setNumberPrecision(this.g, this._outColorPrecision),
|
|
435
|
+
b: setNumberPrecision(this.b, this._outColorPrecision),
|
|
436
|
+
a: setNumberPrecision(this.a, this._outAlphaPrecision),
|
|
377
437
|
};
|
|
378
438
|
};
|
|
439
|
+
/**
|
|
440
|
+
* @description: 返回取反色后的新的实例
|
|
441
|
+
* @return {CssColorParser}
|
|
442
|
+
*/
|
|
443
|
+
CssColorParser.prototype.toInvert = function () {
|
|
444
|
+
var r = 255 - this.r;
|
|
445
|
+
var g = 255 - this.g;
|
|
446
|
+
var b = 255 - this.b;
|
|
447
|
+
var a = 1 - this.a;
|
|
448
|
+
return new CssColorParser(r, g, b, a);
|
|
449
|
+
};
|
|
379
450
|
/**
|
|
380
451
|
* @description: 拷贝
|
|
381
|
-
* @return {
|
|
382
|
-
* @author: roman_123
|
|
452
|
+
* @return {CssColorParser}
|
|
383
453
|
*/
|
|
384
454
|
CssColorParser.prototype.clone = function () {
|
|
385
455
|
return new CssColorParser(this.r, this.g, this.b, this.a);
|
|
386
456
|
};
|
|
387
457
|
/**
|
|
388
458
|
* @description: 比较两个解析对象的数据是否相等
|
|
389
|
-
* @param {
|
|
390
|
-
* @return {
|
|
391
|
-
* @author: roman_123
|
|
459
|
+
* @param {string} color
|
|
460
|
+
* @return {boolean}
|
|
392
461
|
*/
|
|
393
462
|
CssColorParser.prototype.equals = function (color) {
|
|
394
463
|
if (this === color) {
|
|
395
464
|
return true;
|
|
396
465
|
}
|
|
397
466
|
else {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
467
|
+
var json1 = this.toJson();
|
|
468
|
+
var json2 = color.toJson();
|
|
469
|
+
return (json1.r === json2.r &&
|
|
470
|
+
json1.g === json2.g &&
|
|
471
|
+
json1.b === json2.g &&
|
|
472
|
+
json1.a === json2.a);
|
|
402
473
|
}
|
|
403
474
|
};
|
|
475
|
+
/**
|
|
476
|
+
* @description: 反色
|
|
477
|
+
* @return {CssColorParser}
|
|
478
|
+
*/
|
|
479
|
+
CssColorParser.prototype.setInvert = function () {
|
|
480
|
+
this.r = 255 - this.r;
|
|
481
|
+
this.g = 255 - this.g;
|
|
482
|
+
this.b = 255 - this.b;
|
|
483
|
+
this.a = 1 - this.a;
|
|
484
|
+
return this;
|
|
485
|
+
};
|
|
486
|
+
/**
|
|
487
|
+
* @description: 乘以倍数
|
|
488
|
+
* @param {number} scalar
|
|
489
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
490
|
+
* @return {CssColorParser}
|
|
491
|
+
*/
|
|
492
|
+
CssColorParser.prototype.multiplyByScalar = function (scalar, isSetAlpha) {
|
|
493
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
494
|
+
var r = this.r * scalar;
|
|
495
|
+
var g = this.g * scalar;
|
|
496
|
+
var b = this.b * scalar;
|
|
497
|
+
var a = isSetAlpha ? this.a * scalar : this.a;
|
|
498
|
+
return this.setColor(r, g, b, a);
|
|
499
|
+
};
|
|
500
|
+
/**
|
|
501
|
+
* @description: 除以倍数
|
|
502
|
+
* @param {number} scalar
|
|
503
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
504
|
+
* @return {CssColorParser}
|
|
505
|
+
*/
|
|
506
|
+
CssColorParser.prototype.divideByScalar = function (scalar, isSetAlpha) {
|
|
507
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
508
|
+
var r = this.r / scalar;
|
|
509
|
+
var g = this.g / scalar;
|
|
510
|
+
var b = this.b / scalar;
|
|
511
|
+
var a = isSetAlpha ? this.a / scalar : this.a;
|
|
512
|
+
return this.setColor(r, g, b, a);
|
|
513
|
+
};
|
|
514
|
+
/**
|
|
515
|
+
* @description: 实例相加
|
|
516
|
+
* @param {CssColorParser} colorParser
|
|
517
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
518
|
+
* @return {CssColorParser}
|
|
519
|
+
*/
|
|
520
|
+
CssColorParser.prototype.add = function (colorParser, isSetAlpha) {
|
|
521
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
522
|
+
var r = this.r + colorParser.r;
|
|
523
|
+
var g = this.g + colorParser.g;
|
|
524
|
+
var b = this.b + colorParser.b;
|
|
525
|
+
var a = isSetAlpha ? this.a + colorParser.a : this.a;
|
|
526
|
+
return this.setColor(r, g, b, a);
|
|
527
|
+
};
|
|
528
|
+
/**
|
|
529
|
+
* @description: 实例相减
|
|
530
|
+
* @param {CssColorParser} colorParser
|
|
531
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
532
|
+
* @return {CssColorParser}
|
|
533
|
+
*/
|
|
534
|
+
CssColorParser.prototype.subtract = function (colorParser, isSetAlpha) {
|
|
535
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
536
|
+
var r = this.r - colorParser.r;
|
|
537
|
+
var g = this.g - colorParser.g;
|
|
538
|
+
var b = this.b - colorParser.b;
|
|
539
|
+
var a = isSetAlpha ? this.a - colorParser.a : this.a;
|
|
540
|
+
return this.setColor(r, g, b, a);
|
|
541
|
+
};
|
|
542
|
+
/**
|
|
543
|
+
* @description: 实例相乘
|
|
544
|
+
* @param {CssColorParser} colorParser
|
|
545
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
546
|
+
* @return {CssColorParser}
|
|
547
|
+
*/
|
|
548
|
+
CssColorParser.prototype.multiply = function (colorParser, isSetAlpha) {
|
|
549
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
550
|
+
var r = this.r * colorParser.r;
|
|
551
|
+
var g = this.g * colorParser.g;
|
|
552
|
+
var b = this.b * colorParser.b;
|
|
553
|
+
var a = isSetAlpha ? this.a * colorParser.a : this.a;
|
|
554
|
+
return this.setColor(r, g, b, a);
|
|
555
|
+
};
|
|
556
|
+
/**
|
|
557
|
+
* @description: 实例相除
|
|
558
|
+
* @param {CssColorParser} colorParser
|
|
559
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
560
|
+
* @return {CssColorParser}
|
|
561
|
+
*/
|
|
562
|
+
CssColorParser.prototype.divide = function (colorParser, isSetAlpha) {
|
|
563
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
564
|
+
var r = this.r / colorParser.r;
|
|
565
|
+
var g = this.g / colorParser.g;
|
|
566
|
+
var b = this.b / colorParser.b;
|
|
567
|
+
var a = isSetAlpha ? this.a / colorParser.a : this.a;
|
|
568
|
+
return this.setColor(r, g, b, a);
|
|
569
|
+
};
|
|
570
|
+
/**
|
|
571
|
+
* @description: 颜色RGB加上数字
|
|
572
|
+
* @param {number} num
|
|
573
|
+
* @return {CssColorParser}
|
|
574
|
+
*/
|
|
575
|
+
CssColorParser.prototype.addNumberForRGB = function (num) {
|
|
576
|
+
this.r = this.r + num;
|
|
577
|
+
this.g = this.g + num;
|
|
578
|
+
this.b = this.b + num;
|
|
579
|
+
return this;
|
|
580
|
+
};
|
|
581
|
+
/**
|
|
582
|
+
* @description: 透明度加上数字
|
|
583
|
+
* @param {number} num
|
|
584
|
+
* @return {CssColorParser}
|
|
585
|
+
*/
|
|
586
|
+
CssColorParser.prototype.addNumberForAlpha = function (num) {
|
|
587
|
+
this.a = this.a + num;
|
|
588
|
+
return this;
|
|
589
|
+
};
|
|
404
590
|
/**
|
|
405
591
|
* @description: 解析16进制颜色
|
|
406
592
|
* @param {string} v
|
|
407
593
|
* @return {CssColorParser}
|
|
408
|
-
* @example: parseHEX('#FFF')
|
|
594
|
+
* @example: CssColorParser.parseHEX('#FFF')
|
|
409
595
|
*/
|
|
410
596
|
CssColorParser.parseHEX = function (v) {
|
|
411
597
|
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
@@ -419,7 +605,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
419
605
|
* @description: 解析rgba、rgb颜色
|
|
420
606
|
* @param {string} v
|
|
421
607
|
* @return {CssColorParser}
|
|
422
|
-
* @example: parseRGBA('rgba(255,255,255,1)')
|
|
608
|
+
* @example: CssColorParser.parseRGBA('rgba(255,255,255,1)')
|
|
423
609
|
*/
|
|
424
610
|
CssColorParser.parseRGBA = function (v) {
|
|
425
611
|
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
@@ -434,7 +620,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
434
620
|
* @description: 将ColorJson格式的json数据转换为解析对象
|
|
435
621
|
* @param {ColorJson} json
|
|
436
622
|
* @return {CssColorParser}
|
|
437
|
-
* @example: fromJson({r: 255, g: 255, b: 255, a: 1})
|
|
623
|
+
* @example: CssColorParser.fromJson({r: 255, g: 255, b: 255, a: 1})
|
|
438
624
|
*/
|
|
439
625
|
CssColorParser.fromJson = function (json) {
|
|
440
626
|
return new CssColorParser(json.r, json.g, json.b, json.a);
|
|
@@ -443,7 +629,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
443
629
|
* @description: 将RGBA数组转换为解析对象
|
|
444
630
|
* @param {Array} color
|
|
445
631
|
* @return {CssColorParser}
|
|
446
|
-
* @example:
|
|
632
|
+
* @example: CssColorParser.fromArray([255,255,255,1])
|
|
447
633
|
*/
|
|
448
634
|
CssColorParser.fromArray = function (color) {
|
|
449
635
|
return new CssColorParser(color[0], color[1], color[2], color[3]);
|
|
@@ -451,7 +637,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
451
637
|
/**
|
|
452
638
|
* @description: 产生随机颜色
|
|
453
639
|
* @return {CssColorParser}
|
|
454
|
-
* @example: fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
|
|
640
|
+
* @example: CssColorParser.fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
|
|
455
641
|
*/
|
|
456
642
|
CssColorParser.fromRandom = function (color1, color2) {
|
|
457
643
|
var r = Math.random() * Math.abs(color2.r - color1.r) +
|
|
@@ -464,182 +650,303 @@ var CssColorParser = /** @class */ (function () {
|
|
|
464
650
|
Math.min(color1.a, color2.a);
|
|
465
651
|
return new CssColorParser(r, g, b, a);
|
|
466
652
|
};
|
|
653
|
+
/**
|
|
654
|
+
* @description: 颜色序列化数组转换为CssColorParser对象实例
|
|
655
|
+
* @param {array} colorArr
|
|
656
|
+
* @example: CssColorParser.fromNormaliz([1, 0, 0, 1])
|
|
657
|
+
*/
|
|
658
|
+
CssColorParser.fromNormalize = function (colorArr) {
|
|
659
|
+
var r = colorArr[0] * 255;
|
|
660
|
+
var g = colorArr[1] * 255;
|
|
661
|
+
var b = colorArr[2] * 255;
|
|
662
|
+
var a = colorArr[3];
|
|
663
|
+
return CssColorParser.fromArray([r, g, b, a]);
|
|
664
|
+
};
|
|
467
665
|
return CssColorParser;
|
|
468
666
|
}());
|
|
469
667
|
/* harmony default export */ var src_CssColorParser = (CssColorParser);
|
|
470
668
|
|
|
669
|
+
;// CONCATENATED MODULE: external "tslib"
|
|
670
|
+
var external_tslib_namespaceObject = require("tslib");
|
|
471
671
|
;// CONCATENATED MODULE: external "color-convert"
|
|
472
672
|
var external_color_convert_namespaceObject = require("color-convert");
|
|
473
673
|
var external_color_convert_default = /*#__PURE__*/__webpack_require__.n(external_color_convert_namespaceObject);
|
|
474
|
-
;// CONCATENATED MODULE: ./src/
|
|
674
|
+
;// CONCATENATED MODULE: ./src/CssColorParserPlus.ts
|
|
675
|
+
/*
|
|
676
|
+
* @Descripttion: 颜色解析器(增强)
|
|
677
|
+
* @version: 1.0.0
|
|
678
|
+
* @Author: roman_123
|
|
679
|
+
* @Date: 2021-01-19 09:22:11
|
|
680
|
+
* @LastEditors: Please set LastEditors
|
|
681
|
+
* @LastEditTime: 2023-06-27 18:53:31
|
|
682
|
+
*/
|
|
475
683
|
|
|
476
684
|
|
|
477
685
|
|
|
478
686
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
*/
|
|
485
|
-
function parseKeyWord(v) {
|
|
486
|
-
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
487
|
-
var res = external_color_convert_default().keyword.rgb(cssStr);
|
|
488
|
-
return res && fromArray(res);
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* @description: 解析16进制字符串
|
|
492
|
-
* @param {string} v
|
|
493
|
-
* @return {CssColorParser}
|
|
494
|
-
* @example: parseHEX('#FFF')
|
|
495
|
-
*/
|
|
496
|
-
function parseHEX(v) {
|
|
497
|
-
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
498
|
-
var res = CssColorStringParser.parse3BitsHEX(cssStr);
|
|
499
|
-
if (!res) {
|
|
500
|
-
res = CssColorStringParser.parse6BitsHEX(cssStr);
|
|
501
|
-
}
|
|
502
|
-
return res && fromArray(res);
|
|
503
|
-
}
|
|
504
|
-
/**
|
|
505
|
-
* @description: 解析RGBA
|
|
506
|
-
* @param {string} v
|
|
507
|
-
* @return {CssColorParser}
|
|
508
|
-
* @example: parseRGBA('rgba(255,255,255,1)')
|
|
509
|
-
*/
|
|
510
|
-
function parseRGBA(v) {
|
|
511
|
-
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
512
|
-
var res = CssColorStringParser.parseRGBA(cssStr);
|
|
513
|
-
if (!res) {
|
|
514
|
-
var cssStr2 = CssColorStringParser.trimStr(v);
|
|
515
|
-
res = CssColorStringParser.parseRGBA2(cssStr2);
|
|
687
|
+
|
|
688
|
+
var CssColorParserPlus = /** @class */ (function (_super) {
|
|
689
|
+
(0,external_tslib_namespaceObject.__extends)(CssColorParserPlus, _super);
|
|
690
|
+
function CssColorParserPlus() {
|
|
691
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
516
692
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
693
|
+
/**
|
|
694
|
+
* @description: 返回取反色后的新的实例
|
|
695
|
+
* @return {CssColorParserPlus}
|
|
696
|
+
*/
|
|
697
|
+
CssColorParserPlus.prototype.toInvert = function () {
|
|
698
|
+
var r = 255 - this.r;
|
|
699
|
+
var g = 255 - this.g;
|
|
700
|
+
var b = 255 - this.b;
|
|
701
|
+
var a = 1 - this.a;
|
|
702
|
+
return new CssColorParserPlus(r, g, b, a);
|
|
703
|
+
};
|
|
704
|
+
/**
|
|
705
|
+
* @description: 拷贝
|
|
706
|
+
* @return {CssColorParserPlus}
|
|
707
|
+
*/
|
|
708
|
+
CssColorParserPlus.prototype.clone = function () {
|
|
709
|
+
return new CssColorParserPlus(this.r, this.g, this.b, this.a);
|
|
710
|
+
};
|
|
711
|
+
/**
|
|
712
|
+
* @description: 比较两个解析对象的数据是否相等
|
|
713
|
+
* @param {string} color
|
|
714
|
+
* @return {boolean}
|
|
715
|
+
*/
|
|
716
|
+
CssColorParserPlus.prototype.equals = function (color) {
|
|
717
|
+
color = CssColorParserPlus.parseColor(color);
|
|
718
|
+
if (this === color) {
|
|
719
|
+
return true;
|
|
720
|
+
}
|
|
721
|
+
else {
|
|
722
|
+
var json1 = this.toJson();
|
|
723
|
+
var json2 = color.toJson();
|
|
724
|
+
return (json1.r === json2.r &&
|
|
725
|
+
json1.g === json2.g &&
|
|
726
|
+
json1.b === json2.g &&
|
|
727
|
+
json1.a === json2.a);
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
/**
|
|
731
|
+
* @description: 实例相加
|
|
732
|
+
* @param {CssColorParser} colorParser
|
|
733
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
734
|
+
* @return {CssColorParser}
|
|
735
|
+
*/
|
|
736
|
+
CssColorParserPlus.prototype.add = function (color, isSetAlpha) {
|
|
737
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
738
|
+
var colorParser = CssColorParserPlus.parseColor(color);
|
|
739
|
+
return _super.prototype.add.call(this, colorParser, isSetAlpha);
|
|
740
|
+
};
|
|
741
|
+
/**
|
|
742
|
+
* @description: 实例相减
|
|
743
|
+
* @param {CssColorParser} colorParser
|
|
744
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
745
|
+
* @return {CssColorParser}
|
|
746
|
+
*/
|
|
747
|
+
CssColorParserPlus.prototype.subtract = function (color, isSetAlpha) {
|
|
748
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
749
|
+
var colorParser = CssColorParserPlus.parseColor(color);
|
|
750
|
+
return _super.prototype.subtract.call(this, colorParser, isSetAlpha);
|
|
751
|
+
};
|
|
752
|
+
/**
|
|
753
|
+
* @description: 实例相乘
|
|
754
|
+
* @param {CssColorParser} colorParser
|
|
755
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
756
|
+
* @return {CssColorParser}
|
|
757
|
+
*/
|
|
758
|
+
CssColorParserPlus.prototype.multiply = function (color, isSetAlpha) {
|
|
759
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
760
|
+
var colorParser = CssColorParserPlus.parseColor(color);
|
|
761
|
+
return _super.prototype.multiply.call(this, colorParser, isSetAlpha);
|
|
762
|
+
};
|
|
763
|
+
/**
|
|
764
|
+
* @description: 实例相除
|
|
765
|
+
* @param {CssColorParser} colorParser
|
|
766
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
767
|
+
* @return {CssColorParser}
|
|
768
|
+
*/
|
|
769
|
+
CssColorParserPlus.prototype.divide = function (color, isSetAlpha) {
|
|
770
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
771
|
+
var colorParser = CssColorParserPlus.parseColor(color);
|
|
772
|
+
return _super.prototype.divide.call(this, colorParser, isSetAlpha);
|
|
773
|
+
};
|
|
774
|
+
/**
|
|
775
|
+
* @description: 解析css颜色
|
|
776
|
+
* @param {string} v
|
|
777
|
+
* @return {CssColorParserPlus}
|
|
778
|
+
* @example: parseCssColorStr('rgba(255,255,255,1)')
|
|
779
|
+
*/
|
|
780
|
+
CssColorParserPlus.parseColor = function (v) {
|
|
781
|
+
if (v instanceof src_CssColorParser) {
|
|
782
|
+
return v;
|
|
783
|
+
}
|
|
784
|
+
return CssColorParserPlus.parseCssColorStr(v);
|
|
785
|
+
};
|
|
786
|
+
/**
|
|
787
|
+
* @description: 将css字符串转换为解析对象
|
|
788
|
+
* @param {string} v
|
|
789
|
+
* @return {CssColorParserPlus}
|
|
790
|
+
* @example: parseCssColorStr('rgba(255,255,255,1)')
|
|
791
|
+
*/
|
|
792
|
+
CssColorParserPlus.parseCssColorStr = function (v) {
|
|
793
|
+
Check.type('color', v, 'string');
|
|
794
|
+
return (CssColorParserPlus.parseHEX(v) ||
|
|
795
|
+
CssColorParserPlus.parseRGBA(v) ||
|
|
796
|
+
CssColorParserPlus.parseKeyWord(v) ||
|
|
797
|
+
CssColorParserPlus.parseHSLA(v) ||
|
|
798
|
+
CssColorParserPlus.parseHWB(v));
|
|
799
|
+
};
|
|
800
|
+
/**
|
|
801
|
+
* @description: 解析颜色关键字
|
|
802
|
+
* @param {string} v
|
|
803
|
+
* @return {CssColorParserPlus}
|
|
804
|
+
* @example: parseKeyWord('red')
|
|
805
|
+
*/
|
|
806
|
+
CssColorParserPlus.parseKeyWord = function (v) {
|
|
807
|
+
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
808
|
+
var res = external_color_convert_default().keyword.rgb(cssStr);
|
|
809
|
+
return res && CssColorParserPlus.fromArray(res);
|
|
810
|
+
};
|
|
811
|
+
/**
|
|
812
|
+
* @description: 解析HSLA
|
|
813
|
+
* @param {string} v
|
|
814
|
+
* @return {CssColorParserPlus}
|
|
815
|
+
* @example: parseHSLA('hsla(215,85%,62%,0.8)')
|
|
816
|
+
*/
|
|
817
|
+
CssColorParserPlus.parseHSLA = function (v) {
|
|
818
|
+
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
819
|
+
var res = CssColorStringParser.parseHSLA(cssStr);
|
|
820
|
+
if (!res) {
|
|
821
|
+
var cssStr2 = CssColorStringParser.trimStr(v);
|
|
822
|
+
res = CssColorStringParser.parseHSLA2(cssStr2);
|
|
823
|
+
}
|
|
824
|
+
return res && CssColorParserPlus.fromHSL(res[0], res[1], res[2], res[3]);
|
|
825
|
+
};
|
|
826
|
+
/**
|
|
827
|
+
* @description: 解析HWB
|
|
828
|
+
* @param {string} v
|
|
829
|
+
* @return {CssColorParserPlus}
|
|
830
|
+
* @example: parseHWB('hwb(215deg 30% 6% / 80%)')
|
|
831
|
+
*/
|
|
832
|
+
CssColorParserPlus.parseHWB = function (v) {
|
|
529
833
|
var cssStr2 = CssColorStringParser.trimStr(v);
|
|
530
|
-
res = CssColorStringParser.
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
*
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
}
|
|
834
|
+
var res = CssColorStringParser.parseHWB(cssStr2);
|
|
835
|
+
return res && CssColorParserPlus.fromHWB(res[0], res[1], res[2], res[3]);
|
|
836
|
+
};
|
|
837
|
+
/**
|
|
838
|
+
* @description: 将HSL色彩模式转换为解析对象
|
|
839
|
+
* @param {number} hue 色相
|
|
840
|
+
* @param {number} saturation 饱和度
|
|
841
|
+
* @param {number} lightness 亮度
|
|
842
|
+
* @param {number} alpha 不透明度
|
|
843
|
+
* @return {CssColorParserPlus}
|
|
844
|
+
* @example: fromHSL(0,1,1,1)
|
|
845
|
+
*/
|
|
846
|
+
CssColorParserPlus.fromHSL = function (h, s, l, a) {
|
|
847
|
+
var res = external_color_convert_default().hsl.rgb(limitNumber(0, 360, h), limitNumber(0, 100, s * 100), limitNumber(0, 100, l * 100));
|
|
848
|
+
return new CssColorParserPlus(res[0], res[1], res[2], defaultValue(Number(a), 1));
|
|
849
|
+
};
|
|
850
|
+
/**
|
|
851
|
+
* @description: 将HWB色彩模式转换为解析对象
|
|
852
|
+
* @param {number} h 色调
|
|
853
|
+
* @param {number} w 白度
|
|
854
|
+
* @param {number} b 黑度
|
|
855
|
+
* @param {number} a 不透明度
|
|
856
|
+
* @return {CssColorParserPlus}
|
|
857
|
+
* @example: fromHSL(0,1,1,1)
|
|
858
|
+
*/
|
|
859
|
+
CssColorParserPlus.fromHWB = function (h, w, b, a) {
|
|
860
|
+
var res = external_color_convert_default().hwb.rgb(limitNumber(0, 360, h), limitNumber(0, 100, w * 100), limitNumber(0, 100, b * 100));
|
|
861
|
+
return new CssColorParserPlus(res[0], res[1], res[2], defaultValue(Number(a), 1));
|
|
862
|
+
};
|
|
863
|
+
/**
|
|
864
|
+
* @description: 解析16进制颜色
|
|
865
|
+
* @param {string} v
|
|
866
|
+
* @return {CssColorParserPlus}
|
|
867
|
+
* @example: CssColorParserPlus.parseHEX('#FFF')
|
|
868
|
+
*/
|
|
869
|
+
CssColorParserPlus.parseHEX = function (v) {
|
|
870
|
+
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
871
|
+
var res = CssColorStringParser.parse3BitsHEX(cssStr);
|
|
872
|
+
if (!res) {
|
|
873
|
+
res = CssColorStringParser.parse6BitsHEX(cssStr);
|
|
874
|
+
}
|
|
875
|
+
return res && CssColorParserPlus.fromArray(res);
|
|
876
|
+
};
|
|
877
|
+
/**
|
|
878
|
+
* @description: 解析rgba、rgb颜色
|
|
879
|
+
* @param {string} v
|
|
880
|
+
* @return {CssColorParserPlus}
|
|
881
|
+
* @example: CssColorParserPlus.parseRGBA('rgba(255,255,255,1)')
|
|
882
|
+
*/
|
|
883
|
+
CssColorParserPlus.parseRGBA = function (v) {
|
|
884
|
+
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
885
|
+
var res = CssColorStringParser.parseRGBA(cssStr);
|
|
886
|
+
if (!res) {
|
|
887
|
+
var cssStr2 = CssColorStringParser.trimStr(v);
|
|
888
|
+
res = CssColorStringParser.parseRGBA2(cssStr2);
|
|
889
|
+
}
|
|
890
|
+
return res && CssColorParserPlus.fromArray(res);
|
|
891
|
+
};
|
|
892
|
+
/**
|
|
893
|
+
* @description: 将ColorJson格式的json数据转换为解析对象
|
|
894
|
+
* @param {ColorJson} json
|
|
895
|
+
* @return {CssColorParserPlus}
|
|
896
|
+
* @example: CssColorParserPlus.fromJson({r: 255, g: 255, b: 255, a: 1})
|
|
897
|
+
*/
|
|
898
|
+
CssColorParserPlus.fromJson = function (json) {
|
|
899
|
+
return new CssColorParserPlus(json.r, json.g, json.b, json.a);
|
|
900
|
+
};
|
|
901
|
+
/**
|
|
902
|
+
* @description: 将RGBA数组转换为解析对象
|
|
903
|
+
* @param {Array} color
|
|
904
|
+
* @return {CssColorParserPlus}
|
|
905
|
+
* @example: CssColorParserPlus.fromArray([255,255,255,1])
|
|
906
|
+
*/
|
|
907
|
+
CssColorParserPlus.fromArray = function (color) {
|
|
908
|
+
return new CssColorParserPlus(color[0], color[1], color[2], color[3]);
|
|
909
|
+
};
|
|
910
|
+
/**
|
|
911
|
+
* @description: 产生随机颜色
|
|
912
|
+
* @return {CssColorParserPlus}
|
|
913
|
+
* @example: CssColorParserPlus.fromRandom('black', new CssColorParserPlus(255,255,255,1))
|
|
914
|
+
*/
|
|
915
|
+
CssColorParserPlus.fromRandom = function (color1, color2) {
|
|
916
|
+
color1 = CssColorParserPlus.parseColor(color1);
|
|
917
|
+
color2 = CssColorParserPlus.parseColor(color2);
|
|
918
|
+
var r = Math.random() * Math.abs(color2.r - color1.r) +
|
|
919
|
+
Math.min(color1.r, color2.r);
|
|
920
|
+
var g = Math.random() * Math.abs(color2.g - color1.g) +
|
|
921
|
+
Math.min(color1.g, color2.g);
|
|
922
|
+
var b = Math.random() * Math.abs(color2.b - color1.b) +
|
|
923
|
+
Math.min(color1.b, color2.b);
|
|
924
|
+
var a = Math.random() * Math.abs(color2.a - color1.a) +
|
|
925
|
+
Math.min(color1.a, color2.a);
|
|
926
|
+
return new CssColorParserPlus(r, g, b, a);
|
|
927
|
+
};
|
|
928
|
+
/**
|
|
929
|
+
* @description: 颜色序列化数组转换为CssColorParserPlus对象实例
|
|
930
|
+
* @param {array} colorArr
|
|
931
|
+
* @example: CssColorParserPlus.fromNormaliz([1, 0, 0, 1])
|
|
932
|
+
*/
|
|
933
|
+
CssColorParserPlus.fromNormalize = function (colorArr) {
|
|
934
|
+
var r = colorArr[0] * 255;
|
|
935
|
+
var g = colorArr[1] * 255;
|
|
936
|
+
var b = colorArr[2] * 255;
|
|
937
|
+
var a = colorArr[3];
|
|
938
|
+
return CssColorParserPlus.fromArray([r, g, b, a]);
|
|
939
|
+
};
|
|
940
|
+
return CssColorParserPlus;
|
|
941
|
+
}(src_CssColorParser));
|
|
942
|
+
/* harmony default export */ var src_CssColorParserPlus = (CssColorParserPlus);
|
|
636
943
|
|
|
637
944
|
;// CONCATENATED MODULE: ./src/main.ts
|
|
638
945
|
/*
|
|
639
946
|
* @Author: roman_123
|
|
640
947
|
* @Description:
|
|
641
948
|
* @Date: 2023-05-25 17:45:22
|
|
642
|
-
* @LastEditTime: 2023-
|
|
949
|
+
* @LastEditTime: 2023-06-27 18:45:32
|
|
643
950
|
*/
|
|
644
951
|
|
|
645
952
|
|