@whitesev/utils 2.9.12 → 2.10.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/README.md +176 -176
- package/dist/index.amd.js +206 -117
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +206 -117
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +206 -117
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +206 -117
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +206 -117
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +206 -117
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/Dictionary.d.ts +2 -0
- package/dist/types/src/Utils.d.ts +9 -2
- package/dist/types/src/types/Httpx.d.ts +1344 -1344
- package/dist/types/src/types/Log.d.ts +19 -19
- package/dist/types/src/types/Progress.d.ts +20 -20
- package/dist/types/src/types/React.d.ts +119 -119
- package/dist/types/src/types/TryCatch.d.ts +9 -9
- package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
- package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
- package/dist/types/src/types/Vue2.d.ts +166 -166
- package/dist/types/src/types/WindowApi.d.ts +14 -14
- package/dist/types/src/types/ajaxHooker.d.ts +155 -155
- package/dist/types/src/types/env.d.ts +7 -7
- package/dist/types/src/types/global.d.ts +31 -31
- package/package.json +26 -24
- package/src/ColorConversion.ts +118 -118
- package/src/CommonUtil.ts +301 -301
- package/src/DOMUtils.ts +251 -251
- package/src/Dictionary.ts +205 -199
- package/src/GBKEncoder.ts +108 -108
- package/src/Hooks.ts +73 -73
- package/src/Httpx.ts +1457 -1457
- package/src/LockFunction.ts +65 -62
- package/src/Log.ts +233 -233
- package/src/ModuleRaid.js +378 -360
- package/src/Progress.ts +108 -108
- package/src/TryCatch.ts +86 -86
- package/src/Utils.ts +3907 -3852
- package/src/UtilsCommon.ts +14 -14
- package/src/UtilsGMCookie.ts +273 -273
- package/src/UtilsGMMenu.ts +460 -460
- package/src/Vue.ts +233 -233
- package/src/WindowApi.ts +59 -59
- package/src/ajaxHooker/ajaxHooker.js +606 -538
- package/src/ajaxHooker/ajaxHooker1.2.4.js +440 -438
- package/src/indexedDB.ts +497 -497
- package/src/types/Httpx.d.ts +1344 -1344
- package/src/types/Log.d.ts +19 -19
- package/src/types/Progress.d.ts +20 -20
- package/src/types/React.d.ts +119 -119
- package/src/types/TryCatch.d.ts +9 -9
- package/src/types/UtilsGMCookie.d.ts +93 -93
- package/src/types/UtilsGMMenu.d.ts +77 -77
- package/src/types/Vue2.d.ts +166 -166
- package/src/types/WindowApi.d.ts +14 -14
- package/src/types/ajaxHooker.d.ts +155 -155
- package/src/types/env.d.ts +7 -7
- package/src/types/global.d.ts +31 -31
package/src/ColorConversion.ts
CHANGED
|
@@ -1,118 +1,118 @@
|
|
|
1
|
-
export class ColorConversion {
|
|
2
|
-
/**
|
|
3
|
-
* 判断是否是16进制颜色
|
|
4
|
-
* @param str 十六进制颜色,如`#000000`
|
|
5
|
-
*/
|
|
6
|
-
isHex(str: string): boolean {
|
|
7
|
-
if (typeof str !== "string") {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
if (!str.match(/^(#|)[0-9a-fA-F]{6}$/)) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
return true;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* 16进制颜色转rgba
|
|
17
|
-
*
|
|
18
|
-
* 例如:`#ff0000` 转为 `rgba(123,123,123, 0.4)`
|
|
19
|
-
* @param hex 十六进制颜色,如`#000000`
|
|
20
|
-
* @param opacity 透明度,0~1
|
|
21
|
-
*/
|
|
22
|
-
hexToRgba(hex: string, opacity: number): string {
|
|
23
|
-
if (!this.isHex(hex)) {
|
|
24
|
-
throw new TypeError(`输入错误的hex:${hex}`);
|
|
25
|
-
}
|
|
26
|
-
return hex && hex.replace(/\s+/g, "").length === 7
|
|
27
|
-
? `rgba(${parseInt(`0x${hex.slice(1, 3)}`)},${parseInt(`0x${hex.slice(3, 5)}`)},${parseInt(
|
|
28
|
-
`0x${hex.slice(5, 7)}`
|
|
29
|
-
)},${opacity})`
|
|
30
|
-
: "";
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* hex转rgb
|
|
34
|
-
* @param hex 十六进制颜色,如`#000000`
|
|
35
|
-
*/
|
|
36
|
-
hexToRgb(hex: string) {
|
|
37
|
-
if (!this.isHex(hex)) {
|
|
38
|
-
throw new TypeError(`输入错误的hex:${hex}`);
|
|
39
|
-
}
|
|
40
|
-
/* replace替换查找的到的字符串 */
|
|
41
|
-
hex = hex.replace("#", "");
|
|
42
|
-
/* match得到查询数组 */
|
|
43
|
-
const hxs = hex.match(/../g)!;
|
|
44
|
-
for (let index = 0; index < 3; index++) {
|
|
45
|
-
const value = parseInt(hxs[index], 16);
|
|
46
|
-
Reflect.set(hxs, index, value);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return hxs;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* rgb转hex
|
|
53
|
-
* @param redValue 红色值
|
|
54
|
-
* @param greenValue 绿色值
|
|
55
|
-
* @param blueValue 蓝色值
|
|
56
|
-
* @returns hex
|
|
57
|
-
*/
|
|
58
|
-
rgbToHex(redValue: string | number, greenValue: string | number, blueValue: string | number): string {
|
|
59
|
-
/* 验证输入的rgb值是否合法 */
|
|
60
|
-
const validPattern = /^\d{1,3}$/;
|
|
61
|
-
if (
|
|
62
|
-
!validPattern.test(redValue.toString()) ||
|
|
63
|
-
!validPattern.test(greenValue.toString()) ||
|
|
64
|
-
!validPattern.test(blueValue.toString())
|
|
65
|
-
)
|
|
66
|
-
throw new TypeError("输入错误的rgb颜色值");
|
|
67
|
-
const hexs = [redValue.toString(16), greenValue.toString(16), blueValue.toString(16)];
|
|
68
|
-
for (let index = 0; index < 3; index++) if (hexs[index].length == 1) hexs[index] = `0${hexs[index]}`;
|
|
69
|
-
return `#${hexs.join("")}`;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* 获取颜色变暗或亮
|
|
73
|
-
* @param color hex颜色,如`#000000`
|
|
74
|
-
* @param level 0~1.0 系数越大,颜色越变暗
|
|
75
|
-
*/
|
|
76
|
-
getDarkColor(color: string, level: number | string): string {
|
|
77
|
-
if (!this.isHex(color)) {
|
|
78
|
-
throw new TypeError(`输入错误的hex:${color}`);
|
|
79
|
-
}
|
|
80
|
-
if (typeof level !== "number") {
|
|
81
|
-
level = Number(level);
|
|
82
|
-
}
|
|
83
|
-
if (isNaN(level)) {
|
|
84
|
-
throw new TypeError(`输入错误的level:${level}`);
|
|
85
|
-
}
|
|
86
|
-
const rgbc = this.hexToRgb(color);
|
|
87
|
-
for (let index = 0; index < 3; index++) {
|
|
88
|
-
const rgbcItemValue = rgbc[index];
|
|
89
|
-
const value = Math.floor(Number(rgbcItemValue) * (1 - level));
|
|
90
|
-
Reflect.set(rgbc, index, value);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* 获取颜色变亮
|
|
97
|
-
* @param color hex颜色,如`#000000`
|
|
98
|
-
* @param level 0~1.0 系数越大,颜色越变亮
|
|
99
|
-
*/
|
|
100
|
-
getLightColor(color: string, level: number | string): string {
|
|
101
|
-
if (!this.isHex(color)) {
|
|
102
|
-
throw new TypeError(`输入错误的hex:${color}`);
|
|
103
|
-
}
|
|
104
|
-
if (typeof level !== "number") {
|
|
105
|
-
level = Number(level);
|
|
106
|
-
}
|
|
107
|
-
if (isNaN(level)) {
|
|
108
|
-
throw new TypeError(`输入错误的level:${level}`);
|
|
109
|
-
}
|
|
110
|
-
const rgbc = this.hexToRgb(color);
|
|
111
|
-
for (let index = 0; index < 3; index++) {
|
|
112
|
-
const rgbcItemValue = Number(rgbc[index]);
|
|
113
|
-
const value = Math.floor(255 - rgbcItemValue * level + rgbcItemValue);
|
|
114
|
-
Reflect.set(rgbc, index, value);
|
|
115
|
-
}
|
|
116
|
-
return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
1
|
+
export class ColorConversion {
|
|
2
|
+
/**
|
|
3
|
+
* 判断是否是16进制颜色
|
|
4
|
+
* @param str 十六进制颜色,如`#000000`
|
|
5
|
+
*/
|
|
6
|
+
isHex(str: string): boolean {
|
|
7
|
+
if (typeof str !== "string") {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
if (!str.match(/^(#|)[0-9a-fA-F]{6}$/)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 16进制颜色转rgba
|
|
17
|
+
*
|
|
18
|
+
* 例如:`#ff0000` 转为 `rgba(123,123,123, 0.4)`
|
|
19
|
+
* @param hex 十六进制颜色,如`#000000`
|
|
20
|
+
* @param opacity 透明度,0~1
|
|
21
|
+
*/
|
|
22
|
+
hexToRgba(hex: string, opacity: number): string {
|
|
23
|
+
if (!this.isHex(hex)) {
|
|
24
|
+
throw new TypeError(`输入错误的hex:${hex}`);
|
|
25
|
+
}
|
|
26
|
+
return hex && hex.replace(/\s+/g, "").length === 7
|
|
27
|
+
? `rgba(${parseInt(`0x${hex.slice(1, 3)}`)},${parseInt(`0x${hex.slice(3, 5)}`)},${parseInt(
|
|
28
|
+
`0x${hex.slice(5, 7)}`
|
|
29
|
+
)},${opacity})`
|
|
30
|
+
: "";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* hex转rgb
|
|
34
|
+
* @param hex 十六进制颜色,如`#000000`
|
|
35
|
+
*/
|
|
36
|
+
hexToRgb(hex: string) {
|
|
37
|
+
if (!this.isHex(hex)) {
|
|
38
|
+
throw new TypeError(`输入错误的hex:${hex}`);
|
|
39
|
+
}
|
|
40
|
+
/* replace替换查找的到的字符串 */
|
|
41
|
+
hex = hex.replace("#", "");
|
|
42
|
+
/* match得到查询数组 */
|
|
43
|
+
const hxs = hex.match(/../g)!;
|
|
44
|
+
for (let index = 0; index < 3; index++) {
|
|
45
|
+
const value = parseInt(hxs[index], 16);
|
|
46
|
+
Reflect.set(hxs, index, value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return hxs;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* rgb转hex
|
|
53
|
+
* @param redValue 红色值
|
|
54
|
+
* @param greenValue 绿色值
|
|
55
|
+
* @param blueValue 蓝色值
|
|
56
|
+
* @returns hex
|
|
57
|
+
*/
|
|
58
|
+
rgbToHex(redValue: string | number, greenValue: string | number, blueValue: string | number): string {
|
|
59
|
+
/* 验证输入的rgb值是否合法 */
|
|
60
|
+
const validPattern = /^\d{1,3}$/;
|
|
61
|
+
if (
|
|
62
|
+
!validPattern.test(redValue.toString()) ||
|
|
63
|
+
!validPattern.test(greenValue.toString()) ||
|
|
64
|
+
!validPattern.test(blueValue.toString())
|
|
65
|
+
)
|
|
66
|
+
throw new TypeError("输入错误的rgb颜色值");
|
|
67
|
+
const hexs = [redValue.toString(16), greenValue.toString(16), blueValue.toString(16)];
|
|
68
|
+
for (let index = 0; index < 3; index++) if (hexs[index].length == 1) hexs[index] = `0${hexs[index]}`;
|
|
69
|
+
return `#${hexs.join("")}`;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 获取颜色变暗或亮
|
|
73
|
+
* @param color hex颜色,如`#000000`
|
|
74
|
+
* @param level 0~1.0 系数越大,颜色越变暗
|
|
75
|
+
*/
|
|
76
|
+
getDarkColor(color: string, level: number | string): string {
|
|
77
|
+
if (!this.isHex(color)) {
|
|
78
|
+
throw new TypeError(`输入错误的hex:${color}`);
|
|
79
|
+
}
|
|
80
|
+
if (typeof level !== "number") {
|
|
81
|
+
level = Number(level);
|
|
82
|
+
}
|
|
83
|
+
if (isNaN(level)) {
|
|
84
|
+
throw new TypeError(`输入错误的level:${level}`);
|
|
85
|
+
}
|
|
86
|
+
const rgbc = this.hexToRgb(color);
|
|
87
|
+
for (let index = 0; index < 3; index++) {
|
|
88
|
+
const rgbcItemValue = rgbc[index];
|
|
89
|
+
const value = Math.floor(Number(rgbcItemValue) * (1 - level));
|
|
90
|
+
Reflect.set(rgbc, index, value);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 获取颜色变亮
|
|
97
|
+
* @param color hex颜色,如`#000000`
|
|
98
|
+
* @param level 0~1.0 系数越大,颜色越变亮
|
|
99
|
+
*/
|
|
100
|
+
getLightColor(color: string, level: number | string): string {
|
|
101
|
+
if (!this.isHex(color)) {
|
|
102
|
+
throw new TypeError(`输入错误的hex:${color}`);
|
|
103
|
+
}
|
|
104
|
+
if (typeof level !== "number") {
|
|
105
|
+
level = Number(level);
|
|
106
|
+
}
|
|
107
|
+
if (isNaN(level)) {
|
|
108
|
+
throw new TypeError(`输入错误的level:${level}`);
|
|
109
|
+
}
|
|
110
|
+
const rgbc = this.hexToRgb(color);
|
|
111
|
+
for (let index = 0; index < 3; index++) {
|
|
112
|
+
const rgbcItemValue = Number(rgbc[index]);
|
|
113
|
+
const value = Math.floor(255 - rgbcItemValue * level + rgbcItemValue);
|
|
114
|
+
Reflect.set(rgbc, index, value);
|
|
115
|
+
}
|
|
116
|
+
return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
|
|
117
|
+
}
|
|
118
|
+
}
|