css-color-parser-h 2.0.3 → 2.0.4
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 +308 -6
- package/dist/css-color-parser-h.common.js +258 -69
- package/dist/css-color-parser-h.common.min.js +1 -1
- package/dist/css-color-parser-h.umd.js +258 -69
- package/dist/css-color-parser-h.umd.min.js +1 -1
- package/example.html +3 -3
- package/package.json +6 -4
- package/readme.md +57 -28
- package/@types/css-color-parser-h.d.ts +0 -230
|
@@ -70,19 +70,39 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
70
70
|
var Check = /** @class */ (function () {
|
|
71
71
|
function Check() {
|
|
72
72
|
}
|
|
73
|
-
Check.type = function (
|
|
73
|
+
Check.type = function (paramName, value, type) {
|
|
74
74
|
var valueType = typeof value;
|
|
75
75
|
if (valueType !== type) {
|
|
76
76
|
throw new Error("Expected ".concat(paramName, " to be typeof ").concat(type, ", actual typeof was ").concat(valueType));
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
|
-
Check.types = function (
|
|
79
|
+
Check.types = function (paramName, value, types) {
|
|
80
80
|
var valueType = typeof value;
|
|
81
81
|
var isContained = types.includes(valueType);
|
|
82
82
|
if (!isContained) {
|
|
83
83
|
throw new Error("Expected ".concat(paramName, " to be typeof ").concat(types.join('|'), ", actual typeof was ").concat(valueType));
|
|
84
84
|
}
|
|
85
85
|
};
|
|
86
|
+
Check.numValue = function (paramName, value, min, max) {
|
|
87
|
+
Check.numMinValue(paramName, value, min);
|
|
88
|
+
Check.numMaxValue(paramName, value, max);
|
|
89
|
+
};
|
|
90
|
+
Check.numMinValue = function (paramName, value, min) {
|
|
91
|
+
if (value < min) {
|
|
92
|
+
throw new Error("Expected ".concat(paramName, " to > ").concat(min, ", actual value was ").concat(value));
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
Check.numMaxValue = function (paramName, value, max) {
|
|
96
|
+
if (value > max) {
|
|
97
|
+
throw new Error("Expected ".concat(paramName, " to < ").concat(max, ", actual value was ").concat(value));
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
Check.numIsInt = function (paramName, value, expectIsInt) {
|
|
101
|
+
var isInt = Math.floor(value) === value;
|
|
102
|
+
if (isInt !== expectIsInt) {
|
|
103
|
+
throw new Error("Expected ".concat(paramName, " to ").concat(expectIsInt ? 'Integer' : 'Float', ", actual value was ").concat(isInt ? 'Integer' : 'Float'));
|
|
104
|
+
}
|
|
105
|
+
};
|
|
86
106
|
return Check;
|
|
87
107
|
}());
|
|
88
108
|
|
|
@@ -111,7 +131,9 @@ function limitNumber(min, max, v) {
|
|
|
111
131
|
return v;
|
|
112
132
|
}
|
|
113
133
|
function setNumberPrecision(number, precision) {
|
|
114
|
-
|
|
134
|
+
if (number === void 0) { number = 0; }
|
|
135
|
+
var prec = Math.pow(10, precision);
|
|
136
|
+
return Math.round(number * prec) / prec;
|
|
115
137
|
}
|
|
116
138
|
|
|
117
139
|
;// CONCATENATED MODULE: ./src/utils/color-tools.ts
|
|
@@ -265,8 +287,8 @@ var CssColorStringParser = /** @class */ (function () {
|
|
|
265
287
|
* @version: 1.0.0
|
|
266
288
|
* @Author: roman_123
|
|
267
289
|
* @Date: 2021-01-19 09:22:11
|
|
268
|
-
* @LastEditors:
|
|
269
|
-
* @LastEditTime: 2023-
|
|
290
|
+
* @LastEditors: Roman
|
|
291
|
+
* @LastEditTime: 2023-06-03 11:12:30
|
|
270
292
|
*/
|
|
271
293
|
|
|
272
294
|
|
|
@@ -276,8 +298,27 @@ var CssColorParser = /** @class */ (function () {
|
|
|
276
298
|
this.g = 255;
|
|
277
299
|
this.b = 255;
|
|
278
300
|
this.a = 1;
|
|
301
|
+
this._outColorPrecision = 2;
|
|
302
|
+
this._outAlphaPrecision = 2;
|
|
279
303
|
this.setColor(red, green, blue, alpha);
|
|
280
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* @description: 设置CssColorParser实例输出的精度
|
|
307
|
+
* @param {number} colorPrecision 输出颜色的精度
|
|
308
|
+
* @param {number} outAlphaPrecision 输出透明度的精度
|
|
309
|
+
* @return {CssColorParser}
|
|
310
|
+
*/
|
|
311
|
+
CssColorParser.prototype.setOutPrecision = function (colorPrecision, outAlphaPrecision) {
|
|
312
|
+
Check.type('colorPrecision', colorPrecision, 'number');
|
|
313
|
+
Check.type('outAlphaPrecision', outAlphaPrecision, 'number');
|
|
314
|
+
Check.numMinValue('colorPrecision', colorPrecision, 0);
|
|
315
|
+
Check.numMinValue('outAlphaPrecision', outAlphaPrecision, 0);
|
|
316
|
+
Check.numIsInt('colorPrecision', colorPrecision, true);
|
|
317
|
+
Check.numIsInt('outAlphaPrecision', outAlphaPrecision, true);
|
|
318
|
+
this._outColorPrecision = colorPrecision;
|
|
319
|
+
this._outAlphaPrecision = outAlphaPrecision;
|
|
320
|
+
return this;
|
|
321
|
+
};
|
|
281
322
|
/**
|
|
282
323
|
* 设置颜色
|
|
283
324
|
* @param red
|
|
@@ -291,11 +332,47 @@ var CssColorParser = /** @class */ (function () {
|
|
|
291
332
|
this.g = limitNumber(0, 255, defaultValue(Number(green), 0));
|
|
292
333
|
this.b = limitNumber(0, 255, defaultValue(Number(blue), 0));
|
|
293
334
|
this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1));
|
|
335
|
+
return this;
|
|
336
|
+
};
|
|
337
|
+
/**
|
|
338
|
+
* @description: 设置透明度
|
|
339
|
+
* @param {number} alpha
|
|
340
|
+
* @return {CssColorParser}
|
|
341
|
+
*/
|
|
342
|
+
CssColorParser.prototype.setAlpha = function (alpha) {
|
|
343
|
+
this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1));
|
|
344
|
+
return this;
|
|
345
|
+
};
|
|
346
|
+
/**
|
|
347
|
+
* @description: 设置红色值
|
|
348
|
+
* @param {number} red
|
|
349
|
+
* @return {CssColorParser}
|
|
350
|
+
*/
|
|
351
|
+
CssColorParser.prototype.setRed = function (red) {
|
|
352
|
+
this.r = limitNumber(0, 255, defaultValue(Number(red), 0));
|
|
353
|
+
return this;
|
|
354
|
+
};
|
|
355
|
+
/**
|
|
356
|
+
* @description: 设置绿色值
|
|
357
|
+
* @param {number} green
|
|
358
|
+
* @return {CssColorParser}
|
|
359
|
+
*/
|
|
360
|
+
CssColorParser.prototype.setGreen = function (green) {
|
|
361
|
+
this.g = limitNumber(0, 255, defaultValue(Number(green), 0));
|
|
362
|
+
return this;
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* @description: 设置蓝色值
|
|
366
|
+
* @param {number} blue
|
|
367
|
+
* @return {CssColorParser}
|
|
368
|
+
*/
|
|
369
|
+
CssColorParser.prototype.setBlue = function (blue) {
|
|
370
|
+
this.b = limitNumber(0, 255, defaultValue(Number(blue), 0));
|
|
371
|
+
return this;
|
|
294
372
|
};
|
|
295
373
|
/**
|
|
296
374
|
* @description: 返回rgba格式的css字符串
|
|
297
|
-
* @return {
|
|
298
|
-
* @author: roman_123
|
|
375
|
+
* @return {string}
|
|
299
376
|
*/
|
|
300
377
|
CssColorParser.prototype.toRGBA = function () {
|
|
301
378
|
var color = this.toJson();
|
|
@@ -306,29 +383,25 @@ var CssColorParser = /** @class */ (function () {
|
|
|
306
383
|
};
|
|
307
384
|
/**
|
|
308
385
|
* @description: 返回字符串
|
|
309
|
-
* @return {
|
|
310
|
-
* @author: roman_123
|
|
386
|
+
* @return {string}
|
|
311
387
|
*/
|
|
312
388
|
CssColorParser.prototype.toString = function () {
|
|
313
389
|
return this.toRGBA();
|
|
314
390
|
};
|
|
315
391
|
/**
|
|
316
392
|
* @description: 归一化
|
|
317
|
-
* @return {
|
|
318
|
-
* @author: roman_123
|
|
393
|
+
* @return {array}
|
|
319
394
|
*/
|
|
320
395
|
CssColorParser.prototype.toNormalize = function () {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
};
|
|
396
|
+
var r = setNumberPrecision(this.r / 255, this._outColorPrecision);
|
|
397
|
+
var g = setNumberPrecision(this.g / 255, this._outColorPrecision);
|
|
398
|
+
var b = setNumberPrecision(this.b / 255, this._outColorPrecision);
|
|
399
|
+
var a = setNumberPrecision(this.a, this._outAlphaPrecision);
|
|
400
|
+
return [r, g, b, a];
|
|
327
401
|
};
|
|
328
402
|
/**
|
|
329
403
|
* @description: 返回16进制格式的css字符串
|
|
330
|
-
* @return {
|
|
331
|
-
* @author: roman_123
|
|
404
|
+
* @return {string}
|
|
332
405
|
*/
|
|
333
406
|
CssColorParser.prototype.toHEX = function () {
|
|
334
407
|
var color = this.toJson();
|
|
@@ -356,8 +429,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
356
429
|
};
|
|
357
430
|
/**
|
|
358
431
|
* @description: 返回rgba数组
|
|
359
|
-
* @return {
|
|
360
|
-
* @author: roman_123
|
|
432
|
+
* @return {array}
|
|
361
433
|
*/
|
|
362
434
|
CssColorParser.prototype.toArray = function () {
|
|
363
435
|
var color = this.toJson();
|
|
@@ -365,47 +437,172 @@ var CssColorParser = /** @class */ (function () {
|
|
|
365
437
|
};
|
|
366
438
|
/**
|
|
367
439
|
* @description: 返回ColorJson
|
|
368
|
-
* @return {
|
|
369
|
-
* @author: roman_123
|
|
440
|
+
* @return {ColorJson}
|
|
370
441
|
*/
|
|
371
442
|
CssColorParser.prototype.toJson = function () {
|
|
372
443
|
return {
|
|
373
|
-
r:
|
|
374
|
-
g:
|
|
375
|
-
b:
|
|
376
|
-
a:
|
|
444
|
+
r: setNumberPrecision(this.r, this._outColorPrecision),
|
|
445
|
+
g: setNumberPrecision(this.g, this._outColorPrecision),
|
|
446
|
+
b: setNumberPrecision(this.b, this._outColorPrecision),
|
|
447
|
+
a: setNumberPrecision(this.a, this._outAlphaPrecision),
|
|
377
448
|
};
|
|
378
449
|
};
|
|
450
|
+
/**
|
|
451
|
+
* @description: 返回反色的CssColorParser实例
|
|
452
|
+
* @return {CssColorParser}
|
|
453
|
+
*/
|
|
454
|
+
CssColorParser.prototype.toInvert = function () {
|
|
455
|
+
var r = 255 - this.r;
|
|
456
|
+
var g = 255 - this.g;
|
|
457
|
+
var b = 255 - this.b;
|
|
458
|
+
var a = 1 - this.a;
|
|
459
|
+
return new CssColorParser(r, g, b, a);
|
|
460
|
+
};
|
|
379
461
|
/**
|
|
380
462
|
* @description: 拷贝
|
|
381
|
-
* @return {
|
|
382
|
-
* @author: roman_123
|
|
463
|
+
* @return {CssColorParser}
|
|
383
464
|
*/
|
|
384
465
|
CssColorParser.prototype.clone = function () {
|
|
385
466
|
return new CssColorParser(this.r, this.g, this.b, this.a);
|
|
386
467
|
};
|
|
387
468
|
/**
|
|
388
469
|
* @description: 比较两个解析对象的数据是否相等
|
|
389
|
-
* @param {
|
|
390
|
-
* @return {
|
|
391
|
-
* @author: roman_123
|
|
470
|
+
* @param {string} color
|
|
471
|
+
* @return {boolean}
|
|
392
472
|
*/
|
|
393
473
|
CssColorParser.prototype.equals = function (color) {
|
|
394
474
|
if (this === color) {
|
|
395
475
|
return true;
|
|
396
476
|
}
|
|
397
477
|
else {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
478
|
+
var json1 = this.toJson();
|
|
479
|
+
var json2 = color.toJson();
|
|
480
|
+
return (json1.r === json2.r &&
|
|
481
|
+
json1.g === json2.g &&
|
|
482
|
+
json1.b === json2.g &&
|
|
483
|
+
json1.a === json2.a);
|
|
402
484
|
}
|
|
403
485
|
};
|
|
486
|
+
/**
|
|
487
|
+
* @description: 反色
|
|
488
|
+
* @return {CssColorParser}
|
|
489
|
+
*/
|
|
490
|
+
CssColorParser.prototype.setInvert = function () {
|
|
491
|
+
this.r = 255 - this.r;
|
|
492
|
+
this.g = 255 - this.g;
|
|
493
|
+
this.b = 255 - this.b;
|
|
494
|
+
this.a = 1 - this.a;
|
|
495
|
+
return this;
|
|
496
|
+
};
|
|
497
|
+
/**
|
|
498
|
+
* @description: 乘以倍数
|
|
499
|
+
* @param {number} scalar
|
|
500
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
501
|
+
* @return {CssColorParser}
|
|
502
|
+
*/
|
|
503
|
+
CssColorParser.prototype.multiplyByScalar = function (scalar, isSetAlpha) {
|
|
504
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
505
|
+
var r = this.r * scalar;
|
|
506
|
+
var g = this.g * scalar;
|
|
507
|
+
var b = this.b * scalar;
|
|
508
|
+
var a = isSetAlpha ? this.a * scalar : this.a;
|
|
509
|
+
return this.setColor(r, g, b, a);
|
|
510
|
+
};
|
|
511
|
+
/**
|
|
512
|
+
* @description: 除以倍数
|
|
513
|
+
* @param {number} scalar
|
|
514
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
515
|
+
* @return {CssColorParser}
|
|
516
|
+
*/
|
|
517
|
+
CssColorParser.prototype.divideByScalar = function (scalar, isSetAlpha) {
|
|
518
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
519
|
+
var r = this.r / scalar;
|
|
520
|
+
var g = this.g / scalar;
|
|
521
|
+
var b = this.b / scalar;
|
|
522
|
+
var a = isSetAlpha ? this.a / scalar : this.a;
|
|
523
|
+
return this.setColor(r, g, b, a);
|
|
524
|
+
};
|
|
525
|
+
/**
|
|
526
|
+
* @description: 实例相加
|
|
527
|
+
* @param {CssColorParser} colorParser
|
|
528
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
529
|
+
* @return {CssColorParser}
|
|
530
|
+
*/
|
|
531
|
+
CssColorParser.prototype.add = function (colorParser, isSetAlpha) {
|
|
532
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
533
|
+
var r = this.r + colorParser.r;
|
|
534
|
+
var g = this.g + colorParser.g;
|
|
535
|
+
var b = this.b + colorParser.b;
|
|
536
|
+
var a = isSetAlpha ? this.a + colorParser.a : this.a;
|
|
537
|
+
return this.setColor(r, g, b, a);
|
|
538
|
+
};
|
|
539
|
+
/**
|
|
540
|
+
* @description: 实例相减
|
|
541
|
+
* @param {CssColorParser} colorParser
|
|
542
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
543
|
+
* @return {CssColorParser}
|
|
544
|
+
*/
|
|
545
|
+
CssColorParser.prototype.subtract = function (colorParser, isSetAlpha) {
|
|
546
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
547
|
+
var r = this.r - colorParser.r;
|
|
548
|
+
var g = this.g - colorParser.g;
|
|
549
|
+
var b = this.b - colorParser.b;
|
|
550
|
+
var a = isSetAlpha ? this.a - colorParser.a : this.a;
|
|
551
|
+
return this.setColor(r, g, b, a);
|
|
552
|
+
};
|
|
553
|
+
/**
|
|
554
|
+
* @description: 实例相乘
|
|
555
|
+
* @param {CssColorParser} colorParser
|
|
556
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
557
|
+
* @return {CssColorParser}
|
|
558
|
+
*/
|
|
559
|
+
CssColorParser.prototype.multiply = function (colorParser, isSetAlpha) {
|
|
560
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
561
|
+
var r = this.r * colorParser.r;
|
|
562
|
+
var g = this.g * colorParser.g;
|
|
563
|
+
var b = this.b * colorParser.b;
|
|
564
|
+
var a = isSetAlpha ? this.a * colorParser.a : this.a;
|
|
565
|
+
return this.setColor(r, g, b, a);
|
|
566
|
+
};
|
|
567
|
+
/**
|
|
568
|
+
* @description: 实例相除
|
|
569
|
+
* @param {CssColorParser} colorParser
|
|
570
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
571
|
+
* @return {CssColorParser}
|
|
572
|
+
*/
|
|
573
|
+
CssColorParser.prototype.divide = function (colorParser, isSetAlpha) {
|
|
574
|
+
if (isSetAlpha === void 0) { isSetAlpha = true; }
|
|
575
|
+
var r = this.r / colorParser.r;
|
|
576
|
+
var g = this.g / colorParser.g;
|
|
577
|
+
var b = this.b / colorParser.b;
|
|
578
|
+
var a = isSetAlpha ? this.a / colorParser.a : this.a;
|
|
579
|
+
return this.setColor(r, g, b, a);
|
|
580
|
+
};
|
|
581
|
+
/**
|
|
582
|
+
* @description: 颜色RGB加上数字
|
|
583
|
+
* @param {number} num
|
|
584
|
+
* @return {CssColorParser}
|
|
585
|
+
*/
|
|
586
|
+
CssColorParser.prototype.addNumberForRGB = function (num) {
|
|
587
|
+
this.r = this.r + num;
|
|
588
|
+
this.g = this.g + num;
|
|
589
|
+
this.b = this.b + num;
|
|
590
|
+
return this;
|
|
591
|
+
};
|
|
592
|
+
/**
|
|
593
|
+
* @description: 透明度加上数字
|
|
594
|
+
* @param {number} num
|
|
595
|
+
* @return {CssColorParser}
|
|
596
|
+
*/
|
|
597
|
+
CssColorParser.prototype.addNumberForAlpha = function (num) {
|
|
598
|
+
this.a = this.a + num;
|
|
599
|
+
return this;
|
|
600
|
+
};
|
|
404
601
|
/**
|
|
405
602
|
* @description: 解析16进制颜色
|
|
406
603
|
* @param {string} v
|
|
407
604
|
* @return {CssColorParser}
|
|
408
|
-
* @example: parseHEX('#FFF')
|
|
605
|
+
* @example: CssColorParser.parseHEX('#FFF')
|
|
409
606
|
*/
|
|
410
607
|
CssColorParser.parseHEX = function (v) {
|
|
411
608
|
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
@@ -419,7 +616,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
419
616
|
* @description: 解析rgba、rgb颜色
|
|
420
617
|
* @param {string} v
|
|
421
618
|
* @return {CssColorParser}
|
|
422
|
-
* @example: parseRGBA('rgba(255,255,255,1)')
|
|
619
|
+
* @example: CssColorParser.parseRGBA('rgba(255,255,255,1)')
|
|
423
620
|
*/
|
|
424
621
|
CssColorParser.parseRGBA = function (v) {
|
|
425
622
|
var cssStr = CssColorStringParser.clearStrSpace(v);
|
|
@@ -434,7 +631,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
434
631
|
* @description: 将ColorJson格式的json数据转换为解析对象
|
|
435
632
|
* @param {ColorJson} json
|
|
436
633
|
* @return {CssColorParser}
|
|
437
|
-
* @example: fromJson({r: 255, g: 255, b: 255, a: 1})
|
|
634
|
+
* @example: CssColorParser.fromJson({r: 255, g: 255, b: 255, a: 1})
|
|
438
635
|
*/
|
|
439
636
|
CssColorParser.fromJson = function (json) {
|
|
440
637
|
return new CssColorParser(json.r, json.g, json.b, json.a);
|
|
@@ -443,7 +640,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
443
640
|
* @description: 将RGBA数组转换为解析对象
|
|
444
641
|
* @param {Array} color
|
|
445
642
|
* @return {CssColorParser}
|
|
446
|
-
* @example:
|
|
643
|
+
* @example: CssColorParser.fromArray([255,255,255,1])
|
|
447
644
|
*/
|
|
448
645
|
CssColorParser.fromArray = function (color) {
|
|
449
646
|
return new CssColorParser(color[0], color[1], color[2], color[3]);
|
|
@@ -451,7 +648,7 @@ var CssColorParser = /** @class */ (function () {
|
|
|
451
648
|
/**
|
|
452
649
|
* @description: 产生随机颜色
|
|
453
650
|
* @return {CssColorParser}
|
|
454
|
-
* @example: fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
|
|
651
|
+
* @example: CssColorParser.fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
|
|
455
652
|
*/
|
|
456
653
|
CssColorParser.fromRandom = function (color1, color2) {
|
|
457
654
|
var r = Math.random() * Math.abs(color2.r - color1.r) +
|
|
@@ -464,6 +661,18 @@ var CssColorParser = /** @class */ (function () {
|
|
|
464
661
|
Math.min(color1.a, color2.a);
|
|
465
662
|
return new CssColorParser(r, g, b, a);
|
|
466
663
|
};
|
|
664
|
+
/**
|
|
665
|
+
* @description: 颜色序列化数组转换为CssColorParser对象实例
|
|
666
|
+
* @param {array} colorArr
|
|
667
|
+
* @example: CssColorParser.fromNormaliz([1, 0, 0, 1])
|
|
668
|
+
*/
|
|
669
|
+
CssColorParser.fromNormalize = function (colorArr) {
|
|
670
|
+
var r = colorArr[0] * 255;
|
|
671
|
+
var g = colorArr[1] * 255;
|
|
672
|
+
var b = colorArr[2] * 255;
|
|
673
|
+
var a = colorArr[3];
|
|
674
|
+
return CssColorParser.fromArray([r, g, b, a]);
|
|
675
|
+
};
|
|
467
676
|
return CssColorParser;
|
|
468
677
|
}());
|
|
469
678
|
/* harmony default export */ var src_CssColorParser = (CssColorParser);
|
|
@@ -494,12 +703,7 @@ function parseKeyWord(v) {
|
|
|
494
703
|
* @example: parseHEX('#FFF')
|
|
495
704
|
*/
|
|
496
705
|
function parseHEX(v) {
|
|
497
|
-
|
|
498
|
-
var res = CssColorStringParser.parse3BitsHEX(cssStr);
|
|
499
|
-
if (!res) {
|
|
500
|
-
res = CssColorStringParser.parse6BitsHEX(cssStr);
|
|
501
|
-
}
|
|
502
|
-
return res && fromArray(res);
|
|
706
|
+
return src_CssColorParser.parseHEX(v);
|
|
503
707
|
}
|
|
504
708
|
/**
|
|
505
709
|
* @description: 解析RGBA
|
|
@@ -508,13 +712,7 @@ function parseHEX(v) {
|
|
|
508
712
|
* @example: parseRGBA('rgba(255,255,255,1)')
|
|
509
713
|
*/
|
|
510
714
|
function parseRGBA(v) {
|
|
511
|
-
|
|
512
|
-
var res = CssColorStringParser.parseRGBA(cssStr);
|
|
513
|
-
if (!res) {
|
|
514
|
-
var cssStr2 = CssColorStringParser.trimStr(v);
|
|
515
|
-
res = CssColorStringParser.parseRGBA2(cssStr2);
|
|
516
|
-
}
|
|
517
|
-
return res && fromArray(res);
|
|
715
|
+
return src_CssColorParser.parseRGBA(v);
|
|
518
716
|
}
|
|
519
717
|
/**
|
|
520
718
|
* @description: 解析HSLA
|
|
@@ -549,7 +747,7 @@ function parseHWB(v) {
|
|
|
549
747
|
* @example: parseCssColorStr('rgba(255,255,255,1)')
|
|
550
748
|
*/
|
|
551
749
|
function parseCssColorStr(v) {
|
|
552
|
-
Check.type('
|
|
750
|
+
Check.type('color', v, 'string');
|
|
553
751
|
return parseHEX(v) || parseRGBA(v) || parseKeyWord(v) || parseHSLA(v) || parseHWB(v);
|
|
554
752
|
}
|
|
555
753
|
/**
|
|
@@ -561,8 +759,7 @@ function parseCssColorStr(v) {
|
|
|
561
759
|
* @example: fromColorStr('rgba(255,255,255,1)')
|
|
562
760
|
*/
|
|
563
761
|
function fromColorStr(v) {
|
|
564
|
-
|
|
565
|
-
return parseHEX(v) || parseRGBA(v) || parseKeyWord(v) || parseHSLA(v) || parseHWB(v);
|
|
762
|
+
return parseCssColorStr(v);
|
|
566
763
|
}
|
|
567
764
|
/**
|
|
568
765
|
* @description: 将HSL色彩模式转换为解析对象
|
|
@@ -605,24 +802,16 @@ function fromRandom(color1, color2) {
|
|
|
605
802
|
if (!color1 || !color2) {
|
|
606
803
|
throw new Error('fail to create object from random');
|
|
607
804
|
}
|
|
608
|
-
|
|
609
|
-
Math.min(color1.r, color2.r);
|
|
610
|
-
var g = Math.random() * Math.abs(color2.g - color1.g) +
|
|
611
|
-
Math.min(color1.g, color2.g);
|
|
612
|
-
var b = Math.random() * Math.abs(color2.b - color1.b) +
|
|
613
|
-
Math.min(color1.b, color2.b);
|
|
614
|
-
var a = Math.random() * Math.abs(color2.a - color1.a) +
|
|
615
|
-
Math.min(color1.a, color2.a);
|
|
616
|
-
return new src_CssColorParser(r, g, b, a);
|
|
805
|
+
return src_CssColorParser.fromRandom(color1, color2);
|
|
617
806
|
}
|
|
618
807
|
/**
|
|
619
808
|
* @description: 将ColorJson格式的json数据转换为解析对象
|
|
620
809
|
* @param {ColorJson} json
|
|
621
810
|
* @return {CssColorParser}
|
|
622
|
-
* @
|
|
811
|
+
* @example: fromJson({r: 255, g:255, b:255, a:1})
|
|
623
812
|
*/
|
|
624
813
|
function fromJson(json) {
|
|
625
|
-
return
|
|
814
|
+
return src_CssColorParser.fromJson(json);
|
|
626
815
|
}
|
|
627
816
|
/**
|
|
628
817
|
* @description: 将rgba数组转换为解析对象
|
|
@@ -631,7 +820,7 @@ function fromJson(json) {
|
|
|
631
820
|
* @author: roman_123
|
|
632
821
|
*/
|
|
633
822
|
function fromArray(color) {
|
|
634
|
-
return
|
|
823
|
+
return src_CssColorParser.fromArray(color);
|
|
635
824
|
}
|
|
636
825
|
|
|
637
826
|
;// CONCATENATED MODULE: ./src/main.ts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){"use strict";var
|
|
1
|
+
!function(){"use strict";var t={n:function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(n,{a:n}),n},d:function(r,n){for(var e in n)t.o(n,e)&&!t.o(r,e)&&Object.defineProperty(r,e,{enumerable:!0,get:n[e]})},o:function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},r:function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},r={};t.r(r),t.d(r,{CssColorParser:function(){return s},fromArray:function(){return _},fromColorStr:function(){return m},fromHSL:function(){return y},fromHWB:function(){return v},fromJson:function(){return S},fromRandom:function(){return d},parseCssColorStr:function(){return g},parseHEX:function(){return f},parseHSLA:function(){return l},parseHWB:function(){return b},parseKeyWord:function(){return p},parseRGBA:function(){return h}});var n=function(){function t(){}return t.type=function(t,r,n){var e=typeof r;if(e!==n)throw new Error("Expected ".concat(t," to be typeof ").concat(n,", actual typeof was ").concat(e))},t.types=function(t,r,n){var e=typeof r;if(!n.includes(e))throw new Error("Expected ".concat(t," to be typeof ").concat(n.join("|"),", actual typeof was ").concat(e))},t.numValue=function(r,n,e,o){t.numMinValue(r,n,e),t.numMaxValue(r,n,o)},t.numMinValue=function(t,r,n){if(r<n)throw new Error("Expected ".concat(t," to > ").concat(n,", actual value was ").concat(r))},t.numMaxValue=function(t,r,n){if(r>n)throw new Error("Expected ".concat(t," to < ").concat(n,", actual value was ").concat(r))},t.numIsInt=function(t,r,n){var e=Math.floor(r)===r;if(e!==n)throw new Error("Expected ".concat(t," to ").concat(n?"Integer":"Float",", actual value was ").concat(e?"Integer":"Float"))},t}();function e(t,r){return null==t||isNaN(t)&&"number"==typeof r?r:t}function o(t,r,n){return n>r?n=r:n<t&&(n=t),n}function i(t,r){void 0===t&&(t=0);var n=Math.pow(10,r);return Math.round(t*n)/n}var a=function(){function t(){}return t.clearStrSpace=function(t){return t.replace(/\s/g,"")},t.trimStr=function(t){return(t=t.replace(/\s+/g," ")).trim()},t.parse3BitsHEX=function(r){var n=/^#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i.exec(r);if(n){var o=e(n[4],"f");return[t._parseResStrForRgb(parseInt(n[1]+n[1],16)),t._parseResStrForRgb(parseInt(n[2]+n[2],16)),t._parseResStrForRgb(parseInt(n[3]+n[3],16)),t._parsePercent(parseInt(o+o,16)/255)]}return null},t.parse6BitsHEX=function(r){var n=/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i.exec(r);if(n){var o=e(n[4],"ff");return[t._parseResStrForRgb(parseInt(n[1],16)),t._parseResStrForRgb(parseInt(n[2],16)),t._parseResStrForRgb(parseInt(n[3],16)),t._parsePercent(parseInt(o,16)/255)]}return null},t.parseRGBA=function(r){var n=/^rgba?\(([0-9.]+%?),([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i.exec(r);return n?[t._parseResStrForRgb(n[1]),t._parseResStrForRgb(n[2]),t._parseResStrForRgb(n[3]),t._parsePercent(n[4])]:null},t.parseHSLA=function(r){var n=/^hsla?\(([0-9.]+)(?:deg)?,([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i.exec(r);return n?[t._parseResStrForHue(n[1]),t._parsePercent(n[2]),t._parsePercent(n[3]),t._parsePercent(n[4])]:null},t.parseHWB=function(r){var n=/^hwb\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i.exec(r);return n?[t._parseResStrForHue(n[1]),t._parsePercent(n[2]),t._parsePercent(n[3]),t._parsePercent(n[4])]:null},t.parseRGBA2=function(r){var n=/^rgba?\s?\(\s?([0-9.]+%?)\s?([0-9.]+%?)\s?([0-9.]+%?)(?:\s?\/\s?([0-9.]+%?))?\s?\)$/i.exec(r);return n?[t._parseResStrForRgb(n[1]),t._parseResStrForRgb(n[2]),t._parseResStrForRgb(n[3]),t._parsePercent(n[4])]:null},t.parseHSLA2=function(r){var n=/^hsla?\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i.exec(r);return n?[t._parseResStrForHue(n[1]),t._parsePercent(n[2]),t._parsePercent(n[3]),t._parsePercent(n[4])]:null},t._parseResStrForRgb=function(t){return"string"==typeof t&&(t=parseFloat(t)/("%"===t.substr(-1)?100/255:1)),isNaN(t)&&(t=1),o(0,255,t)},t._parseResStrForHue=function(t){return"string"==typeof t&&(t=parseFloat(t)),isNaN(t)&&(t=0),o(0,360,t)},t._parsePercent=function(t){return"string"==typeof t&&(t=parseFloat(t)/("%"===t.substr(-1)?100:1)),isNaN(t)&&(t=1),o(0,1,t)},t}(),s=function(){function t(t,r,n,e){this.r=255,this.g=255,this.b=255,this.a=1,this._outColorPrecision=2,this._outAlphaPrecision=2,this.setColor(t,r,n,e)}return t.prototype.setOutPrecision=function(t,r){return n.type("colorPrecision",t,"number"),n.type("outAlphaPrecision",r,"number"),n.numMinValue("colorPrecision",t,0),n.numMinValue("outAlphaPrecision",r,0),n.numIsInt("colorPrecision",t,!0),n.numIsInt("outAlphaPrecision",r,!0),this._outColorPrecision=t,this._outAlphaPrecision=r,this},t.prototype.setColor=function(t,r,n,i){return this.r=o(0,255,e(Number(t),0)),this.g=o(0,255,e(Number(r),0)),this.b=o(0,255,e(Number(n),0)),this.a=o(0,1,e(Number(i),1)),this},t.prototype.setAlpha=function(t){return this.a=o(0,1,e(Number(t),1)),this},t.prototype.setRed=function(t){return this.r=o(0,255,e(Number(t),0)),this},t.prototype.setGreen=function(t){return this.g=o(0,255,e(Number(t),0)),this},t.prototype.setBlue=function(t){return this.b=o(0,255,e(Number(t),0)),this},t.prototype.toRGBA=function(){var t=this.toJson();return 1===t.a?"rgb(".concat(t.r,",").concat(t.g,",").concat(t.b,")"):"rgba(".concat(t.r,",").concat(t.g,",").concat(t.b,",").concat(t.a,")")},t.prototype.toString=function(){return this.toRGBA()},t.prototype.toNormalize=function(){return[i(this.r/255,this._outColorPrecision),i(this.g/255,this._outColorPrecision),i(this.b/255,this._outColorPrecision),i(this.a,this._outAlphaPrecision)]},t.prototype.toHEX=function(){var t=this.toJson(),r=t.r.toString(16);r.length<2&&(r="0".concat(r));var n=t.g.toString(16);n.length<2&&(n="0".concat(n));var e=t.b.toString(16);if(e.length<2&&(e="0".concat(e)),this.a<1){var o=parseInt((255*this.a).toFixed()).toString(16);return o.length<2&&(o="0".concat(o)),"#".concat(r).concat(n).concat(e).concat(o)}return"#".concat(r).concat(n).concat(e)},t.prototype.toArray=function(){var t=this.toJson();return[t.r,t.g,t.b,t.a]},t.prototype.toJson=function(){return{r:i(this.r,this._outColorPrecision),g:i(this.g,this._outColorPrecision),b:i(this.b,this._outColorPrecision),a:i(this.a,this._outAlphaPrecision)}},t.prototype.toInvert=function(){return new t(255-this.r,255-this.g,255-this.b,1-this.a)},t.prototype.clone=function(){return new t(this.r,this.g,this.b,this.a)},t.prototype.equals=function(t){if(this===t)return!0;var r=this.toJson(),n=t.toJson();return r.r===n.r&&r.g===n.g&&r.b===n.g&&r.a===n.a},t.prototype.setInvert=function(){return this.r=255-this.r,this.g=255-this.g,this.b=255-this.b,this.a=1-this.a,this},t.prototype.multiplyByScalar=function(t,r){void 0===r&&(r=!0);var n=this.r*t,e=this.g*t,o=this.b*t,i=r?this.a*t:this.a;return this.setColor(n,e,o,i)},t.prototype.divideByScalar=function(t,r){void 0===r&&(r=!0);var n=this.r/t,e=this.g/t,o=this.b/t,i=r?this.a/t:this.a;return this.setColor(n,e,o,i)},t.prototype.add=function(t,r){void 0===r&&(r=!0);var n=this.r+t.r,e=this.g+t.g,o=this.b+t.b,i=r?this.a+t.a:this.a;return this.setColor(n,e,o,i)},t.prototype.subtract=function(t,r){void 0===r&&(r=!0);var n=this.r-t.r,e=this.g-t.g,o=this.b-t.b,i=r?this.a-t.a:this.a;return this.setColor(n,e,o,i)},t.prototype.multiply=function(t,r){void 0===r&&(r=!0);var n=this.r*t.r,e=this.g*t.g,o=this.b*t.b,i=r?this.a*t.a:this.a;return this.setColor(n,e,o,i)},t.prototype.divide=function(t,r){void 0===r&&(r=!0);var n=this.r/t.r,e=this.g/t.g,o=this.b/t.b,i=r?this.a/t.a:this.a;return this.setColor(n,e,o,i)},t.prototype.addNumberForRGB=function(t){return this.r=this.r+t,this.g=this.g+t,this.b=this.b+t,this},t.prototype.addNumberForAlpha=function(t){return this.a=this.a+t,this},t.parseHEX=function(r){var n=a.clearStrSpace(r),e=a.parse3BitsHEX(n);return e||(e=a.parse6BitsHEX(n)),e&&t.fromArray(e)},t.parseRGBA=function(r){var n=a.clearStrSpace(r),e=a.parseRGBA(n);if(!e){var o=a.trimStr(r);e=a.parseRGBA2(o)}return e&&t.fromArray(e)},t.fromJson=function(r){return new t(r.r,r.g,r.b,r.a)},t.fromArray=function(r){return new t(r[0],r[1],r[2],r[3])},t.fromRandom=function(r,n){return new t(Math.random()*Math.abs(n.r-r.r)+Math.min(r.r,n.r),Math.random()*Math.abs(n.g-r.g)+Math.min(r.g,n.g),Math.random()*Math.abs(n.b-r.b)+Math.min(r.b,n.b),Math.random()*Math.abs(n.a-r.a)+Math.min(r.a,n.a))},t.fromNormalize=function(r){var n=255*r[0],e=255*r[1],o=255*r[2],i=r[3];return t.fromArray([n,e,o,i])},t}(),u=require("color-convert"),c=t.n(u);function p(t){var r=a.clearStrSpace(t),n=c().keyword.rgb(r);return n&&_(n)}function f(t){return s.parseHEX(t)}function h(t){return s.parseRGBA(t)}function l(t){var r=a.clearStrSpace(t),n=a.parseHSLA(r);if(!n){var e=a.trimStr(t);n=a.parseHSLA2(e)}return n&&y(n[0],n[1],n[2],n[3])}function b(t){var r=a.trimStr(t),n=a.parseHWB(r);return n&&v(n[0],n[1],n[2],n[3])}function g(t){return n.type("color",t,"string"),f(t)||h(t)||p(t)||l(t)||b(t)}function m(t){return g(t)}function y(t,r,n,i){var a=c().hsl.rgb(o(0,360,t),o(0,100,100*r),o(0,100,100*n));return new s(a[0],a[1],a[2],e(Number(i),1))}function v(t,r,n,i){var a=c().hwb.rgb(o(0,360,t),o(0,100,100*r),o(0,100,100*n));return new s(a[0],a[1],a[2],e(Number(i),1))}function d(t,r){if("string"==typeof t&&(t=g(t)),"string"==typeof r&&(r=g(r)),!t||!r)throw new Error("fail to create object from random");return s.fromRandom(t,r)}function S(t){return s.fromJson(t)}function _(t){return s.fromArray(t)}var R=exports;for(var P in r)R[P]=r[P];r.__esModule&&Object.defineProperty(R,"__esModule",{value:!0})}();
|