color-string 1.9.1 → 2.0.0
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/README.md +16 -19
- package/index.d.ts +23 -0
- package/index.js +76 -88
- package/package.json +13 -7
package/README.md
CHANGED
|
@@ -4,10 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
```console
|
|
10
|
-
$ npm install color-string
|
|
7
|
+
```sh
|
|
8
|
+
npm install color-string
|
|
11
9
|
```
|
|
12
10
|
|
|
13
11
|
## Usage
|
|
@@ -44,19 +42,18 @@ colorString.get.rgb('invalid color string') // null
|
|
|
44
42
|
### Generation
|
|
45
43
|
|
|
46
44
|
```js
|
|
47
|
-
colorString.to.hex(
|
|
48
|
-
colorString.to.hex(
|
|
49
|
-
colorString.to.hex(
|
|
50
|
-
colorString.to.rgb(
|
|
51
|
-
colorString.to.rgb(
|
|
52
|
-
colorString.to.rgb(
|
|
53
|
-
colorString.to.rgb.percent(
|
|
54
|
-
colorString.to.keyword(
|
|
55
|
-
colorString.to.hsl(
|
|
56
|
-
colorString.to.hwb(
|
|
57
|
-
|
|
58
|
-
// all functions also support swizzling
|
|
59
|
-
colorString.to.rgb(0, [0, 255], 0.4) // "rgba(0, 0, 255, 0.4)"
|
|
60
|
-
colorString.to.rgb([0, 0], [255], 0.4) // "rgba(0, 0, 255, 0.4)"
|
|
61
|
-
colorString.to.rgb([0], 0, [255, 0.4]) // "rgba(0, 0, 255, 0.4)"
|
|
45
|
+
colorString.to.hex(255, 255, 255) // "#FFFFFF"
|
|
46
|
+
colorString.to.hex(0, 0, 255, 0.4) // "#0000FF66"
|
|
47
|
+
colorString.to.hex(0, 0, 255, 0.4) // "#0000FF66"
|
|
48
|
+
colorString.to.rgb(255, 255, 255) // "rgb(255, 255, 255)"
|
|
49
|
+
colorString.to.rgb(0, 0, 255, 0.4) // "rgba(0, 0, 255, 0.4)"
|
|
50
|
+
colorString.to.rgb(0, 0, 255, 0.4) // "rgba(0, 0, 255, 0.4)"
|
|
51
|
+
colorString.to.rgb.percent(0, 0, 255) // "rgb(0%, 0%, 100%)"
|
|
52
|
+
colorString.to.keyword(255, 255, 0) // "yellow"
|
|
53
|
+
colorString.to.hsl(360, 100, 100) // "hsl(360, 100%, 100%)"
|
|
54
|
+
colorString.to.hwb(50, 3, 15) // "hwb(50, 3%, 15%)"
|
|
62
55
|
```
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
|
|
59
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type Model = 'rgb' | 'hsl' | 'hwb';
|
|
2
|
+
|
|
3
|
+
export type ColorString = {
|
|
4
|
+
get: {
|
|
5
|
+
(color: string): {model: Model; value: number[]} | undefined;
|
|
6
|
+
rgb: (color: string) => number[] | undefined;
|
|
7
|
+
hsl: (color: string) => number[] | undefined;
|
|
8
|
+
hwb: (color: string) => number[] | undefined;
|
|
9
|
+
};
|
|
10
|
+
to: {
|
|
11
|
+
hex: (r: number, g: number, b: number, a?: number) => string | undefined;
|
|
12
|
+
rgb: {
|
|
13
|
+
(r: number, g: number, b: number, a?: number): string | undefined;
|
|
14
|
+
percent: (r: number, g: number, b: number, a?: number) => string | undefined;
|
|
15
|
+
};
|
|
16
|
+
keyword: (r: number, g: number, b: number, a?: number) => string | undefined;
|
|
17
|
+
hsl: (h: number, s: number, l: number, a?: number) => string | undefined;
|
|
18
|
+
hwb: (h: number, w: number, b: number, a?: number) => string | undefined;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
declare const colorString: ColorString;
|
|
23
|
+
export default colorString;
|
package/index.js
CHANGED
|
@@ -1,46 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
var colorNames = require('color-name');
|
|
3
|
-
var swizzle = require('simple-swizzle');
|
|
4
|
-
var hasOwnProperty = Object.hasOwnProperty;
|
|
1
|
+
import colorNames from 'color-name';
|
|
5
2
|
|
|
6
|
-
|
|
3
|
+
const reverseNames = Object.create(null);
|
|
7
4
|
|
|
8
|
-
//
|
|
9
|
-
for (
|
|
10
|
-
if (
|
|
5
|
+
// Create a list of reverse color names
|
|
6
|
+
for (const name in colorNames) {
|
|
7
|
+
if (Object.hasOwn(colorNames, name)) {
|
|
11
8
|
reverseNames[colorNames[name]] = name;
|
|
12
9
|
}
|
|
13
10
|
}
|
|
14
11
|
|
|
15
|
-
|
|
12
|
+
const cs = {
|
|
16
13
|
to: {},
|
|
17
|
-
get: {}
|
|
14
|
+
get: {},
|
|
18
15
|
};
|
|
19
16
|
|
|
20
17
|
cs.get = function (string) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
const prefix = string.slice(0, 3).toLowerCase();
|
|
19
|
+
let value;
|
|
20
|
+
let model;
|
|
24
21
|
switch (prefix) {
|
|
25
|
-
case 'hsl':
|
|
26
|
-
|
|
22
|
+
case 'hsl': {
|
|
23
|
+
value = cs.get.hsl(string);
|
|
27
24
|
model = 'hsl';
|
|
28
25
|
break;
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
case 'hwb': {
|
|
29
|
+
value = cs.get.hwb(string);
|
|
31
30
|
model = 'hwb';
|
|
32
31
|
break;
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
default: {
|
|
35
|
+
value = cs.get.rgb(string);
|
|
35
36
|
model = 'rgb';
|
|
36
37
|
break;
|
|
38
|
+
}
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
if (!
|
|
41
|
+
if (!value) {
|
|
40
42
|
return null;
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
return {model
|
|
45
|
+
return {model, value};
|
|
44
46
|
};
|
|
45
47
|
|
|
46
48
|
cs.get.rgb = function (string) {
|
|
@@ -48,16 +50,16 @@ cs.get.rgb = function (string) {
|
|
|
48
50
|
return null;
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
const abbr = /^#([a-f\d]{3,4})$/i;
|
|
54
|
+
const hex = /^#([a-f\d]{6})([a-f\d]{2})?$/i;
|
|
55
|
+
const rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/;
|
|
56
|
+
const per = /^rgba?\(\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/;
|
|
57
|
+
const keyword = /^(\w+)$/;
|
|
56
58
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
let rgb = [0, 0, 0, 1];
|
|
60
|
+
let match;
|
|
61
|
+
let i;
|
|
62
|
+
let hexAlpha;
|
|
61
63
|
|
|
62
64
|
if (match = string.match(hex)) {
|
|
63
65
|
hexAlpha = match[2];
|
|
@@ -65,54 +67,46 @@ cs.get.rgb = function (string) {
|
|
|
65
67
|
|
|
66
68
|
for (i = 0; i < 3; i++) {
|
|
67
69
|
// https://jsperf.com/slice-vs-substr-vs-substring-methods-long-string/19
|
|
68
|
-
|
|
69
|
-
rgb[i] = parseInt(match.slice(i2, i2 + 2), 16);
|
|
70
|
+
const i2 = i * 2;
|
|
71
|
+
rgb[i] = Number.parseInt(match.slice(i2, i2 + 2), 16);
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
if (hexAlpha) {
|
|
73
|
-
rgb[3] = parseInt(hexAlpha, 16) / 255;
|
|
75
|
+
rgb[3] = Number.parseInt(hexAlpha, 16) / 255;
|
|
74
76
|
}
|
|
75
77
|
} else if (match = string.match(abbr)) {
|
|
76
78
|
match = match[1];
|
|
77
79
|
hexAlpha = match[3];
|
|
78
80
|
|
|
79
81
|
for (i = 0; i < 3; i++) {
|
|
80
|
-
rgb[i] = parseInt(match[i] + match[i], 16);
|
|
82
|
+
rgb[i] = Number.parseInt(match[i] + match[i], 16);
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
if (hexAlpha) {
|
|
84
|
-
rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255;
|
|
86
|
+
rgb[3] = Number.parseInt(hexAlpha + hexAlpha, 16) / 255;
|
|
85
87
|
}
|
|
86
88
|
} else if (match = string.match(rgba)) {
|
|
87
89
|
for (i = 0; i < 3; i++) {
|
|
88
|
-
rgb[i] = parseInt(match[i + 1],
|
|
90
|
+
rgb[i] = Number.parseInt(match[i + 1], 10);
|
|
89
91
|
}
|
|
90
92
|
|
|
91
93
|
if (match[4]) {
|
|
92
|
-
|
|
93
|
-
rgb[3] = parseFloat(match[4]) * 0.01;
|
|
94
|
-
} else {
|
|
95
|
-
rgb[3] = parseFloat(match[4]);
|
|
96
|
-
}
|
|
94
|
+
rgb[3] = match[5] ? Number.parseFloat(match[4]) * 0.01 : Number.parseFloat(match[4]);
|
|
97
95
|
}
|
|
98
96
|
} else if (match = string.match(per)) {
|
|
99
97
|
for (i = 0; i < 3; i++) {
|
|
100
|
-
rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55);
|
|
98
|
+
rgb[i] = Math.round(Number.parseFloat(match[i + 1]) * 2.55);
|
|
101
99
|
}
|
|
102
100
|
|
|
103
101
|
if (match[4]) {
|
|
104
|
-
|
|
105
|
-
rgb[3] = parseFloat(match[4]) * 0.01;
|
|
106
|
-
} else {
|
|
107
|
-
rgb[3] = parseFloat(match[4]);
|
|
108
|
-
}
|
|
102
|
+
rgb[3] = match[5] ? Number.parseFloat(match[4]) * 0.01 : Number.parseFloat(match[4]);
|
|
109
103
|
}
|
|
110
104
|
} else if (match = string.match(keyword)) {
|
|
111
105
|
if (match[1] === 'transparent') {
|
|
112
106
|
return [0, 0, 0, 0];
|
|
113
107
|
}
|
|
114
108
|
|
|
115
|
-
if (!
|
|
109
|
+
if (!Object.hasOwn(colorNames, match[1])) {
|
|
116
110
|
return null;
|
|
117
111
|
}
|
|
118
112
|
|
|
@@ -127,6 +121,7 @@ cs.get.rgb = function (string) {
|
|
|
127
121
|
for (i = 0; i < 3; i++) {
|
|
128
122
|
rgb[i] = clamp(rgb[i], 0, 255);
|
|
129
123
|
}
|
|
124
|
+
|
|
130
125
|
rgb[3] = clamp(rgb[3], 0, 1);
|
|
131
126
|
|
|
132
127
|
return rgb;
|
|
@@ -137,15 +132,15 @@ cs.get.hsl = function (string) {
|
|
|
137
132
|
return null;
|
|
138
133
|
}
|
|
139
134
|
|
|
140
|
-
|
|
141
|
-
|
|
135
|
+
const hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/;
|
|
136
|
+
const match = string.match(hsl);
|
|
142
137
|
|
|
143
138
|
if (match) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
139
|
+
const alpha = Number.parseFloat(match[4]);
|
|
140
|
+
const h = ((Number.parseFloat(match[1]) % 360) + 360) % 360;
|
|
141
|
+
const s = clamp(Number.parseFloat(match[2]), 0, 100);
|
|
142
|
+
const l = clamp(Number.parseFloat(match[3]), 0, 100);
|
|
143
|
+
const a = clamp(Number.isNaN(alpha) ? 1 : alpha, 0, 1);
|
|
149
144
|
|
|
150
145
|
return [h, s, l, a];
|
|
151
146
|
}
|
|
@@ -158,24 +153,22 @@ cs.get.hwb = function (string) {
|
|
|
158
153
|
return null;
|
|
159
154
|
}
|
|
160
155
|
|
|
161
|
-
|
|
162
|
-
|
|
156
|
+
const hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d.]+)%\s*,\s*([+-]?[\d.]+)%\s*(?:,\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/;
|
|
157
|
+
const match = string.match(hwb);
|
|
163
158
|
|
|
164
159
|
if (match) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
160
|
+
const alpha = Number.parseFloat(match[4]);
|
|
161
|
+
const h = ((Number.parseFloat(match[1]) % 360) + 360) % 360;
|
|
162
|
+
const w = clamp(Number.parseFloat(match[2]), 0, 100);
|
|
163
|
+
const b = clamp(Number.parseFloat(match[3]), 0, 100);
|
|
164
|
+
const a = clamp(Number.isNaN(alpha) ? 1 : alpha, 0, 1);
|
|
170
165
|
return [h, w, b, a];
|
|
171
166
|
}
|
|
172
167
|
|
|
173
168
|
return null;
|
|
174
169
|
};
|
|
175
170
|
|
|
176
|
-
cs.to.hex = function () {
|
|
177
|
-
var rgba = swizzle(arguments);
|
|
178
|
-
|
|
171
|
+
cs.to.hex = function (...rgba) {
|
|
179
172
|
return (
|
|
180
173
|
'#' +
|
|
181
174
|
hexDouble(rgba[0]) +
|
|
@@ -187,39 +180,32 @@ cs.to.hex = function () {
|
|
|
187
180
|
);
|
|
188
181
|
};
|
|
189
182
|
|
|
190
|
-
cs.to.rgb = function () {
|
|
191
|
-
var rgba = swizzle(arguments);
|
|
192
|
-
|
|
183
|
+
cs.to.rgb = function (...rgba) {
|
|
193
184
|
return rgba.length < 4 || rgba[3] === 1
|
|
194
185
|
? 'rgb(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ')'
|
|
195
186
|
: 'rgba(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ', ' + rgba[3] + ')';
|
|
196
187
|
};
|
|
197
188
|
|
|
198
|
-
cs.to.rgb.percent = function () {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
var g = Math.round(rgba[1] / 255 * 100);
|
|
203
|
-
var b = Math.round(rgba[2] / 255 * 100);
|
|
189
|
+
cs.to.rgb.percent = function (...rgba) {
|
|
190
|
+
const r = Math.round(rgba[0] / 255 * 100);
|
|
191
|
+
const g = Math.round(rgba[1] / 255 * 100);
|
|
192
|
+
const b = Math.round(rgba[2] / 255 * 100);
|
|
204
193
|
|
|
205
194
|
return rgba.length < 4 || rgba[3] === 1
|
|
206
195
|
? 'rgb(' + r + '%, ' + g + '%, ' + b + '%)'
|
|
207
196
|
: 'rgba(' + r + '%, ' + g + '%, ' + b + '%, ' + rgba[3] + ')';
|
|
208
197
|
};
|
|
209
198
|
|
|
210
|
-
cs.to.hsl = function () {
|
|
211
|
-
var hsla = swizzle(arguments);
|
|
199
|
+
cs.to.hsl = function (...hsla) {
|
|
212
200
|
return hsla.length < 4 || hsla[3] === 1
|
|
213
201
|
? 'hsl(' + hsla[0] + ', ' + hsla[1] + '%, ' + hsla[2] + '%)'
|
|
214
202
|
: 'hsla(' + hsla[0] + ', ' + hsla[1] + '%, ' + hsla[2] + '%, ' + hsla[3] + ')';
|
|
215
203
|
};
|
|
216
204
|
|
|
217
|
-
//
|
|
205
|
+
// Hwb is a bit different than rgb(a) & hsl(a) since there is no alpha specific syntax
|
|
218
206
|
// (hwb have alpha optional & 1 is default value)
|
|
219
|
-
cs.to.hwb = function () {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
var a = '';
|
|
207
|
+
cs.to.hwb = function (...hwba) {
|
|
208
|
+
let a = '';
|
|
223
209
|
if (hwba.length >= 4 && hwba[3] !== 1) {
|
|
224
210
|
a = ', ' + hwba[3];
|
|
225
211
|
}
|
|
@@ -227,16 +213,18 @@ cs.to.hwb = function () {
|
|
|
227
213
|
return 'hwb(' + hwba[0] + ', ' + hwba[1] + '%, ' + hwba[2] + '%' + a + ')';
|
|
228
214
|
};
|
|
229
215
|
|
|
230
|
-
cs.to.keyword = function (rgb) {
|
|
216
|
+
cs.to.keyword = function (...rgb) {
|
|
231
217
|
return reverseNames[rgb.slice(0, 3)];
|
|
232
218
|
};
|
|
233
219
|
|
|
234
|
-
//
|
|
235
|
-
function clamp(
|
|
236
|
-
return Math.min(Math.max(min,
|
|
220
|
+
// Helpers
|
|
221
|
+
function clamp(number_, min, max) {
|
|
222
|
+
return Math.min(Math.max(min, number_), max);
|
|
237
223
|
}
|
|
238
224
|
|
|
239
|
-
function hexDouble(
|
|
240
|
-
|
|
241
|
-
return (
|
|
225
|
+
function hexDouble(number_) {
|
|
226
|
+
const string_ = Math.round(number_).toString(16).toUpperCase();
|
|
227
|
+
return (string_.length < 2) ? '0' + string_ : string_;
|
|
242
228
|
}
|
|
229
|
+
|
|
230
|
+
export default cs;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "color-string",
|
|
3
3
|
"description": "Parser and generator for CSS color strings",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"author": "Heather Arthur <fayearthur@gmail.com>",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Maxime Thirouin",
|
|
@@ -9,13 +9,19 @@
|
|
|
9
9
|
"Josh Junon"
|
|
10
10
|
],
|
|
11
11
|
"repository": "Qix-/color-string",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": "./index.js",
|
|
14
|
+
"types": "./index.d.ts",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
},
|
|
12
18
|
"scripts": {
|
|
13
|
-
"
|
|
14
|
-
"test": "node test/basic.js"
|
|
19
|
+
"test": "xo && tsd && node test.js"
|
|
15
20
|
},
|
|
16
21
|
"license": "MIT",
|
|
17
22
|
"files": [
|
|
18
|
-
"index.js"
|
|
23
|
+
"index.js",
|
|
24
|
+
"index.d.ts"
|
|
19
25
|
],
|
|
20
26
|
"xo": {
|
|
21
27
|
"rules": {
|
|
@@ -24,11 +30,11 @@
|
|
|
24
30
|
}
|
|
25
31
|
},
|
|
26
32
|
"dependencies": {
|
|
27
|
-
"color-name": "^
|
|
28
|
-
"simple-swizzle": "^0.2.2"
|
|
33
|
+
"color-name": "^2.0.0"
|
|
29
34
|
},
|
|
30
35
|
"devDependencies": {
|
|
31
|
-
"
|
|
36
|
+
"tsd": "^0.31.2",
|
|
37
|
+
"xo": "^0.60.0"
|
|
32
38
|
},
|
|
33
39
|
"keywords": [
|
|
34
40
|
"color",
|