css-color-parser-h 3.0.4 → 3.0.6
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/@types/CssColorParser.d.ts +219 -0
- package/@types/CssColorParserPlus.d.ts +186 -0
- package/@types/index.d.ts +4 -382
- package/example.html +3 -1
- package/lib/css-color-parser-h.cjs.js +936 -0
- package/lib/css-color-parser-h.esm.js +933 -0
- package/lib/css-color-parser-h.umd.js +1 -0
- package/package.json +34 -25
- package/readme.md +8 -2
- package/LICENSE +0 -21
- package/dist/css-color-parser-h.cjs +0 -2550
- package/dist/css-color-parser-h.esm.js +0 -2532
- package/dist/css-color-parser-h.esm.min.js +0 -1
- package/dist/css-color-parser-h.min.cjs +0 -1
- package/dist/css-color-parser-h.umd.js +0 -2559
- package/dist/css-color-parser-h.umd.min.js +0 -1
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
export interface ColorJson {
|
|
2
|
+
r: number;
|
|
3
|
+
g: number;
|
|
4
|
+
b: number;
|
|
5
|
+
a: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default class CssColorParser {
|
|
9
|
+
r: number;
|
|
10
|
+
g: number;
|
|
11
|
+
b: number;
|
|
12
|
+
a: number;
|
|
13
|
+
private _outColorPrecision;
|
|
14
|
+
private _outAlphaPrecision;
|
|
15
|
+
constructor(
|
|
16
|
+
red?: number | string,
|
|
17
|
+
green?: number | string,
|
|
18
|
+
blue?: number | string,
|
|
19
|
+
alpha?: number | string
|
|
20
|
+
);
|
|
21
|
+
/**
|
|
22
|
+
* @description: 设置CssColorParser实例输出的精度
|
|
23
|
+
* @param {number} colorPrecision 输出颜色的精度
|
|
24
|
+
* @param {number} outAlphaPrecision 输出透明度的精度
|
|
25
|
+
* @return {CssColorParser}
|
|
26
|
+
*/
|
|
27
|
+
setOutPrecision(
|
|
28
|
+
colorPrecision: number,
|
|
29
|
+
outAlphaPrecision: number
|
|
30
|
+
): CssColorParser;
|
|
31
|
+
/**
|
|
32
|
+
* 设置颜色
|
|
33
|
+
* @param red
|
|
34
|
+
* @param green
|
|
35
|
+
* @param blue
|
|
36
|
+
* @param alpha
|
|
37
|
+
* @example: this.setColor(255,255,255,1)
|
|
38
|
+
*/
|
|
39
|
+
setColor(
|
|
40
|
+
red?: number | string,
|
|
41
|
+
green?: number | string,
|
|
42
|
+
blue?: number | string,
|
|
43
|
+
alpha?: number | string
|
|
44
|
+
): CssColorParser;
|
|
45
|
+
/**
|
|
46
|
+
* @description: 设置透明度
|
|
47
|
+
* @param {number} alpha
|
|
48
|
+
* @return {CssColorParser}
|
|
49
|
+
*/
|
|
50
|
+
setAlpha(alpha?: number | string): CssColorParser;
|
|
51
|
+
/**
|
|
52
|
+
* @description: 设置红色值
|
|
53
|
+
* @param {number} red
|
|
54
|
+
* @return {CssColorParser}
|
|
55
|
+
*/
|
|
56
|
+
setRed(red?: number | string): CssColorParser;
|
|
57
|
+
/**
|
|
58
|
+
* @description: 设置绿色值
|
|
59
|
+
* @param {number} green
|
|
60
|
+
* @return {CssColorParser}
|
|
61
|
+
*/
|
|
62
|
+
setGreen(green?: number | string): CssColorParser;
|
|
63
|
+
/**
|
|
64
|
+
* @description: 设置蓝色值
|
|
65
|
+
* @param {number} blue
|
|
66
|
+
* @return {CssColorParser}
|
|
67
|
+
*/
|
|
68
|
+
setBlue(blue?: number | string): CssColorParser;
|
|
69
|
+
/**
|
|
70
|
+
* @description: 返回rgba格式的css字符串
|
|
71
|
+
* @return {string}
|
|
72
|
+
*/
|
|
73
|
+
toRGBA(): string;
|
|
74
|
+
/**
|
|
75
|
+
* @description: 返回字符串
|
|
76
|
+
* @return {string}
|
|
77
|
+
*/
|
|
78
|
+
toString(): string;
|
|
79
|
+
/**
|
|
80
|
+
* @description: 归一化
|
|
81
|
+
* @return {array}
|
|
82
|
+
*/
|
|
83
|
+
toNormalize(): [number, number, number, number];
|
|
84
|
+
/**
|
|
85
|
+
* @description: 返回16进制格式的css字符串
|
|
86
|
+
* @return {string}
|
|
87
|
+
*/
|
|
88
|
+
toHEX(): string;
|
|
89
|
+
/**
|
|
90
|
+
* @description: 返回rgba数组
|
|
91
|
+
* @return {array}
|
|
92
|
+
*/
|
|
93
|
+
toArray(): [number, number, number, number];
|
|
94
|
+
/**
|
|
95
|
+
* @description: 返回ColorJson
|
|
96
|
+
* @return {ColorJson}
|
|
97
|
+
*/
|
|
98
|
+
toJson(): ColorJson;
|
|
99
|
+
/**
|
|
100
|
+
* @description: 返回取反色后的新的实例
|
|
101
|
+
* @return {CssColorParser}
|
|
102
|
+
*/
|
|
103
|
+
toInvert(): CssColorParser;
|
|
104
|
+
/**
|
|
105
|
+
* @description: 拷贝
|
|
106
|
+
* @return {CssColorParser}
|
|
107
|
+
*/
|
|
108
|
+
clone(): CssColorParser;
|
|
109
|
+
/**
|
|
110
|
+
* @description: 比较两个解析对象的数据是否相等
|
|
111
|
+
* @param {string} color
|
|
112
|
+
* @return {boolean}
|
|
113
|
+
*/
|
|
114
|
+
equals(color: CssColorParser): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* @description: 反色
|
|
117
|
+
* @return {CssColorParser}
|
|
118
|
+
*/
|
|
119
|
+
setInvert(): CssColorParser;
|
|
120
|
+
/**
|
|
121
|
+
* @description: 乘以倍数
|
|
122
|
+
* @param {number} scalar
|
|
123
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
124
|
+
* @return {CssColorParser}
|
|
125
|
+
*/
|
|
126
|
+
multiplyByScalar(scalar: number, isSetAlpha?: boolean): CssColorParser;
|
|
127
|
+
/**
|
|
128
|
+
* @description: 除以倍数
|
|
129
|
+
* @param {number} scalar
|
|
130
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
131
|
+
* @return {CssColorParser}
|
|
132
|
+
*/
|
|
133
|
+
divideByScalar(scalar: number, isSetAlpha?: boolean): CssColorParser;
|
|
134
|
+
/**
|
|
135
|
+
* @description: 实例相加
|
|
136
|
+
* @param {CssColorParser} colorParser
|
|
137
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
138
|
+
* @return {CssColorParser}
|
|
139
|
+
*/
|
|
140
|
+
add(colorParser: CssColorParser, isSetAlpha?: boolean): CssColorParser;
|
|
141
|
+
/**
|
|
142
|
+
* @description: 实例相减
|
|
143
|
+
* @param {CssColorParser} colorParser
|
|
144
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
145
|
+
* @return {CssColorParser}
|
|
146
|
+
*/
|
|
147
|
+
subtract(colorParser: CssColorParser, isSetAlpha?: boolean): CssColorParser;
|
|
148
|
+
/**
|
|
149
|
+
* @description: 实例相乘
|
|
150
|
+
* @param {CssColorParser} colorParser
|
|
151
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
152
|
+
* @return {CssColorParser}
|
|
153
|
+
*/
|
|
154
|
+
multiply(colorParser: CssColorParser, isSetAlpha?: boolean): CssColorParser;
|
|
155
|
+
/**
|
|
156
|
+
* @description: 实例相除
|
|
157
|
+
* @param {CssColorParser} colorParser
|
|
158
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
159
|
+
* @return {CssColorParser}
|
|
160
|
+
*/
|
|
161
|
+
divide(colorParser: CssColorParser, isSetAlpha?: boolean): CssColorParser;
|
|
162
|
+
/**
|
|
163
|
+
* @description: 颜色RGB加上数字
|
|
164
|
+
* @param {number} num
|
|
165
|
+
* @return {CssColorParser}
|
|
166
|
+
*/
|
|
167
|
+
addNumberForRGB(num: number): CssColorParser;
|
|
168
|
+
/**
|
|
169
|
+
* @description: 透明度加上数字
|
|
170
|
+
* @param {number} num
|
|
171
|
+
* @return {CssColorParser}
|
|
172
|
+
*/
|
|
173
|
+
addNumberForAlpha(num: number): CssColorParser;
|
|
174
|
+
/**
|
|
175
|
+
* @description: 解析16进制颜色
|
|
176
|
+
* @param {string} v
|
|
177
|
+
* @return {CssColorParser}
|
|
178
|
+
* @example: CssColorParser.parseHEX('#FFF')
|
|
179
|
+
*/
|
|
180
|
+
static parseHEX(v: string): CssColorParser;
|
|
181
|
+
/**
|
|
182
|
+
* @description: 解析rgba、rgb颜色
|
|
183
|
+
* @param {string} v
|
|
184
|
+
* @return {CssColorParser}
|
|
185
|
+
* @example: CssColorParser.parseRGBA('rgba(255,255,255,1)')
|
|
186
|
+
*/
|
|
187
|
+
static parseRGBA(v: string): CssColorParser;
|
|
188
|
+
/**
|
|
189
|
+
* @description: 将ColorJson格式的json数据转换为解析对象
|
|
190
|
+
* @param {ColorJson} json
|
|
191
|
+
* @return {CssColorParser}
|
|
192
|
+
* @example: CssColorParser.fromJson({r: 255, g: 255, b: 255, a: 1})
|
|
193
|
+
*/
|
|
194
|
+
static fromJson(json: ColorJson): CssColorParser;
|
|
195
|
+
/**
|
|
196
|
+
* @description: 将RGBA数组转换为解析对象
|
|
197
|
+
* @param {Array} color
|
|
198
|
+
* @return {CssColorParser}
|
|
199
|
+
* @example: CssColorParser.fromArray([255,255,255,1])
|
|
200
|
+
*/
|
|
201
|
+
static fromArray(color: Array<number>): CssColorParser;
|
|
202
|
+
/**
|
|
203
|
+
* @description: 产生随机颜色
|
|
204
|
+
* @return {CssColorParser}
|
|
205
|
+
* @example: CssColorParser.fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
|
|
206
|
+
*/
|
|
207
|
+
static fromRandom(
|
|
208
|
+
color1: CssColorParser,
|
|
209
|
+
color2: CssColorParser
|
|
210
|
+
): CssColorParser;
|
|
211
|
+
/**
|
|
212
|
+
* @description: 颜色序列化数组转换为CssColorParser对象实例
|
|
213
|
+
* @param {array} colorArr
|
|
214
|
+
* @example: CssColorParser.fromNormaliz([1, 0, 0, 1])
|
|
215
|
+
*/
|
|
216
|
+
static fromNormalize(
|
|
217
|
+
colorArr: [number, number, number, number]
|
|
218
|
+
): CssColorParser;
|
|
219
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import CssColorParser from "./CssColorParser";
|
|
2
|
+
export interface ColorJson {
|
|
3
|
+
r: number;
|
|
4
|
+
g: number;
|
|
5
|
+
b: number;
|
|
6
|
+
a: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ColorLerpOpt {
|
|
9
|
+
[key: number]: string | CssColorParser;
|
|
10
|
+
}
|
|
11
|
+
export default class CssColorParserPlus extends CssColorParser {
|
|
12
|
+
/**
|
|
13
|
+
* @description: 返回取反色后的新的实例
|
|
14
|
+
* @return {CssColorParserPlus}
|
|
15
|
+
*/
|
|
16
|
+
toInvert(): CssColorParserPlus;
|
|
17
|
+
/**
|
|
18
|
+
* @description: 拷贝
|
|
19
|
+
* @return {CssColorParserPlus}
|
|
20
|
+
*/
|
|
21
|
+
clone(): CssColorParserPlus;
|
|
22
|
+
/**
|
|
23
|
+
* @description: 比较两个解析对象的数据是否相等
|
|
24
|
+
* @param {string} color
|
|
25
|
+
* @return {boolean}
|
|
26
|
+
*/
|
|
27
|
+
equals(color: CssColorParserPlus | string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* @description: 实例相加
|
|
30
|
+
* @param {CssColorParserPlus} colorParser
|
|
31
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
32
|
+
* @return {CssColorParserPlus}
|
|
33
|
+
*/
|
|
34
|
+
add(
|
|
35
|
+
color: CssColorParserPlus | string,
|
|
36
|
+
isSetAlpha?: boolean
|
|
37
|
+
): CssColorParserPlus;
|
|
38
|
+
/**
|
|
39
|
+
* @description: 实例相减
|
|
40
|
+
* @param {CssColorParserPlus} colorParser
|
|
41
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
42
|
+
* @return {CssColorParserPlus}
|
|
43
|
+
*/
|
|
44
|
+
subtract(
|
|
45
|
+
color: CssColorParserPlus | string,
|
|
46
|
+
isSetAlpha?: boolean
|
|
47
|
+
): CssColorParserPlus;
|
|
48
|
+
/**
|
|
49
|
+
* @description: 实例相乘
|
|
50
|
+
* @param {CssColorParserPlus} colorParser
|
|
51
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
52
|
+
* @return {CssColorParserPlus}
|
|
53
|
+
*/
|
|
54
|
+
multiply(
|
|
55
|
+
color: CssColorParserPlus | string,
|
|
56
|
+
isSetAlpha?: boolean
|
|
57
|
+
): CssColorParserPlus;
|
|
58
|
+
/**
|
|
59
|
+
* @description: 实例相除
|
|
60
|
+
* @param {CssColorParserPlus} colorParser
|
|
61
|
+
* @param {boolean} isSetAlpha 透明度值是否参与计算(默认:是)
|
|
62
|
+
* @return {CssColorParserPlus}
|
|
63
|
+
*/
|
|
64
|
+
divide(
|
|
65
|
+
color: CssColorParserPlus | string,
|
|
66
|
+
isSetAlpha?: boolean
|
|
67
|
+
): CssColorParserPlus;
|
|
68
|
+
/**
|
|
69
|
+
* @description: 解析css颜色
|
|
70
|
+
* @param {string} v
|
|
71
|
+
* @return {CssColorParserPlus}
|
|
72
|
+
* @example: parseCssColorStr('rgba(255,255,255,1)')
|
|
73
|
+
*/
|
|
74
|
+
static parseColor(v: string | CssColorParserPlus): CssColorParserPlus;
|
|
75
|
+
/**
|
|
76
|
+
* @description: 将css字符串转换为解析对象
|
|
77
|
+
* @param {string} v
|
|
78
|
+
* @return {CssColorParserPlus}
|
|
79
|
+
* @example: parseCssColorStr('rgba(255,255,255,1)')
|
|
80
|
+
*/
|
|
81
|
+
static parseCssColorStr(v: string): CssColorParserPlus;
|
|
82
|
+
/**
|
|
83
|
+
* @description: 解析颜色关键字
|
|
84
|
+
* @param {string} v
|
|
85
|
+
* @return {CssColorParserPlus}
|
|
86
|
+
* @example: parseKeyWord('red')
|
|
87
|
+
*/
|
|
88
|
+
static parseKeyWord(v: string): CssColorParserPlus;
|
|
89
|
+
/**
|
|
90
|
+
* @description: 解析HSLA
|
|
91
|
+
* @param {string} v
|
|
92
|
+
* @return {CssColorParserPlus}
|
|
93
|
+
* @example: parseHSLA('hsla(215,85%,62%,0.8)')
|
|
94
|
+
*/
|
|
95
|
+
static parseHSLA(v: string): CssColorParserPlus;
|
|
96
|
+
/**
|
|
97
|
+
* @description: 解析HWB
|
|
98
|
+
* @param {string} v
|
|
99
|
+
* @return {CssColorParserPlus}
|
|
100
|
+
* @example: parseHWB('hwb(215deg 30% 6% / 80%)')
|
|
101
|
+
*/
|
|
102
|
+
static parseHWB(v: string): CssColorParserPlus;
|
|
103
|
+
/**
|
|
104
|
+
* @description: 将HSL色彩模式转换为解析对象
|
|
105
|
+
* @param {number} hue 色相
|
|
106
|
+
* @param {number} saturation 饱和度
|
|
107
|
+
* @param {number} lightness 亮度
|
|
108
|
+
* @param {number} alpha 不透明度
|
|
109
|
+
* @return {CssColorParserPlus}
|
|
110
|
+
* @example: fromHSL(0,1,1,1)
|
|
111
|
+
*/
|
|
112
|
+
static fromHSL(
|
|
113
|
+
h: number,
|
|
114
|
+
s: number,
|
|
115
|
+
l: number,
|
|
116
|
+
a?: number
|
|
117
|
+
): CssColorParserPlus;
|
|
118
|
+
/**
|
|
119
|
+
* @description: 将HWB色彩模式转换为解析对象
|
|
120
|
+
* @param {number} h 色调
|
|
121
|
+
* @param {number} w 白度
|
|
122
|
+
* @param {number} b 黑度
|
|
123
|
+
* @param {number} a 不透明度
|
|
124
|
+
* @return {CssColorParserPlus}
|
|
125
|
+
* @example: fromHSL(0,1,1,1)
|
|
126
|
+
*/
|
|
127
|
+
static fromHWB(
|
|
128
|
+
h: number,
|
|
129
|
+
w: number,
|
|
130
|
+
b: number,
|
|
131
|
+
a?: number
|
|
132
|
+
): CssColorParserPlus;
|
|
133
|
+
/**
|
|
134
|
+
* @description: 解析16进制颜色
|
|
135
|
+
* @param {string} v
|
|
136
|
+
* @return {CssColorParserPlus}
|
|
137
|
+
* @example: CssColorParserPlus.parseHEX('#FFF')
|
|
138
|
+
*/
|
|
139
|
+
static parseHEX(v: string): CssColorParserPlus;
|
|
140
|
+
/**
|
|
141
|
+
* @description: 解析rgba、rgb颜色
|
|
142
|
+
* @param {string} v
|
|
143
|
+
* @return {CssColorParserPlus}
|
|
144
|
+
* @example: CssColorParserPlus.parseRGBA('rgba(255,255,255,1)')
|
|
145
|
+
*/
|
|
146
|
+
static parseRGBA(v: string): CssColorParserPlus;
|
|
147
|
+
/**
|
|
148
|
+
* @description: 将ColorJson格式的json数据转换为解析对象
|
|
149
|
+
* @param {ColorJson} json
|
|
150
|
+
* @return {CssColorParserPlus}
|
|
151
|
+
* @example: CssColorParserPlus.fromJson({r: 255, g: 255, b: 255, a: 1})
|
|
152
|
+
*/
|
|
153
|
+
static fromJson(json: ColorJson): CssColorParserPlus;
|
|
154
|
+
/**
|
|
155
|
+
* @description: 将RGBA数组转换为解析对象
|
|
156
|
+
* @param {Array} color
|
|
157
|
+
* @return {CssColorParserPlus}
|
|
158
|
+
* @example: CssColorParserPlus.fromArray([255,255,255,1])
|
|
159
|
+
*/
|
|
160
|
+
static fromArray(color: Array<number>): CssColorParserPlus;
|
|
161
|
+
/**
|
|
162
|
+
* @description: 产生随机颜色
|
|
163
|
+
* @return {CssColorParserPlus}
|
|
164
|
+
* @example: CssColorParserPlus.fromRandom('black', new CssColorParserPlus(255,255,255,1))
|
|
165
|
+
*/
|
|
166
|
+
static fromRandom(
|
|
167
|
+
color1: CssColorParserPlus | string,
|
|
168
|
+
color2: CssColorParserPlus | string
|
|
169
|
+
): CssColorParserPlus;
|
|
170
|
+
/**
|
|
171
|
+
* @description: 颜色序列化数组转换为CssColorParserPlus对象实例
|
|
172
|
+
* @param {array} colorArr
|
|
173
|
+
* @example: CssColorParserPlus.fromNormaliz([1, 0, 0, 1])
|
|
174
|
+
*/
|
|
175
|
+
static fromNormalize(
|
|
176
|
+
colorArr: [number, number, number, number]
|
|
177
|
+
): CssColorParserPlus;
|
|
178
|
+
/**
|
|
179
|
+
* @description: 获取颜色插值
|
|
180
|
+
* @param opt ColorLerpOpt
|
|
181
|
+
* @param value number
|
|
182
|
+
* @returns {CssColorParserPlus}
|
|
183
|
+
* @example: CssColorParserPlus.lerp({0: "rgba(255, 255, 0, 1)",0.5: "rgba(0, 255, 0, 1)",1: "rgba(255, 0, 0, 1)"}, 0.8)
|
|
184
|
+
*/
|
|
185
|
+
static lerp(opt: ColorLerpOpt, value: number): CssColorParserPlus
|
|
186
|
+
}
|