css-color-parser-h 2.0.1 → 2.0.3

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.
@@ -2,7 +2,7 @@
2
2
  * @Author: roman_123
3
3
  * @Description:
4
4
  * @Date: 2023-05-26 13:29:45
5
- * @LastEditTime: 2023-05-26 13:58:07
5
+ * @LastEditTime: 2023-05-26 16:10:51
6
6
  */
7
7
  export class ColorJson {
8
8
  r: number
@@ -12,35 +12,35 @@ export class ColorJson {
12
12
  }
13
13
  declare module 'css-color-parser-h' {
14
14
  /**
15
- * @description: 将css KeyWord字符串转换为解析对象
15
+ * @description: 解析颜色关键字
16
16
  * @param {string} v
17
17
  * @return {CssColorParser}
18
18
  * @example: parseKeyWord('red')
19
19
  */
20
20
  export function parseKeyWord(v: string): CssColorParser
21
21
  /**
22
- * @description: 将css HEX字符串转换为解析对象
22
+ * @description: 解析16进制字符串
23
23
  * @param {string} v
24
24
  * @return {CssColorParser}
25
- * @example: parseHEX('#ccc')
25
+ * @example: parseHEX('#FFF')
26
26
  */
27
27
  export function parseHEX(v: string): CssColorParser
28
28
  /**
29
- * @description: 将css RGBA字符串转换为解析对象
29
+ * @description: 解析RGBA
30
30
  * @param {string} v
31
31
  * @return {CssColorParser}
32
- * @example: parseRGBA('rgb(255,255,255)')
32
+ * @example: parseRGBA('rgba(255,255,255,1)')
33
33
  */
34
34
  export function parseRGBA(v: string): CssColorParser
35
35
  /**
36
- * @description: 将css HSLA字符串转换为解析对象
36
+ * @description: 解析HSLA
37
37
  * @param {string} v
38
38
  * @return {CssColorParser}
39
- * @example: parseHSLA('hsl(215 85% 62% / 0.8)')
39
+ * @example: parseHSLA('hsla(215,85%,62%,0.8)')
40
40
  */
41
41
  export function parseHSLA(v: string): CssColorParser
42
42
  /**
43
- * @description: 将css HSLA字符串转换为解析对象
43
+ * @description: 解析HWB
44
44
  * @param {string} v
45
45
  * @return {CssColorParser}
46
46
  * @example: parseHWB('hwb(215deg 30% 6% / 80%)')
@@ -93,9 +93,9 @@ declare module 'css-color-parser-h' {
93
93
  a?: number
94
94
  ): CssColorParser
95
95
  /**
96
- * @description: 从解析器中产生随机颜色
96
+ * @description: 产生随机颜色
97
97
  * @return {CssColorParser}
98
- * @author: roman_123
98
+ * @example: fromRandom('#000', ''#fff)
99
99
  */
100
100
  export function fromRandom(
101
101
  color1: string | CssColorParser,
@@ -120,14 +120,6 @@ declare module 'css-color-parser-h' {
120
120
  g: number
121
121
  b: number
122
122
  a: number
123
- /**
124
- * 设置颜色
125
- * @param red
126
- * @param green
127
- * @param blue
128
- * @param alpha
129
- * @example: const parser = new CssColorParser(255,255,255,1)
130
- */
131
123
  constructor(
132
124
  red?: number | string,
133
125
  green?: number | string,
@@ -197,5 +189,42 @@ declare module 'css-color-parser-h' {
197
189
  * @author: roman_123
198
190
  */
199
191
  equals(color: any): boolean
192
+ /**
193
+ * @description: 解析16进制颜色
194
+ * @param {string} v
195
+ * @return {CssColorParser}
196
+ * @example: parseHEX('#FFF')
197
+ */
198
+ static parseHEX(v: string): CssColorParser
199
+ /**
200
+ * @description: 解析rgba、rgb颜色
201
+ * @param {string} v
202
+ * @return {CssColorParser}
203
+ * @example: parseRGBA('rgba(255,255,255,1)')
204
+ */
205
+ static parseRGBA(v: string): CssColorParser
206
+ /**
207
+ * @description: 将ColorJson格式的json数据转换为解析对象
208
+ * @param {ColorJson} json
209
+ * @return {CssColorParser}
210
+ * @example: fromJson({r: 255, g: 255, b: 255, a: 1})
211
+ */
212
+ static fromJson(json: ColorJson): CssColorParser
213
+ /**
214
+ * @description: 将RGBA数组转换为解析对象
215
+ * @param {Array} color
216
+ * @return {CssColorParser}
217
+ * @example: fromJson([255,255,255,1])
218
+ */
219
+ static fromArray(color: Array<number>): CssColorParser
220
+ /**
221
+ * @description: 产生随机颜色
222
+ * @return {CssColorParser}
223
+ * @example: fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
224
+ */
225
+ static fromRandom(
226
+ color1: CssColorParser,
227
+ color2: CssColorParser
228
+ ): CssColorParser
200
229
  }
201
230
  }
@@ -51,29 +51,21 @@ __webpack_require__.r(__webpack_exports__);
51
51
 
52
52
  // EXPORTS
53
53
  __webpack_require__.d(__webpack_exports__, {
54
- "default": function() { return /* binding */ main; }
54
+ "CssColorParser": function() { return /* reexport */ src_CssColorParser; },
55
+ "fromArray": function() { return /* reexport */ fromArray; },
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
67
  });
56
68
 
57
- // NAMESPACE OBJECT: ./src/utils/parsers.ts
58
- var parsers_namespaceObject = {};
59
- __webpack_require__.r(parsers_namespaceObject);
60
- __webpack_require__.d(parsers_namespaceObject, {
61
- "fromArray": function() { return fromArray; },
62
- "fromColorStr": function() { return fromColorStr; },
63
- "fromHSL": function() { return fromHSL; },
64
- "fromHWB": function() { return fromHWB; },
65
- "fromJson": function() { return fromJson; },
66
- "fromRandom": function() { return fromRandom; },
67
- "parseCssColorStr": function() { return parseCssColorStr; },
68
- "parseHEX": function() { return parseHEX; },
69
- "parseHSLA": function() { return parseHSLA; },
70
- "parseHWB": function() { return parseHWB; },
71
- "parseKeyWord": function() { return parseKeyWord; },
72
- "parseRGBA": function() { return parseRGBA; }
73
- });
74
-
75
- ;// CONCATENATED MODULE: external "tslib"
76
- var external_tslib_namespaceObject = require("tslib");
77
69
  ;// CONCATENATED MODULE: ./src/utils/common.ts
78
70
  var Check = /** @class */ (function () {
79
71
  function Check() {
@@ -122,6 +114,151 @@ function setNumberPrecision(number, precision) {
122
114
  return Number(number.toFixed(precision));
123
115
  }
124
116
 
117
+ ;// CONCATENATED MODULE: ./src/utils/color-tools.ts
118
+
119
+ /*
120
+ * @Author: roman_123
121
+ * @Description: 部分核心代码修改自cesium
122
+ * @Date: 2022-11-28 17:32:57
123
+ * @LastEditTime: 2022-12-06 00:02:21
124
+ */
125
+ var CssColorStringParser = /** @class */ (function () {
126
+ function CssColorStringParser() {
127
+ }
128
+ // 去除字符串内所有空格
129
+ CssColorStringParser.clearStrSpace = function (v) {
130
+ return v.replace(/\s/g, '');
131
+ };
132
+ // 去除字符串两边空格,将中间多个空格合并为一个
133
+ CssColorStringParser.trimStr = function (v) {
134
+ v = v.replace(/\s+/g, ' ');
135
+ return v.trim();
136
+ };
137
+ // 解析3位16进制 #fff #fff0
138
+ CssColorStringParser.parse3BitsHEX = function (v) {
139
+ var regx = /^#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i;
140
+ var res = regx.exec(v);
141
+ if (res) {
142
+ var res4 = defaultValue(res[4], 'f');
143
+ var r = CssColorStringParser._parseResStrForRgb(parseInt(res[1] + res[1], 16));
144
+ var g = CssColorStringParser._parseResStrForRgb(parseInt(res[2] + res[2], 16));
145
+ var b = CssColorStringParser._parseResStrForRgb(parseInt(res[3] + res[3], 16));
146
+ var a = CssColorStringParser._parsePercent(parseInt(res4 + res4, 16) / 255);
147
+ return [r, g, b, a];
148
+ }
149
+ return null;
150
+ };
151
+ // 解析6位16进制 #ffffff #ffffff00
152
+ CssColorStringParser.parse6BitsHEX = function (v) {
153
+ var regx = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i;
154
+ var res = regx.exec(v);
155
+ if (res) {
156
+ var res4 = defaultValue(res[4], 'ff');
157
+ var r = CssColorStringParser._parseResStrForRgb(parseInt(res[1], 16));
158
+ var g = CssColorStringParser._parseResStrForRgb(parseInt(res[2], 16));
159
+ var b = CssColorStringParser._parseResStrForRgb(parseInt(res[3], 16));
160
+ var a = CssColorStringParser._parsePercent(parseInt(res4, 16) / 255.0);
161
+ return [r, g, b, a];
162
+ }
163
+ return null;
164
+ };
165
+ // 解析rgb rgba rgb(255,255,255) rgba(255,255,255,1) rgba(100%,100%,100%, 1)
166
+ CssColorStringParser.parseRGBA = function (v) {
167
+ var regx = /^rgba?\(([0-9.]+%?),([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i;
168
+ var res = regx.exec(v);
169
+ if (res) {
170
+ var r = CssColorStringParser._parseResStrForRgb(res[1]);
171
+ var g = CssColorStringParser._parseResStrForRgb(res[2]);
172
+ var b = CssColorStringParser._parseResStrForRgb(res[3]);
173
+ var a = CssColorStringParser._parsePercent(res[4]);
174
+ return [r, g, b, a];
175
+ }
176
+ return null;
177
+ };
178
+ // 解析hsl hsla hsl(360,100%,100%) hsla(360,100%,100%,1)
179
+ CssColorStringParser.parseHSLA = function (v) {
180
+ var regx = /^hsla?\(([0-9.]+)(?:deg)?,([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i;
181
+ var res = regx.exec(v);
182
+ if (res) {
183
+ var h = CssColorStringParser._parseResStrForHue(res[1]);
184
+ var s = CssColorStringParser._parsePercent(res[2]);
185
+ var l = CssColorStringParser._parsePercent(res[3]);
186
+ var a = CssColorStringParser._parsePercent(res[4]);
187
+ return [h, s, l, a];
188
+ }
189
+ return null;
190
+ };
191
+ // 解析hwb
192
+ CssColorStringParser.parseHWB = function (v) {
193
+ var regx = /^hwb\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i;
194
+ var res = regx.exec(v);
195
+ if (res) {
196
+ var h = CssColorStringParser._parseResStrForHue(res[1]);
197
+ var w = CssColorStringParser._parsePercent(res[2]);
198
+ var b = CssColorStringParser._parsePercent(res[3]);
199
+ var a = CssColorStringParser._parsePercent(res[4]);
200
+ return [h, w, b, a];
201
+ }
202
+ return null;
203
+ };
204
+ // 解析rgb rgb(178 57 57 / 44%) 字符串中存在空格,因此使用清除两侧空格的原始字符串
205
+ CssColorStringParser.parseRGBA2 = function (v) {
206
+ var regx = /^rgba?\s?\(\s?([0-9.]+%?)\s?([0-9.]+%?)\s?([0-9.]+%?)(?:\s?\/\s?([0-9.]+%?))?\s?\)$/i;
207
+ var res = regx.exec(v);
208
+ if (res) {
209
+ var r = CssColorStringParser._parseResStrForRgb(res[1]);
210
+ var g = CssColorStringParser._parseResStrForRgb(res[2]);
211
+ var b = CssColorStringParser._parseResStrForRgb(res[3]);
212
+ var a = CssColorStringParser._parsePercent(res[4]);
213
+ return [r, g, b, a];
214
+ }
215
+ return null;
216
+ };
217
+ // 解析hsl hsl(215 85% 62% / 1)
218
+ CssColorStringParser.parseHSLA2 = function (v) {
219
+ var regx = /^hsla?\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i;
220
+ var res = regx.exec(v);
221
+ if (res) {
222
+ var h = CssColorStringParser._parseResStrForHue(res[1]);
223
+ var s = CssColorStringParser._parsePercent(res[2]);
224
+ var l = CssColorStringParser._parsePercent(res[3]);
225
+ var a = CssColorStringParser._parsePercent(res[4]);
226
+ return [h, s, l, a];
227
+ }
228
+ return null;
229
+ };
230
+ // 将结果解析为数字
231
+ CssColorStringParser._parseResStrForRgb = function (v) {
232
+ if (typeof v === 'string') {
233
+ v = parseFloat(v) / ('%' === v.substr(-1) ? 100.0 / 255 : 1);
234
+ }
235
+ if (isNaN(v)) {
236
+ v = 1;
237
+ }
238
+ return limitNumber(0, 255, v);
239
+ };
240
+ CssColorStringParser._parseResStrForHue = function (v) {
241
+ if (typeof v === 'string') {
242
+ v = parseFloat(v);
243
+ }
244
+ if (isNaN(v)) {
245
+ v = 0;
246
+ }
247
+ return limitNumber(0, 360, v);
248
+ };
249
+ CssColorStringParser._parsePercent = function (v) {
250
+ if (typeof v === 'string') {
251
+ v = parseFloat(v) / ('%' === v.substr(-1) ? 100.0 : 1);
252
+ }
253
+ if (isNaN(v)) {
254
+ v = 1;
255
+ }
256
+ return limitNumber(0, 1, v);
257
+ };
258
+ return CssColorStringParser;
259
+ }());
260
+
261
+
125
262
  ;// CONCATENATED MODULE: ./src/CssColorParser.ts
126
263
  /*
127
264
  * @Descripttion: 颜色解析器
@@ -129,9 +266,10 @@ function setNumberPrecision(number, precision) {
129
266
  * @Author: roman_123
130
267
  * @Date: 2021-01-19 09:22:11
131
268
  * @LastEditors: Please set LastEditors
132
- * @LastEditTime: 2023-05-26 11:20:17
269
+ * @LastEditTime: 2023-05-26 15:59:40
133
270
  */
134
271
 
272
+
135
273
  var CssColorParser = /** @class */ (function () {
136
274
  function CssColorParser(red, green, blue, alpha) {
137
275
  this.r = 255;
@@ -263,154 +401,72 @@ var CssColorParser = /** @class */ (function () {
263
401
  this.a === color.a);
264
402
  }
265
403
  };
266
- return CssColorParser;
267
- }());
268
- /* harmony default export */ var src_CssColorParser = (CssColorParser);
269
-
270
- ;// CONCATENATED MODULE: ./src/utils/color-tools.ts
271
-
272
- /*
273
- * @Author: roman_123
274
- * @Description: 部分核心代码修改自cesium
275
- * @Date: 2022-11-28 17:32:57
276
- * @LastEditTime: 2022-12-06 00:02:21
277
- */
278
- var CssColorStringParser = /** @class */ (function () {
279
- function CssColorStringParser() {
280
- }
281
- // 去除字符串内所有空格
282
- CssColorStringParser.clearStrSpace = function (v) {
283
- return v.replace(/\s/g, '');
284
- };
285
- // 去除字符串两边空格,将中间多个空格合并为一个
286
- CssColorStringParser.trimStr = function (v) {
287
- v = v.replace(/\s+/g, ' ');
288
- return v.trim();
289
- };
290
- // 解析3位16进制 #fff #fff0
291
- CssColorStringParser.parse3BitsHEX = function (v) {
292
- var regx = /^#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i;
293
- var res = regx.exec(v);
294
- if (res) {
295
- var res4 = defaultValue(res[4], 'f');
296
- var r = CssColorStringParser._parseResStrForRgb(parseInt(res[1] + res[1], 16));
297
- var g = CssColorStringParser._parseResStrForRgb(parseInt(res[2] + res[2], 16));
298
- var b = CssColorStringParser._parseResStrForRgb(parseInt(res[3] + res[3], 16));
299
- var a = CssColorStringParser._parsePercent(parseInt(res4 + res4, 16) / 255);
300
- return [r, g, b, a];
301
- }
302
- return null;
303
- };
304
- // 解析6位16进制 #ffffff #ffffff00
305
- CssColorStringParser.parse6BitsHEX = function (v) {
306
- var regx = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i;
307
- var res = regx.exec(v);
308
- if (res) {
309
- var res4 = defaultValue(res[4], 'ff');
310
- var r = CssColorStringParser._parseResStrForRgb(parseInt(res[1], 16));
311
- var g = CssColorStringParser._parseResStrForRgb(parseInt(res[2], 16));
312
- var b = CssColorStringParser._parseResStrForRgb(parseInt(res[3], 16));
313
- var a = CssColorStringParser._parsePercent(parseInt(res4, 16) / 255.0);
314
- return [r, g, b, a];
315
- }
316
- return null;
317
- };
318
- // 解析rgb rgba rgb(255,255,255) rgba(255,255,255,1) rgba(100%,100%,100%, 1)
319
- CssColorStringParser.parseRGBA = function (v) {
320
- var regx = /^rgba?\(([0-9.]+%?),([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i;
321
- var res = regx.exec(v);
322
- if (res) {
323
- var r = CssColorStringParser._parseResStrForRgb(res[1]);
324
- var g = CssColorStringParser._parseResStrForRgb(res[2]);
325
- var b = CssColorStringParser._parseResStrForRgb(res[3]);
326
- var a = CssColorStringParser._parsePercent(res[4]);
327
- return [r, g, b, a];
328
- }
329
- return null;
330
- };
331
- // 解析hsl hsla hsl(360,100%,100%) hsla(360,100%,100%,1)
332
- CssColorStringParser.parseHSLA = function (v) {
333
- var regx = /^hsla?\(([0-9.]+)(?:deg)?,([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i;
334
- var res = regx.exec(v);
335
- if (res) {
336
- var h = CssColorStringParser._parseResStrForHue(res[1]);
337
- var s = CssColorStringParser._parsePercent(res[2]);
338
- var l = CssColorStringParser._parsePercent(res[3]);
339
- var a = CssColorStringParser._parsePercent(res[4]);
340
- return [h, s, l, a];
341
- }
342
- return null;
343
- };
344
- // 解析hwb
345
- CssColorStringParser.parseHWB = function (v) {
346
- var regx = /^hwb\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i;
347
- var res = regx.exec(v);
348
- if (res) {
349
- var h = CssColorStringParser._parseResStrForHue(res[1]);
350
- var w = CssColorStringParser._parsePercent(res[2]);
351
- var b = CssColorStringParser._parsePercent(res[3]);
352
- var a = CssColorStringParser._parsePercent(res[4]);
353
- return [h, w, b, a];
354
- }
355
- return null;
356
- };
357
- // 解析rgb rgb(178 57 57 / 44%) 字符串中存在空格,因此使用清除两侧空格的原始字符串
358
- CssColorStringParser.parseRGBA2 = function (v) {
359
- var regx = /^rgba?\s?\(\s?([0-9.]+%?)\s?([0-9.]+%?)\s?([0-9.]+%?)(?:\s?\/\s?([0-9.]+%?))?\s?\)$/i;
360
- var res = regx.exec(v);
361
- if (res) {
362
- var r = CssColorStringParser._parseResStrForRgb(res[1]);
363
- var g = CssColorStringParser._parseResStrForRgb(res[2]);
364
- var b = CssColorStringParser._parseResStrForRgb(res[3]);
365
- var a = CssColorStringParser._parsePercent(res[4]);
366
- return [r, g, b, a];
404
+ /**
405
+ * @description: 解析16进制颜色
406
+ * @param {string} v
407
+ * @return {CssColorParser}
408
+ * @example: parseHEX('#FFF')
409
+ */
410
+ CssColorParser.parseHEX = function (v) {
411
+ var cssStr = CssColorStringParser.clearStrSpace(v);
412
+ var res = CssColorStringParser.parse3BitsHEX(cssStr);
413
+ if (!res) {
414
+ res = CssColorStringParser.parse6BitsHEX(cssStr);
367
415
  }
368
- return null;
416
+ return res && CssColorParser.fromArray(res);
369
417
  };
370
- // 解析hsl hsl(215 85% 62% / 1)
371
- CssColorStringParser.parseHSLA2 = function (v) {
372
- var regx = /^hsla?\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i;
373
- var res = regx.exec(v);
374
- if (res) {
375
- var h = CssColorStringParser._parseResStrForHue(res[1]);
376
- var s = CssColorStringParser._parsePercent(res[2]);
377
- var l = CssColorStringParser._parsePercent(res[3]);
378
- var a = CssColorStringParser._parsePercent(res[4]);
379
- return [h, s, l, a];
418
+ /**
419
+ * @description: 解析rgba、rgb颜色
420
+ * @param {string} v
421
+ * @return {CssColorParser}
422
+ * @example: parseRGBA('rgba(255,255,255,1)')
423
+ */
424
+ CssColorParser.parseRGBA = function (v) {
425
+ var cssStr = CssColorStringParser.clearStrSpace(v);
426
+ var res = CssColorStringParser.parseRGBA(cssStr);
427
+ if (!res) {
428
+ var cssStr2 = CssColorStringParser.trimStr(v);
429
+ res = CssColorStringParser.parseRGBA2(cssStr2);
380
430
  }
381
- return null;
431
+ return res && CssColorParser.fromArray(res);
382
432
  };
383
- // 将结果解析为数字
384
- CssColorStringParser._parseResStrForRgb = function (v) {
385
- if (typeof v === 'string') {
386
- v = parseFloat(v) / ('%' === v.substr(-1) ? 100.0 / 255 : 1);
387
- }
388
- if (isNaN(v)) {
389
- v = 1;
390
- }
391
- return limitNumber(0, 255, v);
433
+ /**
434
+ * @description: 将ColorJson格式的json数据转换为解析对象
435
+ * @param {ColorJson} json
436
+ * @return {CssColorParser}
437
+ * @example: fromJson({r: 255, g: 255, b: 255, a: 1})
438
+ */
439
+ CssColorParser.fromJson = function (json) {
440
+ return new CssColorParser(json.r, json.g, json.b, json.a);
392
441
  };
393
- CssColorStringParser._parseResStrForHue = function (v) {
394
- if (typeof v === 'string') {
395
- v = parseFloat(v);
396
- }
397
- if (isNaN(v)) {
398
- v = 0;
399
- }
400
- return limitNumber(0, 360, v);
442
+ /**
443
+ * @description: 将RGBA数组转换为解析对象
444
+ * @param {Array} color
445
+ * @return {CssColorParser}
446
+ * @example: fromJson([255,255,255,1])
447
+ */
448
+ CssColorParser.fromArray = function (color) {
449
+ return new CssColorParser(color[0], color[1], color[2], color[3]);
401
450
  };
402
- CssColorStringParser._parsePercent = function (v) {
403
- if (typeof v === 'string') {
404
- v = parseFloat(v) / ('%' === v.substr(-1) ? 100.0 : 1);
405
- }
406
- if (isNaN(v)) {
407
- v = 1;
408
- }
409
- return limitNumber(0, 1, v);
451
+ /**
452
+ * @description: 产生随机颜色
453
+ * @return {CssColorParser}
454
+ * @example: fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
455
+ */
456
+ CssColorParser.fromRandom = function (color1, color2) {
457
+ var r = Math.random() * Math.abs(color2.r - color1.r) +
458
+ Math.min(color1.r, color2.r);
459
+ var g = Math.random() * Math.abs(color2.g - color1.g) +
460
+ Math.min(color1.g, color2.g);
461
+ var b = Math.random() * Math.abs(color2.b - color1.b) +
462
+ Math.min(color1.b, color2.b);
463
+ var a = Math.random() * Math.abs(color2.a - color1.a) +
464
+ Math.min(color1.a, color2.a);
465
+ return new CssColorParser(r, g, b, a);
410
466
  };
411
- return CssColorStringParser;
467
+ return CssColorParser;
412
468
  }());
413
-
469
+ /* harmony default export */ var src_CssColorParser = (CssColorParser);
414
470
 
415
471
  ;// CONCATENATED MODULE: external "color-convert"
416
472
  var external_color_convert_namespaceObject = require("color-convert");
@@ -420,11 +476,23 @@ var external_color_convert_default = /*#__PURE__*/__webpack_require__.n(external
420
476
 
421
477
 
422
478
 
479
+ /**
480
+ * @description: 解析颜色关键字
481
+ * @param {string} v
482
+ * @return {CssColorParser}
483
+ * @example: parseKeyWord('red')
484
+ */
423
485
  function parseKeyWord(v) {
424
486
  var cssStr = CssColorStringParser.clearStrSpace(v);
425
487
  var res = external_color_convert_default().keyword.rgb(cssStr);
426
488
  return res && fromArray(res);
427
489
  }
490
+ /**
491
+ * @description: 解析16进制字符串
492
+ * @param {string} v
493
+ * @return {CssColorParser}
494
+ * @example: parseHEX('#FFF')
495
+ */
428
496
  function parseHEX(v) {
429
497
  var cssStr = CssColorStringParser.clearStrSpace(v);
430
498
  var res = CssColorStringParser.parse3BitsHEX(cssStr);
@@ -433,6 +501,12 @@ function parseHEX(v) {
433
501
  }
434
502
  return res && fromArray(res);
435
503
  }
504
+ /**
505
+ * @description: 解析RGBA
506
+ * @param {string} v
507
+ * @return {CssColorParser}
508
+ * @example: parseRGBA('rgba(255,255,255,1)')
509
+ */
436
510
  function parseRGBA(v) {
437
511
  var cssStr = CssColorStringParser.clearStrSpace(v);
438
512
  var res = CssColorStringParser.parseRGBA(cssStr);
@@ -442,6 +516,12 @@ function parseRGBA(v) {
442
516
  }
443
517
  return res && fromArray(res);
444
518
  }
519
+ /**
520
+ * @description: 解析HSLA
521
+ * @param {string} v
522
+ * @return {CssColorParser}
523
+ * @example: parseHSLA('hsla(215,85%,62%,0.8)')
524
+ */
445
525
  function parseHSLA(v) {
446
526
  var cssStr = CssColorStringParser.clearStrSpace(v);
447
527
  var res = CssColorStringParser.parseHSLA(cssStr);
@@ -451,6 +531,12 @@ function parseHSLA(v) {
451
531
  }
452
532
  return res && fromHSL(res[0], res[1], res[2], res[3]);
453
533
  }
534
+ /**
535
+ * @description: 解析HWB
536
+ * @param {string} v
537
+ * @return {CssColorParser}
538
+ * @example: parseHWB('hwb(215deg 30% 6% / 80%)')
539
+ */
454
540
  function parseHWB(v) {
455
541
  var cssStr2 = CssColorStringParser.trimStr(v);
456
542
  var res = CssColorStringParser.parseHWB(cssStr2);
@@ -505,10 +591,10 @@ function fromHWB(h, w, b, a) {
505
591
  return new src_CssColorParser(res[0], res[1], res[2], defaultValue(Number(a), 1));
506
592
  }
507
593
  /**
508
- * @description: 从解析器中产生随机颜色
509
- * @return {CssColorParser}
510
- * @author: roman_123
511
- */
594
+ * @description: 产生随机颜色
595
+ * @return {CssColorParser}
596
+ * @example: fromRandom('#000', ''#fff)
597
+ */
512
598
  function fromRandom(color1, color2) {
513
599
  if (typeof color1 === 'string') {
514
600
  color1 = parseCssColorStr(color1);
@@ -549,16 +635,15 @@ function fromArray(color) {
549
635
  }
550
636
 
551
637
  ;// CONCATENATED MODULE: ./src/main.ts
552
-
553
638
  /*
554
639
  * @Author: roman_123
555
640
  * @Description:
556
641
  * @Date: 2023-05-25 17:45:22
557
- * @LastEditTime: 2023-05-26 15:01:18
642
+ * @LastEditTime: 2023-05-26 15:54:02
558
643
  */
559
644
 
560
645
 
561
- /* harmony default export */ var main = ((0,external_tslib_namespaceObject.__assign)({ CssColorParser: src_CssColorParser }, parsers_namespaceObject));
646
+
562
647
 
563
648
  var __webpack_export_target__ = exports;
564
649
  for(var i in __webpack_exports__) __webpack_export_target__[i] = __webpack_exports__[i];
@@ -0,0 +1 @@
1
+ !function(){"use strict";var r={n:function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,{a:e}),e},d:function(t,e){for(var n in e)r.o(e,n)&&!r.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},o:function(r,t){return Object.prototype.hasOwnProperty.call(r,t)},r:function(r){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})}},t={};r.r(t),r.d(t,{CssColorParser:function(){return i},fromArray:function(){return d},fromColorStr:function(){return S},fromHSL:function(){return m},fromHWB:function(){return y},fromJson:function(){return v},fromRandom:function(){return R},parseCssColorStr:function(){return l},parseHEX:function(){return f},parseHSLA:function(){return g},parseHWB:function(){return h},parseKeyWord:function(){return p},parseRGBA:function(){return b}});var e=function(){function r(){}return r.type=function(r,t,e){var n=typeof e;if(n!==r)throw new Error("Expected ".concat(t," to be typeof ").concat(r,", actual typeof was ").concat(n))},r.types=function(r,t,e){var n=typeof e;if(!r.includes(n))throw new Error("Expected ".concat(t," to be typeof ").concat(r.join("|"),", actual typeof was ").concat(n))},r}();function n(r,t){return null==r||isNaN(r)&&"number"==typeof t?t:r}function a(r,t,e){return e>t?e=t:e<r&&(e=r),e}function o(r,t){return Number(r.toFixed(t))}var s=function(){function r(){}return r.clearStrSpace=function(r){return r.replace(/\s/g,"")},r.trimStr=function(r){return(r=r.replace(/\s+/g," ")).trim()},r.parse3BitsHEX=function(t){var e=/^#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i.exec(t);if(e){var a=n(e[4],"f");return[r._parseResStrForRgb(parseInt(e[1]+e[1],16)),r._parseResStrForRgb(parseInt(e[2]+e[2],16)),r._parseResStrForRgb(parseInt(e[3]+e[3],16)),r._parsePercent(parseInt(a+a,16)/255)]}return null},r.parse6BitsHEX=function(t){var e=/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i.exec(t);if(e){var a=n(e[4],"ff");return[r._parseResStrForRgb(parseInt(e[1],16)),r._parseResStrForRgb(parseInt(e[2],16)),r._parseResStrForRgb(parseInt(e[3],16)),r._parsePercent(parseInt(a,16)/255)]}return null},r.parseRGBA=function(t){var e=/^rgba?\(([0-9.]+%?),([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i.exec(t);return e?[r._parseResStrForRgb(e[1]),r._parseResStrForRgb(e[2]),r._parseResStrForRgb(e[3]),r._parsePercent(e[4])]:null},r.parseHSLA=function(t){var e=/^hsla?\(([0-9.]+)(?:deg)?,([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i.exec(t);return e?[r._parseResStrForHue(e[1]),r._parsePercent(e[2]),r._parsePercent(e[3]),r._parsePercent(e[4])]:null},r.parseHWB=function(t){var e=/^hwb\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i.exec(t);return e?[r._parseResStrForHue(e[1]),r._parsePercent(e[2]),r._parsePercent(e[3]),r._parsePercent(e[4])]:null},r.parseRGBA2=function(t){var e=/^rgba?\s?\(\s?([0-9.]+%?)\s?([0-9.]+%?)\s?([0-9.]+%?)(?:\s?\/\s?([0-9.]+%?))?\s?\)$/i.exec(t);return e?[r._parseResStrForRgb(e[1]),r._parseResStrForRgb(e[2]),r._parseResStrForRgb(e[3]),r._parsePercent(e[4])]:null},r.parseHSLA2=function(t){var e=/^hsla?\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i.exec(t);return e?[r._parseResStrForHue(e[1]),r._parsePercent(e[2]),r._parsePercent(e[3]),r._parsePercent(e[4])]:null},r._parseResStrForRgb=function(r){return"string"==typeof r&&(r=parseFloat(r)/("%"===r.substr(-1)?100/255:1)),isNaN(r)&&(r=1),a(0,255,r)},r._parseResStrForHue=function(r){return"string"==typeof r&&(r=parseFloat(r)),isNaN(r)&&(r=0),a(0,360,r)},r._parsePercent=function(r){return"string"==typeof r&&(r=parseFloat(r)/("%"===r.substr(-1)?100:1)),isNaN(r)&&(r=1),a(0,1,r)},r}(),i=function(){function r(r,t,e,n){this.r=255,this.g=255,this.b=255,this.a=1,this.setColor(r,t,e,n)}return r.prototype.setColor=function(r,t,e,o){this.r=a(0,255,n(Number(r),0)),this.g=a(0,255,n(Number(t),0)),this.b=a(0,255,n(Number(e),0)),this.a=a(0,1,n(Number(o),1))},r.prototype.toRGBA=function(){var r=this.toJson();return 1===r.a?"rgb(".concat(r.r,",").concat(r.g,",").concat(r.b,")"):"rgba(".concat(r.r,",").concat(r.g,",").concat(r.b,",").concat(r.a,")")},r.prototype.toString=function(){return this.toRGBA()},r.prototype.toNormalize=function(){return{r:o(this.r/255,2),g:o(this.g/255,2),b:o(this.b/255,2),a:o(this.a,2)}},r.prototype.toHEX=function(){var r=this.toJson(),t=r.r.toString(16);t.length<2&&(t="0".concat(t));var e=r.g.toString(16);e.length<2&&(e="0".concat(e));var n=r.b.toString(16);if(n.length<2&&(n="0".concat(n)),this.a<1){var a=parseInt((255*this.a).toFixed()).toString(16);return a.length<2&&(a="0".concat(a)),"#".concat(t).concat(e).concat(n).concat(a)}return"#".concat(t).concat(e).concat(n)},r.prototype.toArray=function(){var r=this.toJson();return[r.r,r.g,r.b,r.a]},r.prototype.toJson=function(){return{r:parseInt(this.r.toFixed()),g:parseInt(this.g.toFixed()),b:parseInt(this.b.toFixed()),a:parseFloat(this.a.toFixed(2))}},r.prototype.clone=function(){return new r(this.r,this.g,this.b,this.a)},r.prototype.equals=function(r){return this===r||this.r===r.r&&this.g===r.g&&this.b===r.g&&this.a===r.a},r.parseHEX=function(t){var e=s.clearStrSpace(t),n=s.parse3BitsHEX(e);return n||(n=s.parse6BitsHEX(e)),n&&r.fromArray(n)},r.parseRGBA=function(t){var e=s.clearStrSpace(t),n=s.parseRGBA(e);if(!n){var a=s.trimStr(t);n=s.parseRGBA2(a)}return n&&r.fromArray(n)},r.fromJson=function(t){return new r(t.r,t.g,t.b,t.a)},r.fromArray=function(t){return new r(t[0],t[1],t[2],t[3])},r.fromRandom=function(t,e){return new r(Math.random()*Math.abs(e.r-t.r)+Math.min(t.r,e.r),Math.random()*Math.abs(e.g-t.g)+Math.min(t.g,e.g),Math.random()*Math.abs(e.b-t.b)+Math.min(t.b,e.b),Math.random()*Math.abs(e.a-t.a)+Math.min(t.a,e.a))},r}(),c=require("color-convert"),u=r.n(c);function p(r){var t=s.clearStrSpace(r),e=u().keyword.rgb(t);return e&&d(e)}function f(r){var t=s.clearStrSpace(r),e=s.parse3BitsHEX(t);return e||(e=s.parse6BitsHEX(t)),e&&d(e)}function b(r){var t=s.clearStrSpace(r),e=s.parseRGBA(t);if(!e){var n=s.trimStr(r);e=s.parseRGBA2(n)}return e&&d(e)}function g(r){var t=s.clearStrSpace(r),e=s.parseHSLA(t);if(!e){var n=s.trimStr(r);e=s.parseHSLA2(n)}return e&&m(e[0],e[1],e[2],e[3])}function h(r){var t=s.trimStr(r),e=s.parseHWB(t);return e&&y(e[0],e[1],e[2],e[3])}function l(r){return e.type("string","color",r),f(r)||b(r)||p(r)||g(r)||h(r)}function S(r){return e.type("string","color",r),f(r)||b(r)||p(r)||g(r)||h(r)}function m(r,t,e,o){var s=u().hsl.rgb(a(0,360,r),a(0,100,100*t),a(0,100,100*e));return new i(s[0],s[1],s[2],n(Number(o),1))}function y(r,t,e,o){var s=u().hwb.rgb(a(0,360,r),a(0,100,100*t),a(0,100,100*e));return new i(s[0],s[1],s[2],n(Number(o),1))}function R(r,t){if("string"==typeof r&&(r=l(r)),"string"==typeof t&&(t=l(t)),!r||!t)throw new Error("fail to create object from random");var e=Math.random()*Math.abs(t.r-r.r)+Math.min(r.r,t.r),n=Math.random()*Math.abs(t.g-r.g)+Math.min(r.g,t.g),a=Math.random()*Math.abs(t.b-r.b)+Math.min(r.b,t.b),o=Math.random()*Math.abs(t.a-r.a)+Math.min(r.a,t.a);return new i(e,n,a,o)}function v(r){return new i(r.r,r.g,r.b,r.a)}function d(r){return new i(r[0],r[1],r[2],r[3])}var _=exports;for(var M in t)_[M]=t[M];t.__esModule&&Object.defineProperty(_,"__esModule",{value:!0})}();