css-color-parser-h 1.0.5 → 2.0.1
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/css-color-parser-h.d.ts +201 -0
- package/@types/index.d.ts +10 -0
- package/dist/css-color-parser-h.common.js +567 -0
- package/dist/css-color-parser-h.umd.js +2167 -0
- package/dist/{css-color-parser.min.js → css-color-parser-h.umd.min.js} +1 -1
- package/example.html +21 -21
- package/package.json +14 -3
- package/readme.md +45 -42
- package/.eslintrc.js +0 -36
- package/.prettierrc.js +0 -7
- package/dist/css-color-parser.min.d.ts +0 -123
- package/release.js +0 -19
- package/src/main.ts +0 -307
- package/src/utils/color-tools.ts +0 -146
- package/src/utils/common.ts +0 -40
- package/tsconfig.json +0 -50
- package/webpack.config.js +0 -64
package/src/main.ts
DELETED
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Descripttion: 颜色解析器
|
|
3
|
-
* @version: 1.0.0
|
|
4
|
-
* @Author: roman_123
|
|
5
|
-
* @Date: 2021-01-19 09:22:11
|
|
6
|
-
* @LastEditors: Please set LastEditors
|
|
7
|
-
* @LastEditTime: 2022-12-06 19:43:45
|
|
8
|
-
*/
|
|
9
|
-
import { CssColorStringParser } from './utils/color-tools'
|
|
10
|
-
import { Check, defaultValue, limitNumber } from './utils/common'
|
|
11
|
-
const convert = require('color-convert')
|
|
12
|
-
export class CssColorParser {
|
|
13
|
-
r = 255
|
|
14
|
-
g = 255
|
|
15
|
-
b = 255
|
|
16
|
-
a = 1
|
|
17
|
-
constructor(
|
|
18
|
-
red?: number | string,
|
|
19
|
-
green?: number | string,
|
|
20
|
-
blue?: number | string,
|
|
21
|
-
alpha?: number | string
|
|
22
|
-
) {
|
|
23
|
-
this.setColor(red, green, blue, alpha)
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* 设置颜色
|
|
27
|
-
* @param red
|
|
28
|
-
* @param green
|
|
29
|
-
* @param blue
|
|
30
|
-
* @param alpha
|
|
31
|
-
* @example: this.setColor(255,255,255,1)
|
|
32
|
-
*/
|
|
33
|
-
setColor(
|
|
34
|
-
red?: number | string,
|
|
35
|
-
green?: number | string,
|
|
36
|
-
blue?: number | string,
|
|
37
|
-
alpha?: number | string
|
|
38
|
-
) {
|
|
39
|
-
this.r = limitNumber(0, 255, defaultValue(Number(red), 0))
|
|
40
|
-
this.g = limitNumber(0, 255, defaultValue(Number(green), 0))
|
|
41
|
-
this.b = limitNumber(0, 255, defaultValue(Number(blue), 0))
|
|
42
|
-
this.a = limitNumber(0, 1, defaultValue(Number(alpha), 1))
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
static parseKeyWord(v: string): CssColorParser {
|
|
46
|
-
const cssStr = CssColorStringParser.clearStrSpace(v)
|
|
47
|
-
const res = convert.keyword.rgb(cssStr)
|
|
48
|
-
return res && CssColorParser.fromArray(res)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
static parseHEX(v: string): CssColorParser {
|
|
52
|
-
const cssStr = CssColorStringParser.clearStrSpace(v)
|
|
53
|
-
let res = CssColorStringParser.parse3BitsHEX(cssStr)
|
|
54
|
-
if (!res) {
|
|
55
|
-
res = CssColorStringParser.parse6BitsHEX(cssStr)
|
|
56
|
-
}
|
|
57
|
-
return res && CssColorParser.fromArray(res)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
static parseRGBA(v: string): CssColorParser {
|
|
61
|
-
const cssStr = CssColorStringParser.clearStrSpace(v)
|
|
62
|
-
let res = CssColorStringParser.parseRGBA(cssStr)
|
|
63
|
-
if (!res) {
|
|
64
|
-
const cssStr2 = CssColorStringParser.trimStr(v)
|
|
65
|
-
res = CssColorStringParser.parseRGBA2(cssStr2)
|
|
66
|
-
}
|
|
67
|
-
return res && CssColorParser.fromArray(res)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
static parseHSLA(v: string): CssColorParser {
|
|
71
|
-
const cssStr = CssColorStringParser.clearStrSpace(v)
|
|
72
|
-
let res = CssColorStringParser.parseHSLA(cssStr)
|
|
73
|
-
if (!res) {
|
|
74
|
-
const cssStr2 = CssColorStringParser.trimStr(v)
|
|
75
|
-
res = CssColorStringParser.parseHSLA2(cssStr2)
|
|
76
|
-
}
|
|
77
|
-
return res && CssColorParser.fromHSL(
|
|
78
|
-
res[0],
|
|
79
|
-
res[1],
|
|
80
|
-
res[2],
|
|
81
|
-
res[3]
|
|
82
|
-
)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
static parseHWB(v: string) {
|
|
86
|
-
const cssStr2 = CssColorStringParser.trimStr(v)
|
|
87
|
-
const res = CssColorStringParser.parseHWB(cssStr2)
|
|
88
|
-
return res && CssColorParser.fromHWB(res[0], res[1], res[2], res[3])
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* @description: 将css字符串转换为解析对象
|
|
94
|
-
* @param {string} v
|
|
95
|
-
* @return {CssColorParser}
|
|
96
|
-
* @example: CssColorParser.fromColorStr('rgba(255,255,255,1)')
|
|
97
|
-
*/
|
|
98
|
-
static fromColorStr(v: string): CssColorParser {
|
|
99
|
-
Check.type('string', 'color', v)
|
|
100
|
-
return CssColorParser.parseHEX(v) || CssColorParser.parseRGBA(v) || CssColorParser.parseKeyWord(v) || CssColorParser.parseHSLA(v) || CssColorParser.parseHWB(v)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @description: 将HSL色彩模式转换为解析对象
|
|
105
|
-
* @param {number} hue 色相
|
|
106
|
-
* @param {number} saturation 饱和度
|
|
107
|
-
* @param {number} lightness 亮度
|
|
108
|
-
* @param {number} alpha 不透明度
|
|
109
|
-
* @return {CssColorParser}
|
|
110
|
-
* @example: CssColorParser.fromHSL(0,1,1,1)
|
|
111
|
-
*/
|
|
112
|
-
static fromHSL(h: number, s: number, l: number, a?: number): CssColorParser {
|
|
113
|
-
const res = convert.hsl.rgb(limitNumber(0, 360, h), limitNumber(0, 100, s * 100), limitNumber(0, 100, l * 100))
|
|
114
|
-
return new CssColorParser(
|
|
115
|
-
res[0],
|
|
116
|
-
res[1],
|
|
117
|
-
res[2],
|
|
118
|
-
defaultValue(Number(a), 1)
|
|
119
|
-
)
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* @description: 将HWB色彩模式转换为解析对象
|
|
123
|
-
* @param {number} h 色调
|
|
124
|
-
* @param {number} w 白度
|
|
125
|
-
* @param {number} b 黑度
|
|
126
|
-
* @param {number} a 不透明度
|
|
127
|
-
* @return {CssColorParser}
|
|
128
|
-
* @example: CssColorParser.fromHSL(0,1,1,1)
|
|
129
|
-
*/
|
|
130
|
-
static fromHWB(h: number, w: number, b: number, a?: number) {
|
|
131
|
-
const res = convert.hwb.rgb(limitNumber(0, 360, h), limitNumber(0, 100, w * 100), limitNumber(0, 100, b * 100))
|
|
132
|
-
return new CssColorParser(
|
|
133
|
-
res[0],
|
|
134
|
-
res[1],
|
|
135
|
-
res[2],
|
|
136
|
-
defaultValue(Number(a), 1)
|
|
137
|
-
)
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* @description: 从解析器中产生随机颜色
|
|
141
|
-
* @return {CssColorParser}
|
|
142
|
-
* @author: roman_123
|
|
143
|
-
*/
|
|
144
|
-
static fromRandom(
|
|
145
|
-
color1: string | CssColorParser,
|
|
146
|
-
color2: string | CssColorParser
|
|
147
|
-
): CssColorParser {
|
|
148
|
-
if (typeof color1 === 'string') {
|
|
149
|
-
color1 = CssColorParser.fromColorStr(color1)
|
|
150
|
-
}
|
|
151
|
-
if (typeof color2 === 'string') {
|
|
152
|
-
color2 = CssColorParser.fromColorStr(color2)
|
|
153
|
-
}
|
|
154
|
-
if (!color1 || !color2) {
|
|
155
|
-
throw new Error('fail to create object from random')
|
|
156
|
-
}
|
|
157
|
-
const r =
|
|
158
|
-
Math.random() * Math.abs(color2.r - color1.r) +
|
|
159
|
-
Math.min(color1.r, color2.r)
|
|
160
|
-
const g =
|
|
161
|
-
Math.random() * Math.abs(color2.g - color1.g) +
|
|
162
|
-
Math.min(color1.g, color2.g)
|
|
163
|
-
const b =
|
|
164
|
-
Math.random() * Math.abs(color2.b - color1.b) +
|
|
165
|
-
Math.min(color1.b, color2.b)
|
|
166
|
-
const a =
|
|
167
|
-
Math.random() * Math.abs(color2.a - color1.a) +
|
|
168
|
-
Math.min(color1.a, color2.a)
|
|
169
|
-
return new CssColorParser(r, g, b, a)
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* @description: 将ColorJson格式的json数据转换为解析对象
|
|
173
|
-
* @param {ColorJson} json
|
|
174
|
-
* @return {CssColorParser}
|
|
175
|
-
* @author: roman_123
|
|
176
|
-
*/
|
|
177
|
-
static fromJson(json: ColorJson): CssColorParser {
|
|
178
|
-
return new CssColorParser(json.r, json.g, json.b, json.a)
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* @description: 将rgba数组转换为解析对象
|
|
182
|
-
* @param {Array} color
|
|
183
|
-
* @return {CssColorParser}
|
|
184
|
-
* @author: roman_123
|
|
185
|
-
*/
|
|
186
|
-
static fromArray(color: Array<number>): CssColorParser {
|
|
187
|
-
return new CssColorParser(color[0], color[1], color[2], color[3])
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* @description: 返回rgba格式的css字符串
|
|
191
|
-
* @return {*}
|
|
192
|
-
* @author: roman_123
|
|
193
|
-
*/
|
|
194
|
-
toRGBA(): string {
|
|
195
|
-
const color = this.toJson()
|
|
196
|
-
if (color.a === 1) {
|
|
197
|
-
return `rgb(${color.r},${color.g},${color.b})`
|
|
198
|
-
}
|
|
199
|
-
return `rgba(${color.r},${color.g},${color.b},${color.a})`
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* @description: 返回字符串
|
|
203
|
-
* @return {*}
|
|
204
|
-
* @author: roman_123
|
|
205
|
-
*/
|
|
206
|
-
toString(): string {
|
|
207
|
-
return this.toRGBA()
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* @description: 归一化
|
|
211
|
-
* @return {*}
|
|
212
|
-
* @author: roman_123
|
|
213
|
-
*/
|
|
214
|
-
toNormalize(): ColorJson {
|
|
215
|
-
return {
|
|
216
|
-
r: parseFloat((this.r / 255).toFixed(2)),
|
|
217
|
-
g: parseFloat((this.g / 255).toFixed(2)),
|
|
218
|
-
b: parseFloat((this.b / 255).toFixed(2)),
|
|
219
|
-
a: parseFloat(this.a.toFixed(2))
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* @description: 返回16进制格式的css字符串
|
|
224
|
-
* @return {*}
|
|
225
|
-
* @author: roman_123
|
|
226
|
-
*/
|
|
227
|
-
toHEX(): string {
|
|
228
|
-
const color = this.toJson()
|
|
229
|
-
let r = color.r.toString(16)
|
|
230
|
-
if (r.length < 2) {
|
|
231
|
-
r = `0${r}`
|
|
232
|
-
}
|
|
233
|
-
let g = color.g.toString(16)
|
|
234
|
-
if (g.length < 2) {
|
|
235
|
-
g = `0${g}`
|
|
236
|
-
}
|
|
237
|
-
let b = color.b.toString(16)
|
|
238
|
-
if (b.length < 2) {
|
|
239
|
-
b = `0${b}`
|
|
240
|
-
}
|
|
241
|
-
// 由于tojson后a会丢失精度,此处使用this.a
|
|
242
|
-
if (this.a < 1) {
|
|
243
|
-
let hexAlpha = parseInt((this.a * 255).toFixed()).toString(16)
|
|
244
|
-
if (hexAlpha.length < 2) {
|
|
245
|
-
hexAlpha = `0${hexAlpha}`
|
|
246
|
-
}
|
|
247
|
-
return `#${r}${g}${b}${hexAlpha}`
|
|
248
|
-
}
|
|
249
|
-
return `#${r}${g}${b}`
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* @description: 返回rgba数组
|
|
253
|
-
* @return {*}
|
|
254
|
-
* @author: roman_123
|
|
255
|
-
*/
|
|
256
|
-
toArray(): number[] {
|
|
257
|
-
const color = this.toJson()
|
|
258
|
-
return [color.r, color.g, color.b, color.a]
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* @description: 返回ColorJson
|
|
262
|
-
* @return {*}
|
|
263
|
-
* @author: roman_123
|
|
264
|
-
*/
|
|
265
|
-
toJson(): ColorJson {
|
|
266
|
-
return {
|
|
267
|
-
r: parseInt(this.r.toFixed()),
|
|
268
|
-
g: parseInt(this.g.toFixed()),
|
|
269
|
-
b: parseInt(this.b.toFixed()),
|
|
270
|
-
a: parseFloat(this.a.toFixed(2))
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* @description: 拷贝
|
|
275
|
-
* @return {*}
|
|
276
|
-
* @author: roman_123
|
|
277
|
-
*/
|
|
278
|
-
clone(): CssColorParser {
|
|
279
|
-
return new CssColorParser(this.r, this.g, this.b, this.a)
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* @description: 比较两个解析对象的数据是否相等
|
|
283
|
-
* @param {*} color
|
|
284
|
-
* @return {*}
|
|
285
|
-
* @author: roman_123
|
|
286
|
-
*/
|
|
287
|
-
equals(color): boolean {
|
|
288
|
-
if (this === color) {
|
|
289
|
-
return true
|
|
290
|
-
} else {
|
|
291
|
-
return (
|
|
292
|
-
this.r === color.r &&
|
|
293
|
-
this.g === color.g &&
|
|
294
|
-
this.b === color.g &&
|
|
295
|
-
this.a === color.a
|
|
296
|
-
)
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
export class ColorJson {
|
|
303
|
-
r: number
|
|
304
|
-
g: number
|
|
305
|
-
b: number
|
|
306
|
-
a: number
|
|
307
|
-
}
|
package/src/utils/color-tools.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { defaultValue, limitNumber } from "./common"
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* @Author: roman_123
|
|
5
|
-
* @Description: 部分核心代码修改自cesium
|
|
6
|
-
* @Date: 2022-11-28 17:32:57
|
|
7
|
-
* @LastEditTime: 2022-12-06 00:02:21
|
|
8
|
-
*/
|
|
9
|
-
export class CssColorStringParser {
|
|
10
|
-
// 去除字符串内所有空格
|
|
11
|
-
static clearStrSpace(v: string): string {
|
|
12
|
-
return v.replace(/\s/g, '')
|
|
13
|
-
}
|
|
14
|
-
// 去除字符串两边空格,将中间多个空格合并为一个
|
|
15
|
-
static trimStr(v: string): string {
|
|
16
|
-
v = v.replace(/\s+/g, ' ')
|
|
17
|
-
return v.trim()
|
|
18
|
-
}
|
|
19
|
-
// 解析3位16进制 #fff #fff0
|
|
20
|
-
static parse3BitsHEX(v: string): Array<number> {
|
|
21
|
-
const regx = /^#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i
|
|
22
|
-
const res = regx.exec(v)
|
|
23
|
-
if (res) {
|
|
24
|
-
const res4 = defaultValue(res[4], 'f')
|
|
25
|
-
const r = CssColorStringParser._parseResStrForRgb(parseInt(res[1] + res[1], 16))
|
|
26
|
-
const g = CssColorStringParser._parseResStrForRgb(parseInt(res[2] + res[2], 16))
|
|
27
|
-
const b = CssColorStringParser._parseResStrForRgb(parseInt(res[3] + res[3], 16))
|
|
28
|
-
const a = CssColorStringParser._parsePercent(parseInt(res4 + res4, 16) / 255)
|
|
29
|
-
return [r, g, b, a]
|
|
30
|
-
}
|
|
31
|
-
return null
|
|
32
|
-
}
|
|
33
|
-
// 解析6位16进制 #ffffff #ffffff00
|
|
34
|
-
static parse6BitsHEX(v: string): Array<number> {
|
|
35
|
-
const regx = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i
|
|
36
|
-
const res = regx.exec(v)
|
|
37
|
-
if (res) {
|
|
38
|
-
const res4 = defaultValue(res[4], 'ff')
|
|
39
|
-
const r = CssColorStringParser._parseResStrForRgb(parseInt(res[1], 16))
|
|
40
|
-
const g = CssColorStringParser._parseResStrForRgb(parseInt(res[2], 16))
|
|
41
|
-
const b = CssColorStringParser._parseResStrForRgb(parseInt(res[3], 16))
|
|
42
|
-
const a = CssColorStringParser._parsePercent(parseInt(res4, 16) / 255.0)
|
|
43
|
-
return [r, g, b, a]
|
|
44
|
-
}
|
|
45
|
-
return null
|
|
46
|
-
}
|
|
47
|
-
// 解析rgb rgba rgb(255,255,255) rgba(255,255,255,1) rgba(100%,100%,100%, 1)
|
|
48
|
-
static parseRGBA(v: string): Array<number> {
|
|
49
|
-
const regx = /^rgba?\(([0-9.]+%?),([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i
|
|
50
|
-
const res = regx.exec(v)
|
|
51
|
-
if (res) {
|
|
52
|
-
const r = CssColorStringParser._parseResStrForRgb(res[1])
|
|
53
|
-
const g = CssColorStringParser._parseResStrForRgb(res[2])
|
|
54
|
-
const b = CssColorStringParser._parseResStrForRgb(res[3])
|
|
55
|
-
const a = CssColorStringParser._parsePercent(res[4])
|
|
56
|
-
return [r,g,b,a]
|
|
57
|
-
}
|
|
58
|
-
return null
|
|
59
|
-
}
|
|
60
|
-
// 解析hsl hsla hsl(360,100%,100%) hsla(360,100%,100%,1)
|
|
61
|
-
static parseHSLA(v: string): Array<number> {
|
|
62
|
-
const regx = /^hsla?\(([0-9.]+)(?:deg)?,([0-9.]+%?),([0-9.]+%?)(?:,([0-9.]+%?))?\)$/i
|
|
63
|
-
const res = regx.exec(v)
|
|
64
|
-
if (res) {
|
|
65
|
-
const h = CssColorStringParser._parseResStrForHue(res[1])
|
|
66
|
-
const s = CssColorStringParser._parsePercent(res[2])
|
|
67
|
-
const l = CssColorStringParser._parsePercent(res[3])
|
|
68
|
-
const a = CssColorStringParser._parsePercent(res[4])
|
|
69
|
-
return [h,s,l,a]
|
|
70
|
-
}
|
|
71
|
-
return null
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// 解析hwb
|
|
75
|
-
static parseHWB(v: string): Array<number> {
|
|
76
|
-
const regx = /^hwb\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i
|
|
77
|
-
const res = regx.exec(v)
|
|
78
|
-
if (res) {
|
|
79
|
-
const h = CssColorStringParser._parseResStrForHue(res[1])
|
|
80
|
-
const w = CssColorStringParser._parsePercent(res[2])
|
|
81
|
-
const b = CssColorStringParser._parsePercent(res[3])
|
|
82
|
-
const a = CssColorStringParser._parsePercent(res[4])
|
|
83
|
-
return [h,w,b,a]
|
|
84
|
-
}
|
|
85
|
-
return null
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// 解析rgb rgb(178 57 57 / 44%) 字符串中存在空格,因此使用清除两侧空格的原始字符串
|
|
89
|
-
static parseRGBA2(v: string): Array<number> {
|
|
90
|
-
const regx = /^rgba?\s?\(\s?([0-9.]+%?)\s?([0-9.]+%?)\s?([0-9.]+%?)(?:\s?\/\s?([0-9.]+%?))?\s?\)$/i
|
|
91
|
-
const res = regx.exec(v)
|
|
92
|
-
if (res) {
|
|
93
|
-
const r = CssColorStringParser._parseResStrForRgb(res[1])
|
|
94
|
-
const g = CssColorStringParser._parseResStrForRgb(res[2])
|
|
95
|
-
const b = CssColorStringParser._parseResStrForRgb(res[3])
|
|
96
|
-
const a = CssColorStringParser._parsePercent(res[4])
|
|
97
|
-
return [r,g,b,a]
|
|
98
|
-
}
|
|
99
|
-
return null
|
|
100
|
-
}
|
|
101
|
-
// 解析hsl hsl(215 85% 62% / 1)
|
|
102
|
-
static parseHSLA2(v: string): Array<number> {
|
|
103
|
-
const regx = /^hsla?\s?\(\s?([0-9.]+)(?:deg)?\s([0-9.]+%?)\s([0-9.]+%?)\s?(?:\/\s?([0-9.]+%?))?\s?\)$/i
|
|
104
|
-
const res = regx.exec(v)
|
|
105
|
-
if (res) {
|
|
106
|
-
const h = CssColorStringParser._parseResStrForHue(res[1])
|
|
107
|
-
const s = CssColorStringParser._parsePercent(res[2])
|
|
108
|
-
const l = CssColorStringParser._parsePercent(res[3])
|
|
109
|
-
const a = CssColorStringParser._parsePercent(res[4])
|
|
110
|
-
return [h,s,l,a]
|
|
111
|
-
}
|
|
112
|
-
return null
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// 将结果解析为数字
|
|
116
|
-
static _parseResStrForRgb(v: string|number) {
|
|
117
|
-
if(typeof v === 'string') {
|
|
118
|
-
v = parseFloat(v) / ('%' === v.substr(-1) ? 100.0 / 255 : 1)
|
|
119
|
-
}
|
|
120
|
-
if(isNaN(v)) {
|
|
121
|
-
v = 1
|
|
122
|
-
}
|
|
123
|
-
return limitNumber(0, 255, v)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
static _parseResStrForHue(v: string|number) {
|
|
127
|
-
if(typeof v === 'string') {
|
|
128
|
-
v = parseFloat(v)
|
|
129
|
-
}
|
|
130
|
-
if(isNaN(v)) {
|
|
131
|
-
v = 0
|
|
132
|
-
}
|
|
133
|
-
return limitNumber(0, 360, v)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
static _parsePercent(v: string|number) {
|
|
137
|
-
if(typeof v === 'string') {
|
|
138
|
-
v = parseFloat(v) / ('%' === v.substr(-1) ? 100.0 : 1)
|
|
139
|
-
}
|
|
140
|
-
if(isNaN(v)) {
|
|
141
|
-
v = 1
|
|
142
|
-
}
|
|
143
|
-
return limitNumber(0, 1, v)
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
package/src/utils/common.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
export class Check {
|
|
2
|
-
static type(type, paramName, value) {
|
|
3
|
-
const valueType = typeof value
|
|
4
|
-
if (valueType !== type) {
|
|
5
|
-
throw new Error(
|
|
6
|
-
`Expected ${paramName} to be typeof ${type}, actual typeof was ${valueType}`
|
|
7
|
-
)
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
static types(types: string[], paramName, value) {
|
|
11
|
-
const valueType = typeof value
|
|
12
|
-
const isContained = types.includes(valueType)
|
|
13
|
-
if (!isContained) {
|
|
14
|
-
throw new Error(
|
|
15
|
-
`Expected ${paramName} to be typeof ${types.join(
|
|
16
|
-
'|'
|
|
17
|
-
)}, actual typeof was ${valueType}`
|
|
18
|
-
)
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function defaultValue(v, defaultV) {
|
|
24
|
-
if (v === undefined || v === null) {
|
|
25
|
-
return defaultV
|
|
26
|
-
}
|
|
27
|
-
if(isNaN(v) && typeof defaultV === 'number') {
|
|
28
|
-
return defaultV
|
|
29
|
-
}
|
|
30
|
-
return v
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function limitNumber(min: number, max: number, v: number) {
|
|
34
|
-
if (v > max) {
|
|
35
|
-
v = max
|
|
36
|
-
} else if (v < min) {
|
|
37
|
-
v = min
|
|
38
|
-
}
|
|
39
|
-
return v
|
|
40
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES5",
|
|
4
|
-
"module": "ES2015",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"pretty": true,
|
|
7
|
-
"sourceMap": true,
|
|
8
|
-
"outDir": "./dist",
|
|
9
|
-
"rootDir": "./src",
|
|
10
|
-
"importHelpers": true,
|
|
11
|
-
"strict": true,
|
|
12
|
-
"noImplicitAny": false,
|
|
13
|
-
"strictNullChecks": false,
|
|
14
|
-
"noImplicitThis": true,
|
|
15
|
-
"alwaysStrict": true,
|
|
16
|
-
"noUnusedLocals": true,
|
|
17
|
-
"noUnusedParameters": false,
|
|
18
|
-
"noImplicitReturns": true,
|
|
19
|
-
"noFallthroughCasesInSwitch": true,
|
|
20
|
-
"moduleResolution": "node",
|
|
21
|
-
"baseUrl": ".",
|
|
22
|
-
"allowSyntheticDefaultImports": true,
|
|
23
|
-
"experimentalDecorators": true,
|
|
24
|
-
"emitDecoratorMetadata": true,
|
|
25
|
-
"resolveJsonModule": true,
|
|
26
|
-
"esModuleInterop": true,
|
|
27
|
-
"lib": [
|
|
28
|
-
"es5",
|
|
29
|
-
"es6",
|
|
30
|
-
"dom",
|
|
31
|
-
"es2015.core",
|
|
32
|
-
"es2015.collection",
|
|
33
|
-
"es2015.generator",
|
|
34
|
-
"es2015.iterable",
|
|
35
|
-
"es2015.promise",
|
|
36
|
-
"es2015.proxy",
|
|
37
|
-
"es2015.reflect",
|
|
38
|
-
"es2015.symbol",
|
|
39
|
-
"es2015.symbol.wellknown",
|
|
40
|
-
"esnext.asynciterable",
|
|
41
|
-
"es2017"
|
|
42
|
-
],
|
|
43
|
-
"paths": {
|
|
44
|
-
"@/*": ["./src/*"]
|
|
45
|
-
},
|
|
46
|
-
"plugins": [],
|
|
47
|
-
},
|
|
48
|
-
"include": ["src/**/*.ts"],
|
|
49
|
-
"exclude": ["node_modules/**", "dist"]
|
|
50
|
-
}
|
package/webpack.config.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: roman_123_123
|
|
3
|
-
* @Description:
|
|
4
|
-
* @Date: 2022-11-29 19:41:35
|
|
5
|
-
* @LastEditTime: 2023-02-10 18:11:38
|
|
6
|
-
*/
|
|
7
|
-
const path = require('path')
|
|
8
|
-
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
|
|
9
|
-
|
|
10
|
-
module.exports = {
|
|
11
|
-
mode: 'production',
|
|
12
|
-
entry: './src/main.ts',
|
|
13
|
-
output: {
|
|
14
|
-
filename: 'css-color-parser.min.js',
|
|
15
|
-
path: path.resolve(__dirname, 'dist'),
|
|
16
|
-
globalObject: 'this',
|
|
17
|
-
library: {
|
|
18
|
-
name: 'Parser',
|
|
19
|
-
type: 'umd'
|
|
20
|
-
},
|
|
21
|
-
environment: {
|
|
22
|
-
// The environment supports arrow functions ('() => { ... }').
|
|
23
|
-
arrowFunction: false,
|
|
24
|
-
// The environment supports BigInt as literal (123n).
|
|
25
|
-
bigIntLiteral: false,
|
|
26
|
-
// The environment supports const and let for variable declarations.
|
|
27
|
-
const: false,
|
|
28
|
-
// The environment supports destructuring ('{ a, b } = obj').
|
|
29
|
-
destructuring: false,
|
|
30
|
-
// The environment supports an async import() function to import EcmaScript modules.
|
|
31
|
-
dynamicImport: false,
|
|
32
|
-
// The environment supports 'for of' iteration ('for (const x of array) { ... }').
|
|
33
|
-
forOf: false,
|
|
34
|
-
// The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
|
|
35
|
-
module: false,
|
|
36
|
-
// The environment supports optional chaining ('obj?.a' or 'obj?.()').
|
|
37
|
-
optionalChaining: false,
|
|
38
|
-
// The environment supports template literals.
|
|
39
|
-
templateLiteral: true
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
module: {
|
|
43
|
-
rules: [
|
|
44
|
-
{ test: /\.ts$/, exclude: /node_modules/, use: 'ts-loader' },
|
|
45
|
-
{
|
|
46
|
-
test: /\.m?js$/,
|
|
47
|
-
// exclude: /(node_modules|bower_components)/,
|
|
48
|
-
use: {
|
|
49
|
-
loader: 'babel-loader',
|
|
50
|
-
options: {
|
|
51
|
-
presets: ['@babel/preset-env']
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
]
|
|
56
|
-
},
|
|
57
|
-
resolve: {
|
|
58
|
-
extensions: ['.ts', '.js'],
|
|
59
|
-
alias: {
|
|
60
|
-
'@': path.resolve('src')
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
plugins: [new CleanWebpackPlugin()]
|
|
64
|
-
}
|