@whitesev/utils 2.8.2 → 2.9.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.
Files changed (73) hide show
  1. package/README.md +176 -176
  2. package/dist/index.amd.js +269 -777
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.amd.min.js +2 -0
  5. package/dist/index.amd.min.js.map +1 -0
  6. package/dist/index.cjs.js +269 -777
  7. package/dist/index.cjs.js.map +1 -1
  8. package/dist/index.cjs.min.js +2 -0
  9. package/dist/index.cjs.min.js.map +1 -0
  10. package/dist/index.esm.js +269 -777
  11. package/dist/index.esm.js.map +1 -1
  12. package/dist/index.esm.min.js +2 -0
  13. package/dist/index.esm.min.js.map +1 -0
  14. package/dist/index.iife.js +269 -777
  15. package/dist/index.iife.js.map +1 -1
  16. package/dist/index.iife.min.js +2 -0
  17. package/dist/index.iife.min.js.map +1 -0
  18. package/dist/index.system.js +269 -777
  19. package/dist/index.system.js.map +1 -1
  20. package/dist/index.system.min.js +2 -0
  21. package/dist/index.system.min.js.map +1 -0
  22. package/dist/index.umd.js +269 -777
  23. package/dist/index.umd.js.map +1 -1
  24. package/dist/index.umd.min.js +2 -0
  25. package/dist/index.umd.min.js.map +1 -0
  26. package/dist/types/src/Utils.d.ts +103 -450
  27. package/dist/types/src/UtilsGMCookie.d.ts +4 -0
  28. package/dist/types/src/UtilsGMMenu.d.ts +3 -6
  29. package/dist/types/src/types/Httpx.d.ts +1344 -1344
  30. package/dist/types/src/types/Log.d.ts +19 -19
  31. package/dist/types/src/types/Progress.d.ts +20 -20
  32. package/dist/types/src/types/React.d.ts +119 -119
  33. package/dist/types/src/types/TryCatch.d.ts +9 -9
  34. package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
  35. package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
  36. package/dist/types/src/types/Vue2.d.ts +166 -166
  37. package/dist/types/src/types/WindowApi.d.ts +14 -14
  38. package/dist/types/src/types/ajaxHooker.d.ts +151 -151
  39. package/dist/types/src/types/env.d.ts +7 -7
  40. package/dist/types/src/types/global.d.ts +31 -31
  41. package/package.json +13 -9
  42. package/src/ColorConversion.ts +105 -105
  43. package/src/CommonUtil.ts +280 -280
  44. package/src/DOMUtils.ts +251 -251
  45. package/src/Dictionary.ts +153 -153
  46. package/src/GBKEncoder.ts +108 -108
  47. package/src/Hooks.ts +73 -73
  48. package/src/Httpx.ts +1457 -1457
  49. package/src/LockFunction.ts +62 -62
  50. package/src/Log.ts +258 -258
  51. package/src/Progress.ts +108 -108
  52. package/src/TryCatch.ts +86 -86
  53. package/src/Utils.ts +3778 -4773
  54. package/src/UtilsCommon.ts +14 -14
  55. package/src/UtilsGMCookie.ts +272 -254
  56. package/src/UtilsGMMenu.ts +441 -445
  57. package/src/Vue.ts +233 -233
  58. package/src/WindowApi.ts +59 -59
  59. package/src/indexedDB.ts +497 -497
  60. package/src/types/Httpx.d.ts +1344 -1344
  61. package/src/types/Log.d.ts +19 -19
  62. package/src/types/Progress.d.ts +20 -20
  63. package/src/types/React.d.ts +119 -119
  64. package/src/types/TryCatch.d.ts +9 -9
  65. package/src/types/UtilsGMCookie.d.ts +93 -93
  66. package/src/types/UtilsGMMenu.d.ts +77 -77
  67. package/src/types/Vue2.d.ts +166 -166
  68. package/src/types/WindowApi.d.ts +14 -14
  69. package/src/types/ajaxHooker.d.ts +151 -151
  70. package/src/types/env.d.ts +7 -7
  71. package/src/types/global.d.ts +31 -31
  72. package/dist/types/src/types/Event.d.ts +0 -188
  73. package/src/types/Event.d.ts +0 -188
@@ -1,151 +1,151 @@
1
- import type { HttpxHeaders, HttpxMethod, HttpxStatus } from "./Httpx";
2
-
3
- /** 请求的response配置 */
4
- export declare interface UtilsAjaxHookResponseOptions {
5
- /**
6
- * (重定向后的)Url
7
- */
8
- finalUrl: string;
9
- /**
10
- * 响应码
11
- */
12
- status: HttpxStatus;
13
- /**
14
- * 响应头
15
- */
16
- responseHeaders: HttpxHeaders;
17
- /**
18
- * 响应内容
19
- */
20
- response?: string | Blob | ArrayBuffer | XMLDocument | FormData;
21
- /**
22
- * 响应内容文本
23
- */
24
- responseText?: string;
25
- }
26
-
27
- /** hook请求的配置 */
28
- export declare interface UtilsAjaxHookRequestOptions {
29
- /**
30
- * 只读属性。一个字符串,表明请求类型是xhr还是fetch
31
- */
32
- type: "xhr";
33
- /**
34
- * 请求的Url
35
- */
36
- url: string;
37
- /**
38
- * 请求的url和method,可以直接修改
39
- */
40
- method: HttpxMethod;
41
- /**
42
- * 是否取消请求,设置为true即可取消本次请求
43
- */
44
- abort: boolean;
45
- /**
46
- * 请求头,可以直接修改
47
- */
48
- headers: HttpxHeaders;
49
- /**
50
- * 请求携带的数据,可以直接修改
51
- */
52
- data?: any;
53
- /**
54
- * 响应内容,必须通过一个回调函数进行读取和修改。
55
- *
56
- * 响应内容为一个对象,包含finalUrl、status、responseHeaders和被读取的响应数据,除响应数据可修改,其他属性是只读的。
57
- *
58
- * 响应数据是哪个属性取决于哪个属性被读取,xhr可能的属性为response、responseText、responseXML,fetch可能的属性为arrayBuffer、blob、formData、json、text。
59
- *
60
- * 在控制台输出时,xhr响应将包含所有属性,但只有被读取过的属性具有明确的值。修改对应属性即可影响读取结果,进而实现响应数据的修改。
61
- */
62
- response?: (res: Required<UtilsAjaxHookResponseOptions>) => void;
63
- /**
64
- * 只读属性。异步请求为true,同步请求为false,异步特性无法作用于同步请求
65
- */
66
- async: boolean;
67
- }
68
-
69
- /** 过滤规则配置 */
70
- export declare interface UtilsAjaxHookFilterOptions {
71
- /**
72
- * 应是xhr或fetch
73
- */
74
- type?: "xhr" | "fetch";
75
- /**
76
- * 字符串或正则表达式,无需完全匹配
77
- */
78
- url?: string;
79
- /**
80
- * 请求方法
81
- */
82
- method?: HttpxMethod;
83
- /**
84
- * 是否异步
85
- */
86
- async?: boolean;
87
- }
88
-
89
- /** Utils.ajaxHooker */
90
- export declare interface UtilsAjaxHookResult {
91
- /**
92
- * 劫持
93
- * @example
94
- ajaxHooker.hook(request => {
95
- if (request.url === 'https://www.example.com/') {
96
- request.response = res => {
97
- console.log(res);
98
- res.responseText += 'test';
99
- };
100
- }
101
- });
102
- * @example
103
- // 异步特性无法作用于同步请求,但同步修改仍然有效
104
- // 你可以将以上所有可修改属性赋值为Promise,原请求将被阻塞直至Promise完成(若发生reject,数据将不会被修改)
105
- // 此特性可用于异步劫持。以下是一个异步修改响应数据的例子
106
- ajaxHooker.hook(request => {
107
- request.response = res => {
108
- const responseText = res.responseText; // 注意保存原数据
109
- res.responseText = new Promise(resolve => {
110
- setTimeout(() => {
111
- resolve(responseText + 'test');
112
- }, 3000);
113
- });
114
- };
115
- });
116
-
117
- // 也可以传入async回调函数以实现异步
118
- ajaxHooker.hook(async request => {
119
- request.data = await modifyData(request.data);
120
- request.response = async res => {
121
- res.responseText = await modifyResponse(res.responseText);
122
- };
123
- });
124
- */
125
- hook(callback: (request: UtilsAjaxHookRequestOptions) => void | Promise<undefined>): void;
126
- /**
127
- * 过滤
128
- * @example
129
- // 应于hook方法之前执行,此方法若尽早执行,有助于提升性能。
130
- // 为hook方法设置过滤规则,只有符合规则的请求才会触发hook。过滤规则是一个对象数组,参考下例
131
- ajaxHooker.filter([
132
- {type: 'xhr', url: 'www.example.com', method: 'GET', async: true},
133
- {url: /^http/},
134
- ]);
135
- */
136
- filter(filterOptions: UtilsAjaxHookFilterOptions[]): void;
137
- /**
138
- * 阻止xhr和fetch被改写
139
- * @example
140
- // 如果库劫持失败,可能是其他代码对xhr/fetch进行了二次劫持,protect方法会尝试阻止xhr和fetch被改写。应于document-start阶段尽早执行,部分网页下可能引发错误,谨慎使用。
141
- ajaxHooker.protect();
142
- */
143
- protect(): void;
144
- /**
145
- * 取消劫持
146
- * @example
147
- // 将xhr和fetch恢复至劫持前的状态,调用此方法后,hook方法不再生效。
148
- ajaxHooker.unhook();
149
- */
150
- unhook(): void;
151
- }
1
+ import type { HttpxHeaders, HttpxMethod, HttpxStatus } from "./Httpx";
2
+
3
+ /** 请求的response配置 */
4
+ export declare interface UtilsAjaxHookResponseOptions {
5
+ /**
6
+ * (重定向后的)Url
7
+ */
8
+ finalUrl: string;
9
+ /**
10
+ * 响应码
11
+ */
12
+ status: HttpxStatus;
13
+ /**
14
+ * 响应头
15
+ */
16
+ responseHeaders: HttpxHeaders;
17
+ /**
18
+ * 响应内容
19
+ */
20
+ response?: string | Blob | ArrayBuffer | XMLDocument | FormData;
21
+ /**
22
+ * 响应内容文本
23
+ */
24
+ responseText?: string;
25
+ }
26
+
27
+ /** hook请求的配置 */
28
+ export declare interface UtilsAjaxHookRequestOptions {
29
+ /**
30
+ * 只读属性。一个字符串,表明请求类型是xhr还是fetch
31
+ */
32
+ type: "xhr";
33
+ /**
34
+ * 请求的Url
35
+ */
36
+ url: string;
37
+ /**
38
+ * 请求的url和method,可以直接修改
39
+ */
40
+ method: HttpxMethod;
41
+ /**
42
+ * 是否取消请求,设置为true即可取消本次请求
43
+ */
44
+ abort: boolean;
45
+ /**
46
+ * 请求头,可以直接修改
47
+ */
48
+ headers: HttpxHeaders;
49
+ /**
50
+ * 请求携带的数据,可以直接修改
51
+ */
52
+ data?: any;
53
+ /**
54
+ * 响应内容,必须通过一个回调函数进行读取和修改。
55
+ *
56
+ * 响应内容为一个对象,包含finalUrl、status、responseHeaders和被读取的响应数据,除响应数据可修改,其他属性是只读的。
57
+ *
58
+ * 响应数据是哪个属性取决于哪个属性被读取,xhr可能的属性为response、responseText、responseXML,fetch可能的属性为arrayBuffer、blob、formData、json、text。
59
+ *
60
+ * 在控制台输出时,xhr响应将包含所有属性,但只有被读取过的属性具有明确的值。修改对应属性即可影响读取结果,进而实现响应数据的修改。
61
+ */
62
+ response?: (res: Required<UtilsAjaxHookResponseOptions>) => void;
63
+ /**
64
+ * 只读属性。异步请求为true,同步请求为false,异步特性无法作用于同步请求
65
+ */
66
+ async: boolean;
67
+ }
68
+
69
+ /** 过滤规则配置 */
70
+ export declare interface UtilsAjaxHookFilterOptions {
71
+ /**
72
+ * 应是xhr或fetch
73
+ */
74
+ type?: "xhr" | "fetch";
75
+ /**
76
+ * 字符串或正则表达式,无需完全匹配
77
+ */
78
+ url?: string;
79
+ /**
80
+ * 请求方法
81
+ */
82
+ method?: HttpxMethod;
83
+ /**
84
+ * 是否异步
85
+ */
86
+ async?: boolean;
87
+ }
88
+
89
+ /** Utils.ajaxHooker */
90
+ export declare interface UtilsAjaxHookResult {
91
+ /**
92
+ * 劫持
93
+ * @example
94
+ ajaxHooker.hook(request => {
95
+ if (request.url === 'https://www.example.com/') {
96
+ request.response = res => {
97
+ console.log(res);
98
+ res.responseText += 'test';
99
+ };
100
+ }
101
+ });
102
+ * @example
103
+ // 异步特性无法作用于同步请求,但同步修改仍然有效
104
+ // 你可以将以上所有可修改属性赋值为Promise,原请求将被阻塞直至Promise完成(若发生reject,数据将不会被修改)
105
+ // 此特性可用于异步劫持。以下是一个异步修改响应数据的例子
106
+ ajaxHooker.hook(request => {
107
+ request.response = res => {
108
+ const responseText = res.responseText; // 注意保存原数据
109
+ res.responseText = new Promise(resolve => {
110
+ setTimeout(() => {
111
+ resolve(responseText + 'test');
112
+ }, 3000);
113
+ });
114
+ };
115
+ });
116
+
117
+ // 也可以传入async回调函数以实现异步
118
+ ajaxHooker.hook(async request => {
119
+ request.data = await modifyData(request.data);
120
+ request.response = async res => {
121
+ res.responseText = await modifyResponse(res.responseText);
122
+ };
123
+ });
124
+ */
125
+ hook(callback: (request: UtilsAjaxHookRequestOptions) => void | Promise<undefined>): void;
126
+ /**
127
+ * 过滤
128
+ * @example
129
+ // 应于hook方法之前执行,此方法若尽早执行,有助于提升性能。
130
+ // 为hook方法设置过滤规则,只有符合规则的请求才会触发hook。过滤规则是一个对象数组,参考下例
131
+ ajaxHooker.filter([
132
+ {type: 'xhr', url: 'www.example.com', method: 'GET', async: true},
133
+ {url: /^http/},
134
+ ]);
135
+ */
136
+ filter(filterOptions: UtilsAjaxHookFilterOptions[]): void;
137
+ /**
138
+ * 阻止xhr和fetch被改写
139
+ * @example
140
+ // 如果库劫持失败,可能是其他代码对xhr/fetch进行了二次劫持,protect方法会尝试阻止xhr和fetch被改写。应于document-start阶段尽早执行,部分网页下可能引发错误,谨慎使用。
141
+ ajaxHooker.protect();
142
+ */
143
+ protect(): void;
144
+ /**
145
+ * 取消劫持
146
+ * @example
147
+ // 将xhr和fetch恢复至劫持前的状态,调用此方法后,hook方法不再生效。
148
+ ajaxHooker.unhook();
149
+ */
150
+ unhook(): void;
151
+ }
@@ -1,7 +1,7 @@
1
- /* eslint-disable no-var */
2
- declare var jQuery: any;
3
- declare var unsafeWindow: Window;
4
-
5
- declare var mozIndexedDB: IDBFactory | null;
6
- declare var webkitIndexedDB: IDBFactory | null;
7
- declare var msIndexedDB: IDBFactory | null;
1
+ /* eslint-disable no-var */
2
+ declare var jQuery: any;
3
+ declare var unsafeWindow: Window;
4
+
5
+ declare var mozIndexedDB: IDBFactory | null;
6
+ declare var webkitIndexedDB: IDBFactory | null;
7
+ declare var msIndexedDB: IDBFactory | null;
@@ -1,31 +1,31 @@
1
- export type JSTypeMap = {
2
- string: string;
3
- number: number;
4
- boolean: boolean;
5
- object: object;
6
- symbol: symbol;
7
- bigint: bigint;
8
- undefined: undefined;
9
- null: null;
10
- };
11
-
12
- export type JSTypeNames = keyof JSTypeMap;
13
-
14
- export type ArgsType<T extends JSTypeNames[]> = {
15
- [I in keyof T]: JSTypeMap[T[I]];
16
- };
17
-
18
- export declare interface UtilsOwnObject<V> {
19
- [key: string]: V | UtilsOwnObject<V>;
20
- }
21
-
22
- export declare interface AnyObject {
23
- [key: string]: any | AnyObject;
24
- toString(): string;
25
- }
26
-
27
- export type PartialKeys<T, K extends keyof T> = {
28
- [P in K]?: T[P];
29
- };
30
-
31
- export type Values<T> = T[keyof T];
1
+ export type JSTypeMap = {
2
+ string: string;
3
+ number: number;
4
+ boolean: boolean;
5
+ object: object;
6
+ symbol: symbol;
7
+ bigint: bigint;
8
+ undefined: undefined;
9
+ null: null;
10
+ };
11
+
12
+ export type JSTypeNames = keyof JSTypeMap;
13
+
14
+ export type ArgsType<T extends JSTypeNames[]> = {
15
+ [I in keyof T]: JSTypeMap[T[I]];
16
+ };
17
+
18
+ export declare interface UtilsOwnObject<V> {
19
+ [key: string]: V | UtilsOwnObject<V>;
20
+ }
21
+
22
+ export declare interface AnyObject {
23
+ [key: string]: any | AnyObject;
24
+ toString(): string;
25
+ }
26
+
27
+ export type PartialKeys<T, K extends keyof T> = {
28
+ [P in K]?: T[P];
29
+ };
30
+
31
+ export type Values<T> = T[keyof T];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.8.2",
4
+ "version": "2.9.0",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
@@ -36,28 +36,32 @@
36
36
  "author": "WhiteSev",
37
37
  "license": "MIT",
38
38
  "dependencies": {
39
- "worker-timers": "^8.0.21"
39
+ "worker-timers": "^8.0.25"
40
40
  },
41
41
  "devDependencies": {
42
- "@eslint/js": "^9.35.0",
42
+ "@eslint/js": "^9.36.0",
43
43
  "@rollup/plugin-commonjs": "^28.0.6",
44
44
  "@rollup/plugin-json": "^6.1.0",
45
45
  "@rollup/plugin-node-resolve": "^16.0.1",
46
- "@rollup/plugin-typescript": "^12.1.2",
46
+ "@rollup/plugin-terser": "^0.4.4",
47
+ "@rollup/plugin-typescript": "^12.1.4",
47
48
  "browserslist": "^4.26.2",
48
- "caniuse-lite": "^1.0.30001743",
49
- "eslint": "^9.35.0",
49
+ "caniuse-lite": "^1.0.30001745",
50
+ "eslint": "^9.36.0",
50
51
  "eslint-config-prettier": "^10.1.8",
51
52
  "eslint-plugin-compat": "^6.0.2",
52
53
  "eslint-plugin-prettier": "^5.5.4",
53
54
  "globals": "^16.4.0",
54
- "rollup": "^4.44.1",
55
+ "rollup": "^4.52.3",
55
56
  "rollup-plugin-clear": "^2.0.7",
56
57
  "tslib": "^2.8.1",
57
- "typescript": "^5.8.3",
58
- "typescript-eslint": "^8.44.0"
58
+ "typescript": "^5.9.2",
59
+ "typescript-eslint": "^8.44.1"
59
60
  },
60
61
  "scripts": {
62
+ "install": "pnpm install --ignore-workspace",
63
+ "outdated": "pnpm outdated --ignore-workspace",
64
+ "update": "pnpm update --latest --ignore-workspace",
61
65
  "lint": "eslint .",
62
66
  "lint:fix": "eslint . --fix",
63
67
  "format": "prettier . --write",
@@ -1,105 +1,105 @@
1
- export class ColorConversion {
2
- /**
3
- * 判断是否是16进制颜色
4
- * @param str
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
20
- * @param opacity
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 str
35
- */
36
- hexToRgb(str: string) {
37
- if (!this.isHex(str)) {
38
- throw new TypeError(`输入错误的hex:${str}`);
39
- }
40
- /* replace替换查找的到的字符串 */
41
- str = str.replace("#", "");
42
- /* match得到查询数组 */
43
- const hxs = str.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
- */
57
- rgbToHex(redValue: string | number, greenValue: string | number, blueValue: string | number): string {
58
- /* 验证输入的rgb值是否合法 */
59
- const validPattern = /^\d{1,3}$/;
60
- if (
61
- !validPattern.test(redValue.toString()) ||
62
- !validPattern.test(greenValue.toString()) ||
63
- !validPattern.test(blueValue.toString())
64
- )
65
- throw new TypeError("输入错误的rgb颜色值");
66
- const hexs = [redValue.toString(16), greenValue.toString(16), blueValue.toString(16)];
67
- for (let index = 0; index < 3; index++) if (hexs[index].length == 1) hexs[index] = `0${hexs[index]}`;
68
- return `#${hexs.join("")}`;
69
- }
70
- /**
71
- * 获取颜色变暗或亮
72
- * @param color 颜色
73
- * @param level 0~1.0
74
- */
75
- getDarkColor(color: string, level: string): string {
76
- if (!this.isHex(color)) {
77
- throw new TypeError(`输入错误的hex:${color}`);
78
- }
79
- const rgbc = this.hexToRgb(color);
80
- for (let index = 0; index < 3; index++) {
81
- const rgbcItemValue = rgbc[index];
82
- const value = Math.floor(Number(rgbcItemValue) * (1 - Number(level)));
83
- Reflect.set(rgbc, index, value);
84
- }
85
-
86
- return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
87
- }
88
- /**
89
- * 获取颜色变亮
90
- * @param color 颜色
91
- * @param level 0~1.0
92
- */
93
- getLightColor(color: string, level: number): string {
94
- if (!this.isHex(color)) {
95
- throw new TypeError(`输入错误的hex:${color}`);
96
- }
97
- const rgbc = this.hexToRgb(color);
98
- for (let index = 0; index < 3; index++) {
99
- const rgbcItemValue = Number(rgbc[index]);
100
- const value = Math.floor(255 - rgbcItemValue * level + rgbcItemValue);
101
- Reflect.set(rgbc, index, value);
102
- }
103
- return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
104
- }
105
- }
1
+ export class ColorConversion {
2
+ /**
3
+ * 判断是否是16进制颜色
4
+ * @param str
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
20
+ * @param opacity
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 str
35
+ */
36
+ hexToRgb(str: string) {
37
+ if (!this.isHex(str)) {
38
+ throw new TypeError(`输入错误的hex:${str}`);
39
+ }
40
+ /* replace替换查找的到的字符串 */
41
+ str = str.replace("#", "");
42
+ /* match得到查询数组 */
43
+ const hxs = str.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
+ */
57
+ rgbToHex(redValue: string | number, greenValue: string | number, blueValue: string | number): string {
58
+ /* 验证输入的rgb值是否合法 */
59
+ const validPattern = /^\d{1,3}$/;
60
+ if (
61
+ !validPattern.test(redValue.toString()) ||
62
+ !validPattern.test(greenValue.toString()) ||
63
+ !validPattern.test(blueValue.toString())
64
+ )
65
+ throw new TypeError("输入错误的rgb颜色值");
66
+ const hexs = [redValue.toString(16), greenValue.toString(16), blueValue.toString(16)];
67
+ for (let index = 0; index < 3; index++) if (hexs[index].length == 1) hexs[index] = `0${hexs[index]}`;
68
+ return `#${hexs.join("")}`;
69
+ }
70
+ /**
71
+ * 获取颜色变暗或亮
72
+ * @param color 颜色
73
+ * @param level 0~1.0
74
+ */
75
+ getDarkColor(color: string, level: string): string {
76
+ if (!this.isHex(color)) {
77
+ throw new TypeError(`输入错误的hex:${color}`);
78
+ }
79
+ const rgbc = this.hexToRgb(color);
80
+ for (let index = 0; index < 3; index++) {
81
+ const rgbcItemValue = rgbc[index];
82
+ const value = Math.floor(Number(rgbcItemValue) * (1 - Number(level)));
83
+ Reflect.set(rgbc, index, value);
84
+ }
85
+
86
+ return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
87
+ }
88
+ /**
89
+ * 获取颜色变亮
90
+ * @param color 颜色
91
+ * @param level 0~1.0
92
+ */
93
+ getLightColor(color: string, level: number): string {
94
+ if (!this.isHex(color)) {
95
+ throw new TypeError(`输入错误的hex:${color}`);
96
+ }
97
+ const rgbc = this.hexToRgb(color);
98
+ for (let index = 0; index < 3; index++) {
99
+ const rgbcItemValue = Number(rgbc[index]);
100
+ const value = Math.floor(255 - rgbcItemValue * level + rgbcItemValue);
101
+ Reflect.set(rgbc, index, value);
102
+ }
103
+ return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
104
+ }
105
+ }