@whitesev/utils 1.1.8 → 1.1.9

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.
@@ -17,7 +17,7 @@ declare class ColorConversion {
17
17
  * @param str
18
18
  * @returns
19
19
  */
20
- hexToRgb(str: string): RegExpMatchArray;
20
+ hexToRgb(str: string): RegExpMatchArray | null;
21
21
  /**
22
22
  * rgb转hex
23
23
  * @param redValue
@@ -32,7 +32,7 @@ declare class ColorConversion {
32
32
  * @param level 0~1.0
33
33
  * @returns
34
34
  */
35
- getDarkColor(color: string, level: number): string;
35
+ getDarkColor(color: string, level: string): string;
36
36
  /**
37
37
  * 获取颜色变亮
38
38
  * @param color 颜色
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/node/index.esm.js",
@@ -3,7 +3,7 @@ class ColorConversion {
3
3
  * 判断是否是16进制颜色
4
4
  * @param str
5
5
  */
6
- isHex(str: string) {
6
+ isHex(str: string): boolean {
7
7
  if (typeof str !== "string") {
8
8
  return false;
9
9
  }
@@ -19,9 +19,10 @@ class ColorConversion {
19
19
  * @param hex
20
20
  * @param opacity
21
21
  */
22
- hexToRgba(hex: string, opacity: number) {
22
+ hexToRgba(hex: string, opacity: number): string {
23
23
  if (!this.isHex(hex)) {
24
- throw new TypeError("输入错误的hex" + hex);
24
+ // @ts-ignore
25
+ throw new TypeError("输入错误的hex", hex);
25
26
  }
26
27
  return hex && hex.replace(/\s+/g, "").length === 7
27
28
  ? "rgba(" +
@@ -42,17 +43,18 @@ class ColorConversion {
42
43
  */
43
44
  hexToRgb(str: string) {
44
45
  if (!this.isHex(str)) {
45
- throw new TypeError("输入错误的hex" + str);
46
+ // @ts-ignore
47
+ throw new TypeError("输入错误的hex", str);
46
48
  }
47
49
  /* replace替换查找的到的字符串 */
48
50
  str = str.replace("#", "");
49
51
  /* match得到查询数组 */
50
52
  let hxs = str.match(/../g);
51
- if (hxs == null) {
52
- throw new TypeError("输入错误的hex" + str);
53
+ for (let index = 0; index < 3; index++) {
54
+ // @ts-ignore
55
+ hxs[index] = parseInt(hxs[index], 16);
53
56
  }
54
- for (let index = 0; index < 3; index++)
55
- hxs[index] = parseInt(hxs[index], 16).toString();
57
+
56
58
  return hxs;
57
59
  }
58
60
  /**
@@ -66,7 +68,7 @@ class ColorConversion {
66
68
  redValue: string | number,
67
69
  greenValue: string | number,
68
70
  blueValue: string | number
69
- ) {
71
+ ): string {
70
72
  /* 验证输入的rgb值是否合法 */
71
73
  let validPattern = /^\d{1,3}$/;
72
74
  if (
@@ -90,14 +92,18 @@ class ColorConversion {
90
92
  * @param level 0~1.0
91
93
  * @returns
92
94
  */
93
- getDarkColor(color: string, level: number) {
95
+ getDarkColor(color: string, level: string): string {
94
96
  if (!this.isHex(color)) {
95
- throw new TypeError("输入错误的hex" + color);
97
+ // @ts-ignore
98
+ throw new TypeError("输入错误的hex", color);
96
99
  }
97
100
  let rgbc = this.hexToRgb(color);
98
- for (let index = 0; index < 3; index++)
101
+ for (let index = 0; index < 3; index++) {
99
102
  // @ts-ignore
100
- rgbc[index] = Math.floor(rgbc[index] * (1 - level)).toString();
103
+ rgbc[index] = Math.floor(rgbc[index] * (1 - level));
104
+ }
105
+
106
+ // @ts-ignore
101
107
  return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
102
108
  }
103
109
  /**
@@ -106,16 +112,17 @@ class ColorConversion {
106
112
  * @param level 0~1.0
107
113
  * @returns
108
114
  */
109
- getLightColor(color: string, level: number) {
115
+ getLightColor(color: string, level: number): string {
110
116
  if (!this.isHex(color)) {
111
- throw new TypeError("输入错误的hex" + color);
117
+ // @ts-ignore
118
+ throw new TypeError("输入错误的hex", color);
112
119
  }
113
120
  let rgbc = this.hexToRgb(color);
114
- for (let index = 0; index < 3; index++)
115
- rgbc[index] = Math.floor(
116
- // @ts-ignore
117
- (255 - rgbc[index]) * level + rgbc[index]
118
- ).toString();
121
+ for (let index = 0; index < 3; index++) {
122
+ // @ts-ignore
123
+ rgbc[index] = Math.floor((255 - rgbc[index]) * level + rgbc[index]);
124
+ }
125
+ // @ts-ignore
119
126
  return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
120
127
  }
121
128
  }