@zcrkey/js-utils 0.0.6 → 0.0.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.
- package/dist/cjs/color.d.ts +195 -0
- package/dist/cjs/color.js +357 -0
- package/dist/cjs/index.d.ts +5 -4
- package/dist/cjs/index.js +7 -4
- package/dist/cjs/{objUtil.js → obj.js} +4 -4
- package/dist/{esm/treeUtil.d.ts → cjs/tree.d.ts} +6 -2
- package/dist/cjs/{treeUtil.js → tree.js} +46 -12
- package/dist/cjs/util.d.ts +17 -5
- package/dist/cjs/util.js +39 -4
- package/dist/esm/color.d.ts +195 -0
- package/dist/esm/color.js +500 -0
- package/dist/esm/index.d.ts +5 -4
- package/dist/esm/index.js +3 -2
- package/dist/{cjs/treeUtil.d.ts → esm/tree.d.ts} +6 -2
- package/dist/esm/{treeUtil.js → tree.js} +39 -10
- package/dist/esm/util.d.ts +17 -5
- package/dist/esm/util.js +53 -4
- package/dist/umd/index.umd.js +1 -1
- package/package.json +1 -1
- /package/dist/cjs/{objUtil.d.ts → obj.d.ts} +0 -0
- /package/dist/esm/{objUtil.d.ts → obj.d.ts} +0 -0
- /package/dist/esm/{objUtil.js → obj.js} +0 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
export default class CrColorUtil {
|
|
2
|
+
/**
|
|
3
|
+
* hex 转 rgb 数组 [r,g,b] r/g/b范围 0-255
|
|
4
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
5
|
+
*/
|
|
6
|
+
static hexToRgb(hex: string): number[];
|
|
7
|
+
/**
|
|
8
|
+
* rgb 转 hex 标准格式
|
|
9
|
+
* @param r 0-255
|
|
10
|
+
* @param g 0-255
|
|
11
|
+
* @param b 0-255
|
|
12
|
+
*/
|
|
13
|
+
static rgbToHex(r: number, g: number, b: number): string;
|
|
14
|
+
/**
|
|
15
|
+
* 将 rgb 格式转为 hsv 格式
|
|
16
|
+
* input: r,g,b in [0,1], out: h in [0,360) and s,v in [0,1]
|
|
17
|
+
* @param r 0-1
|
|
18
|
+
* @param g 0-1
|
|
19
|
+
* @param b 0-1
|
|
20
|
+
*/
|
|
21
|
+
static rgb2hsv(r: number, g: number, b: number): number[];
|
|
22
|
+
/**
|
|
23
|
+
* 将 hsv 格式转为 rgb 格式
|
|
24
|
+
* input: h in [0,360] and s,v in [0,1] - output: r,g,b in [0,1]
|
|
25
|
+
* @param h 0-360
|
|
26
|
+
* @param s 0-1
|
|
27
|
+
* @param v 0-1
|
|
28
|
+
*/
|
|
29
|
+
static hsv2rgb(h: number, s: number, v: number): number[];
|
|
30
|
+
/**
|
|
31
|
+
* hex 转 rgba 格式字符串 可直接用于css
|
|
32
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
33
|
+
* @param alpha 透明度 0-1 默认1(完全不透明)
|
|
34
|
+
* @returns 例: rgba(255,255,255,0.5)
|
|
35
|
+
*/
|
|
36
|
+
static hexToRgba(hex: string, alpha?: number): string;
|
|
37
|
+
/**
|
|
38
|
+
* 给hex颜色添加透明度,返回 8位带透明通道的hex格式 #RRGGBBAA (CSS常用)
|
|
39
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
40
|
+
* @param alpha 透明度 0-1 默认1(完全不透明)
|
|
41
|
+
* @returns 例: #ffffff80 #ff000033
|
|
42
|
+
*/
|
|
43
|
+
static hexAddAlpha(hex: string, alpha?: number): string;
|
|
44
|
+
/**
|
|
45
|
+
* rgba 转 hex 格式
|
|
46
|
+
* @param r 0-255
|
|
47
|
+
* @param g 0-255
|
|
48
|
+
* @param b 0-255
|
|
49
|
+
*/
|
|
50
|
+
static rgbaToHex(r: number, g: number, b: number): string;
|
|
51
|
+
/**
|
|
52
|
+
* hsv 转 hex 十六进制颜色
|
|
53
|
+
* @param h 色相 0-360
|
|
54
|
+
* @param s 饱和度 0-1
|
|
55
|
+
* @param v 明度 0-1
|
|
56
|
+
*/
|
|
57
|
+
static hsvToHex(h: number, s: number, v: number): string;
|
|
58
|
+
/**
|
|
59
|
+
* hex 转 hsv 格式
|
|
60
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
61
|
+
* @returns [h:0-360, s:0-1, v:0-1]
|
|
62
|
+
*/
|
|
63
|
+
static hexToHsv(hex: string): number[];
|
|
64
|
+
/**
|
|
65
|
+
* 颜色加深处理 - 基于hex颜色
|
|
66
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
67
|
+
* @param level 加深程度 0-1 0不变 1最深(接近黑色)
|
|
68
|
+
*/
|
|
69
|
+
static darkenColor(hex: string, level: number): string;
|
|
70
|
+
/**
|
|
71
|
+
* 颜色减淡/提亮处理 - 基于hex颜色
|
|
72
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
73
|
+
* @param level 减淡程度 0-1 0不变 1最亮(接近白色)
|
|
74
|
+
*/
|
|
75
|
+
static lightenColor(hex: string, level: number): string;
|
|
76
|
+
/**
|
|
77
|
+
* 解析8位带透明的hex #RRGGBBAA 为 普通hex + 透明度
|
|
78
|
+
* @param hex8 8位带透明的十六进制颜色 例:#ffffff80
|
|
79
|
+
* @returns { hex: '#ffffff', alpha: 0.5 }
|
|
80
|
+
*/
|
|
81
|
+
static parseHex8(hex8: string): {
|
|
82
|
+
hex: string;
|
|
83
|
+
alpha: number;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* rgb 数组 转 rgb/rgba 字符串
|
|
87
|
+
* @param rgb [r,g,b] 0-255
|
|
88
|
+
* @param alpha 透明度0-1 传则返回rgba,不传返回rgb
|
|
89
|
+
*/
|
|
90
|
+
static rgbToStr(rgb: number[], alpha?: number): string;
|
|
91
|
+
/**
|
|
92
|
+
* Hex颜色字符串 转 十进制数值(6位纯颜色值,无透明通道,专用于颜色运算)
|
|
93
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
94
|
+
* @returns 十进制数值 例:#ff0000 → 16711680,非法入参返回 0
|
|
95
|
+
*/
|
|
96
|
+
static hexToNumber(hex: string): number;
|
|
97
|
+
/**
|
|
98
|
+
* 十进制数值 转回 Hex颜色字符串(无透明通道 标准6位)
|
|
99
|
+
* @param num 十进制数值 例:16711680 → #ff0000
|
|
100
|
+
* @returns 标准6位十六进制颜色 #RRGGBB 格式
|
|
101
|
+
*/
|
|
102
|
+
static numberToHex(num: number): string;
|
|
103
|
+
/**
|
|
104
|
+
* ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
|
|
105
|
+
* @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
|
|
106
|
+
* @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
|
|
107
|
+
*/
|
|
108
|
+
static argbToNumber(argb: string): number;
|
|
109
|
+
/**
|
|
110
|
+
* 十进制数值 转回 ARGB颜色字符串(8位带透明通道,标准#AARRGGBB格式)
|
|
111
|
+
* @param num 十进制数值 例:2147483647 → #80ffffff
|
|
112
|
+
* @returns 标准ARGB颜色字符串 #AARRGGBB 小写格式
|
|
113
|
+
*/
|
|
114
|
+
static numberToArgb(num: number): string;
|
|
115
|
+
/**
|
|
116
|
+
* 生成随机Hex颜色
|
|
117
|
+
* @param withSharp 是否带#号 默认为true
|
|
118
|
+
* @returns 随机hex颜色
|
|
119
|
+
*/
|
|
120
|
+
static randomHexColor(withSharp?: boolean): string;
|
|
121
|
+
/**
|
|
122
|
+
* 反色处理 - 取颜色的补色(比如黑色→白色,白色→黑色)
|
|
123
|
+
* @param hex 目标颜色
|
|
124
|
+
* @returns 反色hex
|
|
125
|
+
*/
|
|
126
|
+
static reverseColor(hex: string): string;
|
|
127
|
+
/**
|
|
128
|
+
* 校验是否为合法的hex颜色
|
|
129
|
+
* @param hex 待校验的颜色字符串
|
|
130
|
+
* @returns true=合法 false=不合法
|
|
131
|
+
*/
|
|
132
|
+
static isHexColor(hex: string): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* 标准6位Hex颜色 转 ARGB 8位颜色格式 ( 核心:ARGB格式是 #AARRGGBB )
|
|
135
|
+
* @param hex 6位标准hex 支持 #ffffff / ffffff / #fff / fff
|
|
136
|
+
* @param alpha 透明度 0~1 默认 1 (完全不透明)
|
|
137
|
+
* @returns 8位ARGB格式颜色 例:#80FFFFFF 半透明白色
|
|
138
|
+
*/
|
|
139
|
+
static hexToArgb(hex: string, alpha?: number): string;
|
|
140
|
+
/**
|
|
141
|
+
* ARGB 8位颜色格式 转回 标准6位Hex颜色 + 独立的透明度值 ( 反向解析 )
|
|
142
|
+
* @param argb 8位ARGB格式颜色 必须是 #AARRGGBB 格式 例:#80FFFFFF
|
|
143
|
+
* @returns { hex: '#ffffff', alpha: 0.5 } 标准hex + 0~1的透明度
|
|
144
|
+
*/
|
|
145
|
+
static argbToHex(argb: string): {
|
|
146
|
+
hex: string;
|
|
147
|
+
alpha: number;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* ARGB格式(#AARRGGBB) 转 RRGGBBAA格式(#RRGGBBAA)
|
|
151
|
+
* @param argb #AARRGGBB
|
|
152
|
+
* @returns #RRGGBBAA
|
|
153
|
+
*/
|
|
154
|
+
static argbToRgbaHex(argb: string): string;
|
|
155
|
+
/**
|
|
156
|
+
* RRGGBBAA格式(#RRGGBBAA) 转 ARGB格式(#AARRGGBB)
|
|
157
|
+
* @param rgbaHex #RRGGBBAA
|
|
158
|
+
* @returns #AARRGGBB
|
|
159
|
+
*/
|
|
160
|
+
static rgbaHexToArgb(rgbaHex: string): string;
|
|
161
|
+
/**
|
|
162
|
+
* 调整颜色饱和度
|
|
163
|
+
* @param hex 目标颜色
|
|
164
|
+
* @param level 调整程度 0-2 1=不变 <1=降低饱和度 >1=提高饱和度
|
|
165
|
+
* @returns 调整后的hex颜色
|
|
166
|
+
*/
|
|
167
|
+
static adjustSaturation(hex: string, level: number): string;
|
|
168
|
+
/**
|
|
169
|
+
* 调整颜色明度
|
|
170
|
+
* @param hex 目标颜色
|
|
171
|
+
* @param level 调整程度 0-2 1=不变 <1=降低明度 >1=提高明度
|
|
172
|
+
* @returns 调整后的hex颜色
|
|
173
|
+
*/
|
|
174
|
+
static adjustBrightness(hex: string, level: number): string;
|
|
175
|
+
/**
|
|
176
|
+
* 普通6位Hex+透明度 → 直接返回ARGB对应的十进制数值
|
|
177
|
+
* @param hex 支持 #fff / #ffffff 格式
|
|
178
|
+
* @param alpha 透明度 0-1
|
|
179
|
+
* @returns ARGB十进制数值
|
|
180
|
+
*/
|
|
181
|
+
static hexAlphaToArgbNumber(hex: string, alpha?: number): number;
|
|
182
|
+
/**
|
|
183
|
+
* ARGB十进制数值 → 解析出 普通Hex + 0-1透明度
|
|
184
|
+
* @param num ARGB十进制数值
|
|
185
|
+
* @returns { hex: '#ffffff', alpha: 0.5 }
|
|
186
|
+
*/
|
|
187
|
+
static argbNumberToHexAlpha(num: number): {
|
|
188
|
+
hex: string;
|
|
189
|
+
alpha: number;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* 补零成两位字符
|
|
193
|
+
*/
|
|
194
|
+
private static twoDigits;
|
|
195
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/color.ts
|
|
20
|
+
var color_exports = {};
|
|
21
|
+
__export(color_exports, {
|
|
22
|
+
default: () => CrColorUtil
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(color_exports);
|
|
25
|
+
var CrColorUtil = class {
|
|
26
|
+
/**
|
|
27
|
+
* hex 转 rgb 数组 [r,g,b] r/g/b范围 0-255
|
|
28
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
29
|
+
*/
|
|
30
|
+
static hexToRgb(hex) {
|
|
31
|
+
let hexVal = hex.replace("#", "");
|
|
32
|
+
if (hexVal.length === 3) {
|
|
33
|
+
hexVal = hexVal.split("").map((c) => c + c).join("");
|
|
34
|
+
}
|
|
35
|
+
const hexInt = parseInt(hexVal, 16);
|
|
36
|
+
const r = hexInt >> 16 & 255;
|
|
37
|
+
const g = hexInt >> 8 & 255;
|
|
38
|
+
const b = hexInt & 255;
|
|
39
|
+
return [r, g, b];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* rgb 转 hex 标准格式
|
|
43
|
+
* @param r 0-255
|
|
44
|
+
* @param g 0-255
|
|
45
|
+
* @param b 0-255
|
|
46
|
+
*/
|
|
47
|
+
static rgbToHex(r, g, b) {
|
|
48
|
+
const clamp = (val) => Math.max(0, Math.min(255, Math.round(val)));
|
|
49
|
+
const red = clamp(r);
|
|
50
|
+
const green = clamp(g);
|
|
51
|
+
const blue = clamp(b);
|
|
52
|
+
return "#" + this.twoDigits(red.toString(16)) + this.twoDigits(green.toString(16)) + this.twoDigits(blue.toString(16));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 将 rgb 格式转为 hsv 格式
|
|
56
|
+
* input: r,g,b in [0,1], out: h in [0,360) and s,v in [0,1]
|
|
57
|
+
* @param r 0-1
|
|
58
|
+
* @param g 0-1
|
|
59
|
+
* @param b 0-1
|
|
60
|
+
*/
|
|
61
|
+
static rgb2hsv(r, g, b) {
|
|
62
|
+
let v = Math.max(r, g, b);
|
|
63
|
+
let n = v - Math.min(r, g, b);
|
|
64
|
+
let h = n && (v == r ? (g - b) / n : v == g ? 2 + (b - r) / n : 4 + (r - g) / n);
|
|
65
|
+
return [60 * (h < 0 ? h + 6 : h), v && n / v, v];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 将 hsv 格式转为 rgb 格式
|
|
69
|
+
* input: h in [0,360] and s,v in [0,1] - output: r,g,b in [0,1]
|
|
70
|
+
* @param h 0-360
|
|
71
|
+
* @param s 0-1
|
|
72
|
+
* @param v 0-1
|
|
73
|
+
*/
|
|
74
|
+
static hsv2rgb(h, s, v) {
|
|
75
|
+
let f = (n, k = (n + h / 60) % 6) => v - v * s * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
76
|
+
return [f(5), f(3), f(1)];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* hex 转 rgba 格式字符串 可直接用于css
|
|
80
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
81
|
+
* @param alpha 透明度 0-1 默认1(完全不透明)
|
|
82
|
+
* @returns 例: rgba(255,255,255,0.5)
|
|
83
|
+
*/
|
|
84
|
+
static hexToRgba(hex, alpha = 1) {
|
|
85
|
+
const [r, g, b] = this.hexToRgb(hex);
|
|
86
|
+
const validAlpha = Math.max(0, Math.min(1, alpha));
|
|
87
|
+
return `rgba(${r}, ${g}, ${b}, ${validAlpha})`;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 给hex颜色添加透明度,返回 8位带透明通道的hex格式 #RRGGBBAA (CSS常用)
|
|
91
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
92
|
+
* @param alpha 透明度 0-1 默认1(完全不透明)
|
|
93
|
+
* @returns 例: #ffffff80 #ff000033
|
|
94
|
+
*/
|
|
95
|
+
static hexAddAlpha(hex, alpha = 1) {
|
|
96
|
+
let pureHex = hex.replace("#", "");
|
|
97
|
+
pureHex = pureHex.length === 3 ? pureHex.split("").map((char) => char + char).join("") : pureHex;
|
|
98
|
+
const validAlpha = Math.max(0, Math.min(1, alpha));
|
|
99
|
+
const alphaHex = this.twoDigits(Math.round(validAlpha * 255).toString(16));
|
|
100
|
+
return `#${pureHex}${alphaHex}`;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* rgba 转 hex 格式
|
|
104
|
+
* @param r 0-255
|
|
105
|
+
* @param g 0-255
|
|
106
|
+
* @param b 0-255
|
|
107
|
+
*/
|
|
108
|
+
static rgbaToHex(r, g, b) {
|
|
109
|
+
return this.rgbToHex(r, g, b);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* hsv 转 hex 十六进制颜色
|
|
113
|
+
* @param h 色相 0-360
|
|
114
|
+
* @param s 饱和度 0-1
|
|
115
|
+
* @param v 明度 0-1
|
|
116
|
+
*/
|
|
117
|
+
static hsvToHex(h, s, v) {
|
|
118
|
+
const [r, g, b] = this.hsv2rgb(h, s, v);
|
|
119
|
+
return this.rgbToHex(
|
|
120
|
+
Math.round(r * 255),
|
|
121
|
+
Math.round(g * 255),
|
|
122
|
+
Math.round(b * 255)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* hex 转 hsv 格式
|
|
127
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
128
|
+
* @returns [h:0-360, s:0-1, v:0-1]
|
|
129
|
+
*/
|
|
130
|
+
static hexToHsv(hex) {
|
|
131
|
+
const [r, g, b] = this.hexToRgb(hex);
|
|
132
|
+
return this.rgb2hsv(r / 255, g / 255, b / 255);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 颜色加深处理 - 基于hex颜色
|
|
136
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
137
|
+
* @param level 加深程度 0-1 0不变 1最深(接近黑色)
|
|
138
|
+
*/
|
|
139
|
+
static darkenColor(hex, level) {
|
|
140
|
+
const [r, g, b] = this.hexToRgb(hex);
|
|
141
|
+
const clamp = (val) => Math.max(0, Math.min(255, val));
|
|
142
|
+
const darkR = clamp(r - r * level);
|
|
143
|
+
const darkG = clamp(g - g * level);
|
|
144
|
+
const darkB = clamp(b - b * level);
|
|
145
|
+
return this.rgbToHex(darkR, darkG, darkB);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 颜色减淡/提亮处理 - 基于hex颜色
|
|
149
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
150
|
+
* @param level 减淡程度 0-1 0不变 1最亮(接近白色)
|
|
151
|
+
*/
|
|
152
|
+
static lightenColor(hex, level) {
|
|
153
|
+
const [r, g, b] = this.hexToRgb(hex);
|
|
154
|
+
const clamp = (val) => Math.max(0, Math.min(255, val));
|
|
155
|
+
const lightR = clamp(r + (255 - r) * level);
|
|
156
|
+
const lightG = clamp(g + (255 - g) * level);
|
|
157
|
+
const lightB = clamp(b + (255 - b) * level);
|
|
158
|
+
return this.rgbToHex(lightR, lightG, lightB);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 解析8位带透明的hex #RRGGBBAA 为 普通hex + 透明度
|
|
162
|
+
* @param hex8 8位带透明的十六进制颜色 例:#ffffff80
|
|
163
|
+
* @returns { hex: '#ffffff', alpha: 0.5 }
|
|
164
|
+
*/
|
|
165
|
+
static parseHex8(hex8) {
|
|
166
|
+
const pureHex = hex8.replace("#", "");
|
|
167
|
+
if (pureHex.length !== 8)
|
|
168
|
+
return { hex: hex8, alpha: 1 };
|
|
169
|
+
const hex = `#${pureHex.slice(0, 6)}`;
|
|
170
|
+
const alphaHex = pureHex.slice(6);
|
|
171
|
+
const alpha = Math.round(parseInt(alphaHex, 16) / 255 * 100) / 100;
|
|
172
|
+
return { hex, alpha };
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* rgb 数组 转 rgb/rgba 字符串
|
|
176
|
+
* @param rgb [r,g,b] 0-255
|
|
177
|
+
* @param alpha 透明度0-1 传则返回rgba,不传返回rgb
|
|
178
|
+
*/
|
|
179
|
+
static rgbToStr(rgb, alpha) {
|
|
180
|
+
const [r, g, b] = rgb;
|
|
181
|
+
return alpha !== void 0 ? `rgba(${r}, ${g}, ${b}, ${Math.max(0, Math.min(1, alpha))})` : `rgb(${r}, ${g}, ${b})`;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Hex颜色字符串 转 十进制数值(6位纯颜色值,无透明通道,专用于颜色运算)
|
|
185
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
186
|
+
* @returns 十进制数值 例:#ff0000 → 16711680,非法入参返回 0
|
|
187
|
+
*/
|
|
188
|
+
static hexToNumber(hex) {
|
|
189
|
+
const pureHex = hex.replace("#", "");
|
|
190
|
+
if (!/^[0-9a-fA-F]{3}|[0-9a-fA-F]{6}$/.test(pureHex)) {
|
|
191
|
+
return 0;
|
|
192
|
+
}
|
|
193
|
+
return parseInt(pureHex, 16);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 十进制数值 转回 Hex颜色字符串(无透明通道 标准6位)
|
|
197
|
+
* @param num 十进制数值 例:16711680 → #ff0000
|
|
198
|
+
* @returns 标准6位十六进制颜色 #RRGGBB 格式
|
|
199
|
+
*/
|
|
200
|
+
static numberToHex(num) {
|
|
201
|
+
const r = this.twoDigits((num >> 16 & 255).toString(16));
|
|
202
|
+
const g = this.twoDigits((num >> 8 & 255).toString(16));
|
|
203
|
+
const b = this.twoDigits((num & 255).toString(16));
|
|
204
|
+
return `#${r}${g}${b}`;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
|
|
208
|
+
* @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
|
|
209
|
+
* @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
|
|
210
|
+
*/
|
|
211
|
+
static argbToNumber(argb) {
|
|
212
|
+
const pureArgb = argb.replace("#", "");
|
|
213
|
+
if (!/^[0-9a-fA-F]{8}$/.test(pureArgb)) {
|
|
214
|
+
return 0;
|
|
215
|
+
}
|
|
216
|
+
return parseInt(pureArgb, 16);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* 十进制数值 转回 ARGB颜色字符串(8位带透明通道,标准#AARRGGBB格式)
|
|
220
|
+
* @param num 十进制数值 例:2147483647 → #80ffffff
|
|
221
|
+
* @returns 标准ARGB颜色字符串 #AARRGGBB 小写格式
|
|
222
|
+
*/
|
|
223
|
+
static numberToArgb(num) {
|
|
224
|
+
const a = this.twoDigits((num >> 24 & 255).toString(16));
|
|
225
|
+
const r = this.twoDigits((num >> 16 & 255).toString(16));
|
|
226
|
+
const g = this.twoDigits((num >> 8 & 255).toString(16));
|
|
227
|
+
const b = this.twoDigits((num & 255).toString(16));
|
|
228
|
+
return `#${a}${r}${g}${b}`;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* 生成随机Hex颜色
|
|
232
|
+
* @param withSharp 是否带#号 默认为true
|
|
233
|
+
* @returns 随机hex颜色
|
|
234
|
+
*/
|
|
235
|
+
static randomHexColor(withSharp = true) {
|
|
236
|
+
const hex = this.twoDigits(Math.floor(Math.random() * 16).toString(16)) + this.twoDigits(Math.floor(Math.random() * 16).toString(16)) + this.twoDigits(Math.floor(Math.random() * 16).toString(16)) + this.twoDigits(Math.floor(Math.random() * 16).toString(16)) + this.twoDigits(Math.floor(Math.random() * 16).toString(16)) + this.twoDigits(Math.floor(Math.random() * 16).toString(16));
|
|
237
|
+
return withSharp ? `#${hex}` : hex;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* 反色处理 - 取颜色的补色(比如黑色→白色,白色→黑色)
|
|
241
|
+
* @param hex 目标颜色
|
|
242
|
+
* @returns 反色hex
|
|
243
|
+
*/
|
|
244
|
+
static reverseColor(hex) {
|
|
245
|
+
const [r, g, b] = this.hexToRgb(hex);
|
|
246
|
+
return this.rgbToHex(255 - r, 255 - g, 255 - b);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* 校验是否为合法的hex颜色
|
|
250
|
+
* @param hex 待校验的颜色字符串
|
|
251
|
+
* @returns true=合法 false=不合法
|
|
252
|
+
*/
|
|
253
|
+
static isHexColor(hex) {
|
|
254
|
+
return /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(hex);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* 标准6位Hex颜色 转 ARGB 8位颜色格式 ( 核心:ARGB格式是 #AARRGGBB )
|
|
258
|
+
* @param hex 6位标准hex 支持 #ffffff / ffffff / #fff / fff
|
|
259
|
+
* @param alpha 透明度 0~1 默认 1 (完全不透明)
|
|
260
|
+
* @returns 8位ARGB格式颜色 例:#80FFFFFF 半透明白色
|
|
261
|
+
*/
|
|
262
|
+
static hexToArgb(hex, alpha = 1) {
|
|
263
|
+
let pureHex = hex.replace("#", "");
|
|
264
|
+
if (pureHex.length === 3) {
|
|
265
|
+
pureHex = pureHex.split("").map((char) => char + char).join("");
|
|
266
|
+
}
|
|
267
|
+
const validAlpha = Math.max(0, Math.min(1, alpha));
|
|
268
|
+
const alphaHex = this.twoDigits(Math.round(validAlpha * 255).toString(16));
|
|
269
|
+
return `#${alphaHex}${pureHex}`;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* ARGB 8位颜色格式 转回 标准6位Hex颜色 + 独立的透明度值 ( 反向解析 )
|
|
273
|
+
* @param argb 8位ARGB格式颜色 必须是 #AARRGGBB 格式 例:#80FFFFFF
|
|
274
|
+
* @returns { hex: '#ffffff', alpha: 0.5 } 标准hex + 0~1的透明度
|
|
275
|
+
*/
|
|
276
|
+
static argbToHex(argb) {
|
|
277
|
+
const pureArgb = argb.replace("#", "");
|
|
278
|
+
if (pureArgb.length !== 8) {
|
|
279
|
+
console.warn("传入的不是合法的ARGB格式,格式必须是 #AARRGGBB");
|
|
280
|
+
return { hex: argb, alpha: 1 };
|
|
281
|
+
}
|
|
282
|
+
const alphaHex = pureArgb.slice(0, 2);
|
|
283
|
+
const hexVal = pureArgb.slice(2);
|
|
284
|
+
const alpha = Math.round(parseInt(alphaHex, 16) / 255 * 100) / 100;
|
|
285
|
+
return { hex: `#${hexVal}`, alpha };
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* ARGB格式(#AARRGGBB) 转 RRGGBBAA格式(#RRGGBBAA)
|
|
289
|
+
* @param argb #AARRGGBB
|
|
290
|
+
* @returns #RRGGBBAA
|
|
291
|
+
*/
|
|
292
|
+
static argbToRgbaHex(argb) {
|
|
293
|
+
const pureArgb = argb.replace("#", "");
|
|
294
|
+
if (pureArgb.length !== 8)
|
|
295
|
+
return argb;
|
|
296
|
+
return `#${pureArgb.slice(2)}${pureArgb.slice(0, 2)}`;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* RRGGBBAA格式(#RRGGBBAA) 转 ARGB格式(#AARRGGBB)
|
|
300
|
+
* @param rgbaHex #RRGGBBAA
|
|
301
|
+
* @returns #AARRGGBB
|
|
302
|
+
*/
|
|
303
|
+
static rgbaHexToArgb(rgbaHex) {
|
|
304
|
+
const pureRgba = rgbaHex.replace("#", "");
|
|
305
|
+
if (pureRgba.length !== 8)
|
|
306
|
+
return rgbaHex;
|
|
307
|
+
return `#${pureRgba.slice(6)}${pureRgba.slice(0, 6)}`;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* 调整颜色饱和度
|
|
311
|
+
* @param hex 目标颜色
|
|
312
|
+
* @param level 调整程度 0-2 1=不变 <1=降低饱和度 >1=提高饱和度
|
|
313
|
+
* @returns 调整后的hex颜色
|
|
314
|
+
*/
|
|
315
|
+
static adjustSaturation(hex, level) {
|
|
316
|
+
const [h, s, v] = this.hexToHsv(hex);
|
|
317
|
+
const newS = Math.max(0, Math.min(1, s * level));
|
|
318
|
+
return this.hsvToHex(h, newS, v);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* 调整颜色明度
|
|
322
|
+
* @param hex 目标颜色
|
|
323
|
+
* @param level 调整程度 0-2 1=不变 <1=降低明度 >1=提高明度
|
|
324
|
+
* @returns 调整后的hex颜色
|
|
325
|
+
*/
|
|
326
|
+
static adjustBrightness(hex, level) {
|
|
327
|
+
const [h, s, v] = this.hexToHsv(hex);
|
|
328
|
+
const newV = Math.max(0, Math.min(1, v * level));
|
|
329
|
+
return this.hsvToHex(h, s, newV);
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* 普通6位Hex+透明度 → 直接返回ARGB对应的十进制数值
|
|
333
|
+
* @param hex 支持 #fff / #ffffff 格式
|
|
334
|
+
* @param alpha 透明度 0-1
|
|
335
|
+
* @returns ARGB十进制数值
|
|
336
|
+
*/
|
|
337
|
+
static hexAlphaToArgbNumber(hex, alpha = 1) {
|
|
338
|
+
const argbStr = this.hexToArgb(hex, alpha);
|
|
339
|
+
return this.argbToNumber(argbStr);
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* ARGB十进制数值 → 解析出 普通Hex + 0-1透明度
|
|
343
|
+
* @param num ARGB十进制数值
|
|
344
|
+
* @returns { hex: '#ffffff', alpha: 0.5 }
|
|
345
|
+
*/
|
|
346
|
+
static argbNumberToHexAlpha(num) {
|
|
347
|
+
const argbStr = this.numberToArgb(num);
|
|
348
|
+
return this.argbToHex(argbStr);
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* 补零成两位字符
|
|
352
|
+
*/
|
|
353
|
+
static twoDigits(num) {
|
|
354
|
+
const s = num.toString();
|
|
355
|
+
return s.length === 1 ? "0" + s : s;
|
|
356
|
+
}
|
|
357
|
+
};
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
export { default as CrColorUtil } from './color';
|
|
1
2
|
export type * from './eventCenter';
|
|
2
3
|
export { default as CrEventCenter } from './eventCenter';
|
|
3
|
-
export type * from './
|
|
4
|
-
export { default as CrObjUtil } from './
|
|
4
|
+
export type * from './obj';
|
|
5
|
+
export { default as CrObjUtil } from './obj';
|
|
5
6
|
export { default as CrStorage } from './storage';
|
|
6
|
-
export type * from './
|
|
7
|
-
export { default as CrTreeUtil } from './
|
|
7
|
+
export type * from './tree';
|
|
8
|
+
export { default as CrTreeUtil } from './tree';
|
|
8
9
|
export { default as CrUtil } from './util';
|
package/dist/cjs/index.js
CHANGED
|
@@ -29,20 +29,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
// src/index.ts
|
|
30
30
|
var src_exports = {};
|
|
31
31
|
__export(src_exports, {
|
|
32
|
+
CrColorUtil: () => import_color.default,
|
|
32
33
|
CrEventCenter: () => import_eventCenter.default,
|
|
33
|
-
CrObjUtil: () =>
|
|
34
|
+
CrObjUtil: () => import_obj.default,
|
|
34
35
|
CrStorage: () => import_storage.default,
|
|
35
|
-
CrTreeUtil: () =>
|
|
36
|
+
CrTreeUtil: () => import_tree.default,
|
|
36
37
|
CrUtil: () => import_util.default
|
|
37
38
|
});
|
|
38
39
|
module.exports = __toCommonJS(src_exports);
|
|
40
|
+
var import_color = __toESM(require("./color"));
|
|
39
41
|
var import_eventCenter = __toESM(require("./eventCenter"));
|
|
40
|
-
var
|
|
42
|
+
var import_obj = __toESM(require("./obj"));
|
|
41
43
|
var import_storage = __toESM(require("./storage"));
|
|
42
|
-
var
|
|
44
|
+
var import_tree = __toESM(require("./tree"));
|
|
43
45
|
var import_util = __toESM(require("./util"));
|
|
44
46
|
// Annotate the CommonJS export names for ESM import in node:
|
|
45
47
|
0 && (module.exports = {
|
|
48
|
+
CrColorUtil,
|
|
46
49
|
CrEventCenter,
|
|
47
50
|
CrObjUtil,
|
|
48
51
|
CrStorage,
|
|
@@ -16,12 +16,12 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
};
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
18
|
|
|
19
|
-
// src/
|
|
20
|
-
var
|
|
21
|
-
__export(
|
|
19
|
+
// src/obj.ts
|
|
20
|
+
var obj_exports = {};
|
|
21
|
+
__export(obj_exports, {
|
|
22
22
|
default: () => CrObjUtil
|
|
23
23
|
});
|
|
24
|
-
module.exports = __toCommonJS(
|
|
24
|
+
module.exports = __toCommonJS(obj_exports);
|
|
25
25
|
var import_lodash = require("lodash");
|
|
26
26
|
var CrObjUtil = class {
|
|
27
27
|
/**
|
|
@@ -21,13 +21,17 @@ export default class CrTreeUtil {
|
|
|
21
21
|
* 树形数据转列表数据
|
|
22
22
|
* @param tree
|
|
23
23
|
* @param options
|
|
24
|
+
* @param options.idField 默认值 id
|
|
25
|
+
* @param options.parentIdField 默认值 parentId
|
|
24
26
|
* @param options.childrenField 默认值 children
|
|
25
|
-
* @param options.
|
|
27
|
+
* @param options.rootParentValue 根节点的父值,默认值 null
|
|
26
28
|
* @returns
|
|
27
29
|
*/
|
|
28
30
|
static treeToList<T extends Record<string, any>>(tree: T[], options?: {
|
|
31
|
+
idField?: string;
|
|
32
|
+
parentIdField?: string;
|
|
29
33
|
childrenField?: string;
|
|
30
|
-
|
|
34
|
+
rootParentValue?: any;
|
|
31
35
|
}): T[];
|
|
32
36
|
/**
|
|
33
37
|
* 获取扁平化父数据(包含自身)
|