@zcrkey/js-utils 0.0.11 → 0.0.15
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/index.cjs.js +23 -0
- package/dist/index.es.js +5943 -0
- package/dist/index.umd.js +23 -0
- package/dist/types/color.d.ts +303 -0
- package/dist/{cjs → types}/eventCenter.d.ts +5 -5
- package/dist/types/file.d.ts +481 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/obj.d.ts +20 -0
- package/dist/{esm → types}/storage.d.ts +1 -1
- package/dist/{esm → types}/tree.d.ts +1 -1
- package/dist/{cjs → types}/util.d.ts +27 -7
- package/package.json +14 -19
- package/dist/cjs/color.d.ts +0 -195
- package/dist/cjs/color.js +0 -357
- package/dist/cjs/eventCenter.js +0 -120
- package/dist/cjs/file.d.ts +0 -71
- package/dist/cjs/file.js +0 -154
- package/dist/cjs/index.d.ts +0 -11
- package/dist/cjs/index.js +0 -57
- package/dist/cjs/obj.d.ts +0 -12
- package/dist/cjs/obj.js +0 -37
- package/dist/cjs/storage.d.ts +0 -44
- package/dist/cjs/storage.js +0 -118
- package/dist/cjs/tree.d.ts +0 -52
- package/dist/cjs/tree.js +0 -179
- package/dist/cjs/util.js +0 -607
- package/dist/esm/color.d.ts +0 -195
- package/dist/esm/color.js +0 -500
- package/dist/esm/eventCenter.d.ts +0 -59
- package/dist/esm/eventCenter.js +0 -134
- package/dist/esm/file.d.ts +0 -71
- package/dist/esm/file.js +0 -267
- package/dist/esm/index.d.ts +0 -11
- package/dist/esm/index.js +0 -7
- package/dist/esm/obj.d.ts +0 -12
- package/dist/esm/obj.js +0 -28
- package/dist/esm/storage.js +0 -130
- package/dist/esm/tree.js +0 -214
- package/dist/esm/util.d.ts +0 -221
- package/dist/esm/util.js +0 -744
- package/dist/umd/index.umd.js +0 -1
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
export interface RGB {
|
|
2
|
+
r: number;
|
|
3
|
+
g: number;
|
|
4
|
+
b: number;
|
|
5
|
+
a?: number;
|
|
6
|
+
}
|
|
7
|
+
export interface HSV {
|
|
8
|
+
h: number;
|
|
9
|
+
s: number;
|
|
10
|
+
v: number;
|
|
11
|
+
a?: number;
|
|
12
|
+
}
|
|
13
|
+
export type CrColorAsType = 'string' | 'object';
|
|
14
|
+
export type CrColorInputType = 'hex' | 'rgb' | 'hsv' | 'object';
|
|
15
|
+
export type CrColorOutputType<T extends 'string' | 'object' | undefined, R> = T extends 'object' ? R : string;
|
|
16
|
+
export declare class CrColorUtil {
|
|
17
|
+
private static readonly HEX_COLOR_REGEXP;
|
|
18
|
+
private static readonly HEX_VALUE_REGEXP;
|
|
19
|
+
private static readonly RGB_FUNCTION_REGEXP;
|
|
20
|
+
private static readonly HSV_FUNCTION_REGEXP;
|
|
21
|
+
/**
|
|
22
|
+
* 判断是否为合法的 HEX / HEXA 颜色值
|
|
23
|
+
* @param hex 必须 # 开头
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
static isHexColor(hex: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* 判断是否为合法的 RGB / RGBA 颜色值
|
|
29
|
+
* @param color
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
static isRgbColor(color: string): boolean;
|
|
33
|
+
static isHsvColor(color: string): boolean;
|
|
34
|
+
private static clamp;
|
|
35
|
+
private static clampByte;
|
|
36
|
+
private static clampAlpha;
|
|
37
|
+
private static toFixedNumber;
|
|
38
|
+
private static toHexByte;
|
|
39
|
+
private static normalizeHue;
|
|
40
|
+
private static normalizeHex;
|
|
41
|
+
private static isRgbLike;
|
|
42
|
+
private static isHsvLike;
|
|
43
|
+
private static normalizeRgb;
|
|
44
|
+
private static normalizeHsv;
|
|
45
|
+
private static rgbToHsv;
|
|
46
|
+
private static hsvToRgb;
|
|
47
|
+
private static splitColorFunctionArgs;
|
|
48
|
+
private static parseRgbChannel;
|
|
49
|
+
private static parsePercentChannel;
|
|
50
|
+
private static parseAlpha;
|
|
51
|
+
static parse(input: string | RGB | HSV): RGB | null;
|
|
52
|
+
static parseHex(hex: string): RGB | null;
|
|
53
|
+
static parseRgb(str: string): RGB | null;
|
|
54
|
+
static parseHsv(str: string): RGB | null;
|
|
55
|
+
static toRgb<T extends 'string' | 'object' | undefined = 'string'>(input: string | RGB | HSV, as?: T): CrColorOutputType<T, RGB> | null;
|
|
56
|
+
static toHex<T extends 'string' | 'object' | undefined = 'string'>(input: string | RGB | HSV, as?: T): CrColorOutputType<T, RGB> | null;
|
|
57
|
+
static toHsv<T extends 'string' | 'object' | undefined = 'string'>(input: string | RGB | HSV, as?: T): CrColorOutputType<T, HSV> | null;
|
|
58
|
+
/**
|
|
59
|
+
* 转换为 ARGB 格式(Android / Windows 标准:0xAARRGGBB)
|
|
60
|
+
* @param input 任意颜色
|
|
61
|
+
* @param as 'string' | 'number' 输出 16进制字符串 或 整型数字
|
|
62
|
+
* @returns 0xAARRGGBB 格式
|
|
63
|
+
*/
|
|
64
|
+
static toArgb<T extends 'string' | 'number' = 'string'>(input: string | RGB | HSV, as?: T): (T extends 'string' ? string : number) | null;
|
|
65
|
+
/**
|
|
66
|
+
* 内部增强解析:获取颜色对象 + 原始来源类型
|
|
67
|
+
*/
|
|
68
|
+
static parseWithSource(input: string | RGB | HSV): {
|
|
69
|
+
rgb: RGB | null;
|
|
70
|
+
sourceType: CrColorInputType;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* 根据来源类型自动格式化输出
|
|
74
|
+
*/
|
|
75
|
+
static formatOutputBySource<T extends 'string' | 'object' | undefined>(rgb: RGB, sourceType: CrColorInputType, as?: T): CrColorOutputType<T, RGB | HSV> | null;
|
|
76
|
+
/**
|
|
77
|
+
* 设置 / 追加透明度
|
|
78
|
+
* @param input
|
|
79
|
+
* @param alpha
|
|
80
|
+
* @param as
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
static setAlpha<T extends 'string' | 'object' | undefined = 'string'>(input: string | RGB | HSV, alpha: number, as?: T): CrColorOutputType<T, RGB | HSV> | null;
|
|
84
|
+
/**
|
|
85
|
+
* 移除透明度
|
|
86
|
+
* @param input
|
|
87
|
+
* @param as
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
static removeAlpha<T extends 'string' | 'object' | undefined = 'string'>(input: string | RGB | HSV, as?: T): CrColorOutputType<T, RGB | HSV> | null;
|
|
91
|
+
/**
|
|
92
|
+
* 颜色加深处理
|
|
93
|
+
* @param input
|
|
94
|
+
* @param level 加深程度 0-1 0不变 1最深(接近黑色)
|
|
95
|
+
*/
|
|
96
|
+
static darkenColor(input: string, level: number): string | null;
|
|
97
|
+
/**
|
|
98
|
+
* 颜色减淡/提亮处理
|
|
99
|
+
* @param input
|
|
100
|
+
* @param level 减淡程度 0-1 0不变 1最亮(接近白色)
|
|
101
|
+
*/
|
|
102
|
+
static lightenColor(input: string, level: number): string | null;
|
|
103
|
+
/**
|
|
104
|
+
* 调整颜色饱和度
|
|
105
|
+
* @param hex 目标颜色
|
|
106
|
+
* @param level 调整程度 0-2 1=不变 <1=降低饱和度 >1=提高饱和度
|
|
107
|
+
* @returns 调整后的hex颜色
|
|
108
|
+
*/
|
|
109
|
+
static adjustSaturation(hex: string, level: number): string | null;
|
|
110
|
+
/**
|
|
111
|
+
* 调整颜色明度
|
|
112
|
+
* @param hex 目标颜色
|
|
113
|
+
* @param level 调整程度 0-2 1=不变 <1=降低明度 >1=提高明度
|
|
114
|
+
* @returns 调整后的hex颜色
|
|
115
|
+
*/
|
|
116
|
+
static adjustBrightness(hex: string, level: number): string | null;
|
|
117
|
+
/**
|
|
118
|
+
* 反色处理 - 取颜色的补色(比如黑色→白色,白色→黑色)
|
|
119
|
+
* @param hex 目标颜色
|
|
120
|
+
* @returns 反色hex
|
|
121
|
+
*/
|
|
122
|
+
static reverseColor(hex: string): string | null;
|
|
123
|
+
/**
|
|
124
|
+
* 生成随机Hex颜色
|
|
125
|
+
* @param withHash 是否带 # 前缀,默认 true
|
|
126
|
+
* @returns 随机hex颜色
|
|
127
|
+
*/
|
|
128
|
+
static randomHexColor(withHash?: boolean): string;
|
|
129
|
+
private static parseLegacyHex;
|
|
130
|
+
/**
|
|
131
|
+
* hex 转 rgb 数组 [r,g,b] r/g/b范围 0-255
|
|
132
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
133
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toRgb} 替代
|
|
134
|
+
*/
|
|
135
|
+
static hexToRgb(hex: string): number[];
|
|
136
|
+
/**
|
|
137
|
+
* rgb 转 hex 标准格式
|
|
138
|
+
* @param r 0-255
|
|
139
|
+
* @param g 0-255
|
|
140
|
+
* @param b 0-255
|
|
141
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toHex} 替代
|
|
142
|
+
*/
|
|
143
|
+
static rgbToHex(r: number, g: number, b: number): string;
|
|
144
|
+
/**
|
|
145
|
+
* 将 rgb 格式转为 hsv 格式
|
|
146
|
+
* input: r,g,b in [0,1], out: h in [0,360) and s,v in [0,1]
|
|
147
|
+
* @param r 0-1
|
|
148
|
+
* @param g 0-1
|
|
149
|
+
* @param b 0-1
|
|
150
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toHsv} 替代
|
|
151
|
+
*/
|
|
152
|
+
static rgb2hsv(r: number, g: number, b: number): number[];
|
|
153
|
+
/**
|
|
154
|
+
* 将 hsv 格式转为 rgb 格式
|
|
155
|
+
* input: h in [0,360] and s,v in [0,1] - output: r,g,b in [0,1]
|
|
156
|
+
* @param h 0-360
|
|
157
|
+
* @param s 0-1
|
|
158
|
+
* @param v 0-1
|
|
159
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toRgb} 替代
|
|
160
|
+
*/
|
|
161
|
+
static hsv2rgb(h: number, s: number, v: number): number[];
|
|
162
|
+
/**
|
|
163
|
+
* hex 转 rgba 格式字符串 可直接用于css
|
|
164
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
165
|
+
* @param alpha 透明度 0-1 默认1(完全不透明)
|
|
166
|
+
* @returns 例: rgba(255,255,255,0.5)
|
|
167
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.setAlpha} 替代
|
|
168
|
+
*/
|
|
169
|
+
static hexToRgba(hex: string, alpha?: number): string;
|
|
170
|
+
/**
|
|
171
|
+
* 给hex颜色添加透明度,返回 8位带透明通道的hex格式 #RRGGBBAA (CSS常用)
|
|
172
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
173
|
+
* @param alpha 透明度 0-1 默认1(完全不透明)
|
|
174
|
+
* @returns 例: #ffffff80 #ff000033
|
|
175
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.setAlpha} 替代
|
|
176
|
+
*/
|
|
177
|
+
static hexAddAlpha(hex: string, alpha?: number): string;
|
|
178
|
+
/**
|
|
179
|
+
* rgba 转 hex 格式
|
|
180
|
+
* @param r 0-255
|
|
181
|
+
* @param g 0-255
|
|
182
|
+
* @param b 0-255
|
|
183
|
+
* @param alpha 兼容旧调用,转换时会忽略透明度
|
|
184
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toHex} 替代
|
|
185
|
+
*/
|
|
186
|
+
static rgbaToHex(r: number, g: number, b: number, alpha?: number): string;
|
|
187
|
+
/**
|
|
188
|
+
* hsv 转 hex 十六进制颜色
|
|
189
|
+
* @param h 色相 0-360
|
|
190
|
+
* @param s 饱和度 0-1
|
|
191
|
+
* @param v 明度 0-1
|
|
192
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toHex} 替代
|
|
193
|
+
*/
|
|
194
|
+
static hsvToHex(h: number, s: number, v: number): string;
|
|
195
|
+
/**
|
|
196
|
+
* hex 转 hsv 格式
|
|
197
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
198
|
+
* @returns [h:0-360, s:0-1, v:0-1]
|
|
199
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toHsv} 替代
|
|
200
|
+
*/
|
|
201
|
+
static hexToHsv(hex: string): number[];
|
|
202
|
+
/**
|
|
203
|
+
* 解析8位带透明的hex #RRGGBBAA 为 普通hex + 透明度
|
|
204
|
+
* @param hex8 8位带透明的十六进制颜色 例:#ffffff80
|
|
205
|
+
* @returns { hex: '#ffffff', alpha: 0.5 }
|
|
206
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.parseHex} {@link CrColorUtil.parseRgb} 替代
|
|
207
|
+
*/
|
|
208
|
+
static parseHex8(hex8: string): {
|
|
209
|
+
hex: string;
|
|
210
|
+
alpha: number;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* rgb 数组 转 rgb/rgba 字符串
|
|
214
|
+
* @param rgb [r,g,b] 0-255
|
|
215
|
+
* @param alpha 透明度0-1 传则返回rgba,不传返回rgb
|
|
216
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toRgb} 替代
|
|
217
|
+
*/
|
|
218
|
+
static rgbToStr(rgb: number[], alpha?: number): string;
|
|
219
|
+
/**
|
|
220
|
+
* Hex颜色字符串 转 十进制数值(6位纯颜色值,无透明通道,专用于颜色运算)
|
|
221
|
+
* @param hex 支持 #fff / #ffffff / fff / ffffff 格式
|
|
222
|
+
* @returns 十进制数值 例:#ff0000 → 16711680,非法入参返回 0
|
|
223
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toHex} 替代
|
|
224
|
+
*/
|
|
225
|
+
static hexToNumber(hex: string): number;
|
|
226
|
+
/**
|
|
227
|
+
* 十进制数值 转回 Hex颜色字符串(无透明通道 标准6位)
|
|
228
|
+
* @param num 十进制数值 例:16711680 → #ff0000
|
|
229
|
+
* @returns 标准6位十六进制颜色 #RRGGBB 格式
|
|
230
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toRgb} 替代
|
|
231
|
+
*/
|
|
232
|
+
static numberToHex(num: number): string;
|
|
233
|
+
/**
|
|
234
|
+
* ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
|
|
235
|
+
* @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
|
|
236
|
+
* @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
|
|
237
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toArgb} 替代
|
|
238
|
+
*/
|
|
239
|
+
static argbToNumber(argb: string): number;
|
|
240
|
+
/**
|
|
241
|
+
* 十进制数值 转回 ARGB颜色字符串(8位带透明通道,标准#AARRGGBB格式)
|
|
242
|
+
* @param num 十进制数值 例:2147483647 → #80ffffff
|
|
243
|
+
* @returns 标准ARGB颜色字符串 #AARRGGBB 小写格式
|
|
244
|
+
* @deprecated 即将移除。生成 ARGB 请使用 {@link CrColorUtil.toArgb};
|
|
245
|
+
* 已有 ARGB number 转 #AARRGGBB 的场景暂无简洁等价新 API,暂保留该兼容方法。
|
|
246
|
+
*/
|
|
247
|
+
static numberToArgb(num: number): string;
|
|
248
|
+
/**
|
|
249
|
+
* 标准6位Hex颜色 转 ARGB 8位颜色格式 ( 核心:ARGB格式是 #AARRGGBB )
|
|
250
|
+
* @param hex 6位标准hex 支持 #ffffff / ffffff / #fff / fff
|
|
251
|
+
* @param alpha 透明度 0~1 默认 1 (完全不透明)
|
|
252
|
+
* @returns 8位ARGB格式颜色 例:#80FFFFFF 半透明白色
|
|
253
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.toArgb} 替代
|
|
254
|
+
*/
|
|
255
|
+
static hexToArgb(hex: string, alpha?: number): string;
|
|
256
|
+
/**
|
|
257
|
+
* ARGB 8位颜色格式 转回 标准6位Hex颜色 + 独立的透明度值 ( 反向解析 )
|
|
258
|
+
* @param argb 8位ARGB格式颜色 必须是 #AARRGGBB 格式 例:#80FFFFFF
|
|
259
|
+
* @returns { hex: '#ffffff', alpha: 0.5 } 标准hex + 0~1的透明度
|
|
260
|
+
* @deprecated 即将移除。可用 {@link CrColorUtil.argbToRgbaHex} + {@link CrColorUtil.parseHex}
|
|
261
|
+
* 组合替代;若需要旧返回结构 { hex, alpha },需自行从 parseHex 结果中拆出 a。
|
|
262
|
+
*/
|
|
263
|
+
static argbToHex(argb: string): {
|
|
264
|
+
hex: string;
|
|
265
|
+
alpha: number;
|
|
266
|
+
};
|
|
267
|
+
/**
|
|
268
|
+
* ARGB格式(#AARRGGBB) 转 RRGGBBAA格式(#RRGGBBAA)
|
|
269
|
+
* @param argb #AARRGGBB
|
|
270
|
+
* @returns #RRGGBBAA
|
|
271
|
+
* @deprecated 即将移除。纯新 API 暂无单步等价;如已拆出 { hex, alpha },
|
|
272
|
+
* 可用 {@link CrColorUtil.setAlpha} 生成 #RRGGBBAA。
|
|
273
|
+
*/
|
|
274
|
+
static argbToRgbaHex(argb: string): string;
|
|
275
|
+
/**
|
|
276
|
+
* RRGGBBAA格式(#RRGGBBAA) 转 ARGB格式(#AARRGGBB)
|
|
277
|
+
* @param rgbaHex #RRGGBBAA
|
|
278
|
+
* @returns #AARRGGBB
|
|
279
|
+
* @deprecated 即将移除。可用 {@link CrColorUtil.parseHex} + {@link CrColorUtil.toArgb}
|
|
280
|
+
* 组合替代;toArgb 返回 0xAARRGGBB,若需要旧格式需将 0x 前缀替换为 #。
|
|
281
|
+
*/
|
|
282
|
+
static rgbaHexToArgb(rgbaHex: string): string;
|
|
283
|
+
/**
|
|
284
|
+
* 普通6位Hex+透明度 → 直接返回ARGB对应的十进制数值
|
|
285
|
+
* @param hex 支持 #fff / #ffffff 格式
|
|
286
|
+
* @param alpha 透明度 0-1
|
|
287
|
+
* @returns ARGB十进制数值
|
|
288
|
+
* @deprecated 即将移除,使用 {@link CrColorUtil.setAlpha} + {@link CrColorUtil.toArgb}
|
|
289
|
+
* 替代:CrColorUtil.toArgb(CrColorUtil.setAlpha(hex, alpha, 'object')!, 'number')。
|
|
290
|
+
*/
|
|
291
|
+
static hexAlphaToArgbNumber(hex: string, alpha?: number): number;
|
|
292
|
+
/**
|
|
293
|
+
* ARGB十进制数值 → 解析出 普通Hex + 0-1透明度
|
|
294
|
+
* @param num ARGB十进制数值
|
|
295
|
+
* @returns { hex: '#ffffff', alpha: 0.5 }
|
|
296
|
+
* @deprecated 即将移除。纯新 API 暂无简洁等价;可先将 number 转成 #RRGGBBAA,
|
|
297
|
+
* 再用 {@link CrColorUtil.parseHex} 解析,若需要旧返回结构需自行组装 { hex, alpha }。
|
|
298
|
+
*/
|
|
299
|
+
static argbNumberToHexAlpha(num: number): {
|
|
300
|
+
hex: string;
|
|
301
|
+
alpha: number;
|
|
302
|
+
};
|
|
303
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface TSubscribers {
|
|
2
2
|
key: string;
|
|
3
3
|
listeners: Array<(...args: any) => void>;
|
|
4
|
-
}
|
|
5
|
-
export
|
|
4
|
+
}
|
|
5
|
+
export interface TSubscription {
|
|
6
6
|
key: string;
|
|
7
7
|
listener: (...args: any) => void;
|
|
8
8
|
remove: () => void;
|
|
9
|
-
}
|
|
10
|
-
export
|
|
9
|
+
}
|
|
10
|
+
export declare class CrEventCenter {
|
|
11
11
|
/**
|
|
12
12
|
* 储存订阅者
|
|
13
13
|
*/
|