css-color-parser-h 2.0.3 → 2.0.4

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/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "css-color-parser-h",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "A tool for parsing css color",
5
5
  "main": "dist/css-color-parser-h.common.js",
6
- "types": "@types/css-color-parser-h.d.ts",
6
+ "types": "@types/index.d.ts",
7
7
  "scripts": {
8
- "build": "webpack && node release.js"
8
+ "build": "webpack && node release.js",
9
+ "test": "npm run build && jest",
10
+ "test-c": "jest --coverage"
9
11
  },
10
12
  "repository": {
11
13
  "type": "git",
@@ -35,8 +37,8 @@
35
37
  "@babel/core": "^7.20.12",
36
38
  "@babel/preset-env": "^7.20.2",
37
39
  "babel-loader": "^9.1.2",
38
- "clean-webpack-plugin": "^4.0.0",
39
40
  "fs-extra": "^11.1.0",
41
+ "jest": "^29.5.0",
40
42
  "ts-loader": "^9.4.1",
41
43
  "tslib": "^2.5.2",
42
44
  "typescript": "^4.9.3",
package/readme.md CHANGED
@@ -40,7 +40,53 @@ Parser.fromColorStr('#4c90f0cc')
40
40
  /**
41
41
  * 方法总览
42
42
  * */
43
- // Parser的函数
43
+ // CssColorParser类
44
+ // 实例方法
45
+ import { CssColorParser } from 'css-color-parser-h'
46
+ const colorParser = new CssColorParser()
47
+ // 设置输出值的精度
48
+ colorParser.setOutPrecision(colorPrecision: number, outAlphaPrecision: number): CssColorParser;
49
+ colorParser.setColor(red?: number | string,green?: number | string,blue?: number | string,alpha?: number | string): CssColorParser
50
+ colorParser.setAlpha(alpha?: number | string): CssColorParser
51
+ colorParser.setRed(red?: number | string): CssColorParser;
52
+ colorParser.setGreen(green?: number | string): CssColorParser;
53
+ colorParser.setBlue(blue?: number | string): CssColorParser;
54
+ // 设置反色
55
+ colorParser.setInvert(): CssColorParser;
56
+ colorParser.toRGBA(): string
57
+ colorParser.toHEX(): string
58
+ // 获取反色的值(输出一个新的实例)
59
+ colorParser.toInvert(): CssColorParser;
60
+ colorParser.toString(): string
61
+ colorParser.toNormalize(): ColorJson
62
+ colorParser.toArray(): number[]
63
+ colorParser.toJson(): ColorJson
64
+ // 颜色相加
65
+ colorParser.add(colorParser: CssColorParser, isSetAlpha?: boolean): CssColorParser;
66
+ // 颜色相减
67
+ colorParser.subtract(colorParser: CssColorParser, isSetAlpha?: boolean): CssColorParser;
68
+ // 颜色相乘
69
+ colorParser.multiply(colorParser: CssColorParser, isSetAlpha?: boolean): CssColorParser;
70
+ // 颜色相除
71
+ colorParser.divide(colorParser: CssColorParser, isSetAlpha?: boolean): CssColorParser;
72
+ // 颜色乘以一个数值
73
+ colorParser.multiplyByScalar(scalar: number, isSetAlpha?: boolean): CssColorParser;
74
+ // 颜色除以一个数值
75
+ colorParser.divideByScalar(scalar: number, isSetAlpha?: boolean): CssColorParser;
76
+ // 颜色RGB属性加上一个数值
77
+ colorParser.addNumberForRGB(num: number): CssColorParser;
78
+ // 颜色透明度属性加上一个数值
79
+ colorParser.addNumberForAlpha(num: number): CssColorParser;
80
+ colorParser.clone():CssColorParser
81
+ colorParser.equals(color: CssColorParser):boolean
82
+ // 静态方法
83
+ CssColorParser.parseHEX(v: string): CssColorParser
84
+ CssColorParser.parseRGBA(v: string): CssColorParser
85
+ CssColorParser.fromJson(json: ColorJson): CssColorParser
86
+ CssColorParser.fromArray(color: Array<number>): CssColorParser
87
+ CssColorParser.fromRandomfromRandom(color1: CssColorParser, color2: CssColorParser): CssColorParser
88
+ CssColorParser.fromNormalize(colorArr: [number, number, number, number]): CssColorParser;
89
+ // Parser中的其他函数
44
90
  import Parser from 'css-color-parser-h'
45
91
  Parser.parseKeyWord(v: string): CssColorParser
46
92
  Parser.parseHEX(v: string): CssColorParser
@@ -53,29 +99,21 @@ Parser.fromHWB(h: number, w: number, b: number, a?: number)
53
99
  Parser.fromRandom(color1: string | CssColorParser,color2: string | CssColorParser): CssColorParser
54
100
  Parser.fromJson(json: ColorJson): CssColorParser
55
101
  Parser.fromArray(color: Array<number>): CssColorParser
56
- // 实例方法
57
- import { CssColorParser } from 'css-color-parser-h'
58
- const parser = new CssColorParser()
59
- parser.setColor(red?: number | string,green?: number | string,blue?: number | string,alpha?: number | string)
60
- parser.toRGBA(): string
61
- parser.toHEX(): string
62
- parser.toString(): string
63
- parser.toNormalize(): ColorJson
64
- parser.toArray(): number[]
65
- parser.toJson(): ColorJson
66
- parser.clone():CssColorParser
67
- parser.equals(color):boolean
68
- // CssColorParser的静态函数
69
- CssColorParser.parseHEX(v: string): CssColorParser
70
- CssColorParser.parseRGBA(v: string): CssColorParser
71
- CssColorParser.fromJson(json: ColorJson): CssColorParser
72
- CssColorParser.fromArray(color: Array<number>): CssColorParser
73
- CssColorParser.fromRandomfromRandom(color1: CssColorParser, color2: CssColorParser): CssColorParser
74
102
  ```
75
103
 
76
104
  ## Example
77
105
 
78
106
  ```js
107
+ import { CssColorParser } from 'css-color-parser-h'
108
+
109
+ const colorParser = new CssColorParser(255,255,255,1)
110
+ colorParser.toHEX()
111
+ CssColorParser.parseHEX('#FFF')
112
+ CssColorParser.parseRGBA('rgba(255,255,255,1)')
113
+ CssColorParser.fromJson({r: 255, g: 255, b: 255, a: 1})
114
+ CssColorParser.fromArray([255,255,255,1])
115
+ CssColorParser.fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
116
+
79
117
  import Parser from 'css-color-parser-h'
80
118
 
81
119
  Parser.fromColorStr('blue') // CssColorParser { r: 0, g: 0, b: 255, a: 1 }
@@ -100,13 +138,4 @@ Parser.fromRandom(Parser.fromArray([205,205,100,0.5]), '#ccffbbaa').toRGBA() //
100
138
 
101
139
  // 归一化
102
140
  Parser.fromArray([100, 200, 0, 0.552]).toNormalize() // { r: 0.39, g: 0.78, b: 0, a: 0.55 }
103
-
104
- import { CssColorParser } from 'css-color-parser-h'
105
-
106
- CssColorParser.parseHEX('#FFF')
107
- CssColorParser.parseRGBA('rgba(255,255,255,1)')
108
- CssColorParser.fromJson({r: 255, g: 255, b: 255, a: 1})
109
- CssColorParser.fromArray([255,255,255,1])
110
- CssColorParser.fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
111
-
112
141
  ```
@@ -1,230 +0,0 @@
1
- /*
2
- * @Author: roman_123
3
- * @Description:
4
- * @Date: 2023-05-26 13:29:45
5
- * @LastEditTime: 2023-05-26 16:10:51
6
- */
7
- export class ColorJson {
8
- r: number
9
- g: number
10
- b: number
11
- a: number
12
- }
13
- declare module 'css-color-parser-h' {
14
- /**
15
- * @description: 解析颜色关键字
16
- * @param {string} v
17
- * @return {CssColorParser}
18
- * @example: parseKeyWord('red')
19
- */
20
- export function parseKeyWord(v: string): CssColorParser
21
- /**
22
- * @description: 解析16进制字符串
23
- * @param {string} v
24
- * @return {CssColorParser}
25
- * @example: parseHEX('#FFF')
26
- */
27
- export function parseHEX(v: string): CssColorParser
28
- /**
29
- * @description: 解析RGBA
30
- * @param {string} v
31
- * @return {CssColorParser}
32
- * @example: parseRGBA('rgba(255,255,255,1)')
33
- */
34
- export function parseRGBA(v: string): CssColorParser
35
- /**
36
- * @description: 解析HSLA
37
- * @param {string} v
38
- * @return {CssColorParser}
39
- * @example: parseHSLA('hsla(215,85%,62%,0.8)')
40
- */
41
- export function parseHSLA(v: string): CssColorParser
42
- /**
43
- * @description: 解析HWB
44
- * @param {string} v
45
- * @return {CssColorParser}
46
- * @example: parseHWB('hwb(215deg 30% 6% / 80%)')
47
- */
48
- export function parseHWB(v: string): CssColorParser
49
- /**
50
- * @description: 将css字符串转换为解析对象
51
- * @param {string} v
52
- * @return {CssColorParser}
53
- * @example: parseCssColorStr('rgba(255,255,255,1)')
54
- */
55
- export function parseCssColorStr(v: string): CssColorParser
56
- /**
57
- * **Deprecated method.** Use `parseCssColorStr()` instead.
58
- * @description: since 2.0.1
59
- * @deprecated
60
- * @param {string} v
61
- * @return {CssColorParser}
62
- * @example: fromColorStr('rgba(255,255,255,1)')
63
- */
64
- export function fromColorStr(v: string): CssColorParser
65
- /**
66
- * @description: 将HSL色彩模式转换为解析对象
67
- * @param {number} hue 色相
68
- * @param {number} saturation 饱和度
69
- * @param {number} lightness 亮度
70
- * @param {number} alpha 不透明度
71
- * @return {CssColorParser}
72
- * @example: fromHSL(0,1,1,1)
73
- */
74
- export function fromHSL(
75
- h: number,
76
- s: number,
77
- l: number,
78
- a?: number
79
- ): CssColorParser
80
- /**
81
- * @description: 将HWB色彩模式转换为解析对象
82
- * @param {number} h 色调
83
- * @param {number} w 白度
84
- * @param {number} b 黑度
85
- * @param {number} a 不透明度
86
- * @return {CssColorParser}
87
- * @example: fromHSL(0,1,1,1)
88
- */
89
- export function fromHWB(
90
- h: number,
91
- w: number,
92
- b: number,
93
- a?: number
94
- ): CssColorParser
95
- /**
96
- * @description: 产生随机颜色
97
- * @return {CssColorParser}
98
- * @example: fromRandom('#000', ''#fff)
99
- */
100
- export function fromRandom(
101
- color1: string | CssColorParser,
102
- color2: string | CssColorParser
103
- ): CssColorParser
104
- /**
105
- * @description: 将ColorJson格式的json数据转换为解析对象
106
- * @param {ColorJson} json
107
- * @return {CssColorParser}
108
- * @author: roman_123
109
- */
110
- export function fromJson(json: ColorJson): CssColorParser
111
- /**
112
- * @description: 将rgba数组转换为解析对象
113
- * @param {Array} color
114
- * @return {CssColorParser}
115
- * @author: roman_123
116
- */
117
- export function fromArray(color: Array<number>): CssColorParser
118
- export class CssColorParser {
119
- r: number
120
- g: number
121
- b: number
122
- a: number
123
- constructor(
124
- red?: number | string,
125
- green?: number | string,
126
- blue?: number | string,
127
- alpha?: number | string
128
- )
129
- /**
130
- * 设置颜色
131
- * @param red
132
- * @param green
133
- * @param blue
134
- * @param alpha
135
- * @example: this.setColor(255,255,255,1)
136
- */
137
- setColor(
138
- red?: number | string,
139
- green?: number | string,
140
- blue?: number | string,
141
- alpha?: number | string
142
- ): void
143
- /**
144
- * @description: 返回rgba格式的css字符串
145
- * @return {*}
146
- * @author: roman_123
147
- */
148
- toRGBA(): string
149
- /**
150
- * @description: 返回字符串
151
- * @return {*}
152
- * @author: roman_123
153
- */
154
- toString(): string
155
- /**
156
- * @description: 归一化
157
- * @return {*}
158
- * @author: roman_123
159
- */
160
- toNormalize(): ColorJson
161
- /**
162
- * @description: 返回16进制格式的css字符串
163
- * @return {*}
164
- * @author: roman_123
165
- */
166
- toHEX(): string
167
- /**
168
- * @description: 返回rgba数组
169
- * @return {*}
170
- * @author: roman_123
171
- */
172
- toArray(): number[]
173
- /**
174
- * @description: 返回ColorJson
175
- * @return {*}
176
- * @author: roman_123
177
- */
178
- toJson(): ColorJson
179
- /**
180
- * @description: 拷贝
181
- * @return {*}
182
- * @author: roman_123
183
- */
184
- clone(): CssColorParser
185
- /**
186
- * @description: 比较两个解析对象的数据是否相等
187
- * @param {*} color
188
- * @return {*}
189
- * @author: roman_123
190
- */
191
- equals(color: any): boolean
192
- /**
193
- * @description: 解析16进制颜色
194
- * @param {string} v
195
- * @return {CssColorParser}
196
- * @example: parseHEX('#FFF')
197
- */
198
- static parseHEX(v: string): CssColorParser
199
- /**
200
- * @description: 解析rgba、rgb颜色
201
- * @param {string} v
202
- * @return {CssColorParser}
203
- * @example: parseRGBA('rgba(255,255,255,1)')
204
- */
205
- static parseRGBA(v: string): CssColorParser
206
- /**
207
- * @description: 将ColorJson格式的json数据转换为解析对象
208
- * @param {ColorJson} json
209
- * @return {CssColorParser}
210
- * @example: fromJson({r: 255, g: 255, b: 255, a: 1})
211
- */
212
- static fromJson(json: ColorJson): CssColorParser
213
- /**
214
- * @description: 将RGBA数组转换为解析对象
215
- * @param {Array} color
216
- * @return {CssColorParser}
217
- * @example: fromJson([255,255,255,1])
218
- */
219
- static fromArray(color: Array<number>): CssColorParser
220
- /**
221
- * @description: 产生随机颜色
222
- * @return {CssColorParser}
223
- * @example: fromRandom(new CssColorParser(0,0,0,0), new CssColorParser(255,255,255,1))
224
- */
225
- static fromRandom(
226
- color1: CssColorParser,
227
- color2: CssColorParser
228
- ): CssColorParser
229
- }
230
- }