color-string 1.4.0 → 1.5.3
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.
Potentially problematic release.
This version of color-string might be problematic. Click here for more details.
- package/README.md +4 -0
- package/index.js +38 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,8 @@ $ npm install color-string
|
|
|
18
18
|
|
|
19
19
|
```js
|
|
20
20
|
colorString.get('#FFF') // {model: 'rgb', value: [255, 255, 255, 1]}
|
|
21
|
+
colorString.get('#FFFA') // {model: 'rgb', value: [255, 255, 255, 0.67]}
|
|
22
|
+
colorString.get('#FFFFFFAA') // {model: 'rgb', value: [255, 255, 255, 0.67]}
|
|
21
23
|
colorString.get('hsl(360, 100%, 50%)') // {model: 'hsl', value: [0, 100, 50, 1]}
|
|
22
24
|
colorString.get('hwb(60, 3%, 60%)') // {model: 'hwb', value: [60, 3, 60, 1]}
|
|
23
25
|
|
|
@@ -39,6 +41,8 @@ colorString.get.rgb('invalid color string') // null
|
|
|
39
41
|
|
|
40
42
|
```js
|
|
41
43
|
colorString.to.hex([255, 255, 255]) // "#FFFFFF"
|
|
44
|
+
colorString.to.hex([0, 0, 255, 0.4]) // "#0000FF66"
|
|
45
|
+
colorString.to.hex([0, 0, 255], 0.4) // "#0000FF66"
|
|
42
46
|
colorString.to.rgb([255, 255, 255]) // "rgb(255, 255, 255)"
|
|
43
47
|
colorString.to.rgb([0, 0, 255, 0.4]) // "rgba(0, 0, 255, 0.4)"
|
|
44
48
|
colorString.to.rgb([0, 0, 255], 0.4) // "rgba(0, 0, 255, 0.4)"
|
package/index.js
CHANGED
|
@@ -12,7 +12,8 @@ for (var name in colorNames) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
var cs = module.exports = {
|
|
15
|
-
to: {}
|
|
15
|
+
to: {},
|
|
16
|
+
get: {}
|
|
16
17
|
};
|
|
17
18
|
|
|
18
19
|
cs.get = function (string) {
|
|
@@ -46,8 +47,8 @@ cs.get.rgb = function (string) {
|
|
|
46
47
|
return null;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
var abbr = /^#([a-
|
|
50
|
-
var hex = /^#([a-
|
|
50
|
+
var abbr = /^#([a-f0-9]{3,4})$/i;
|
|
51
|
+
var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i;
|
|
51
52
|
var rgba = /^rgba?\(\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
|
52
53
|
var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
|
53
54
|
var keyword = /(\D+)/;
|
|
@@ -55,20 +56,31 @@ cs.get.rgb = function (string) {
|
|
|
55
56
|
var rgb = [0, 0, 0, 1];
|
|
56
57
|
var match;
|
|
57
58
|
var i;
|
|
59
|
+
var hexAlpha;
|
|
58
60
|
|
|
59
|
-
if (match = string.match(
|
|
61
|
+
if (match = string.match(hex)) {
|
|
62
|
+
hexAlpha = match[2];
|
|
60
63
|
match = match[1];
|
|
61
64
|
|
|
62
65
|
for (i = 0; i < 3; i++) {
|
|
63
|
-
|
|
66
|
+
// https://jsperf.com/slice-vs-substr-vs-substring-methods-long-string/19
|
|
67
|
+
var i2 = i * 2;
|
|
68
|
+
rgb[i] = parseInt(match.slice(i2, i2 + 2), 16);
|
|
64
69
|
}
|
|
65
|
-
|
|
70
|
+
|
|
71
|
+
if (hexAlpha) {
|
|
72
|
+
rgb[3] = Math.round((parseInt(hexAlpha, 16) / 255) * 100) / 100;
|
|
73
|
+
}
|
|
74
|
+
} else if (match = string.match(abbr)) {
|
|
66
75
|
match = match[1];
|
|
76
|
+
hexAlpha = match[3];
|
|
67
77
|
|
|
68
78
|
for (i = 0; i < 3; i++) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
79
|
+
rgb[i] = parseInt(match[i] + match[i], 16);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (hexAlpha) {
|
|
83
|
+
rgb[3] = Math.round((parseInt(hexAlpha + hexAlpha, 16) / 255) * 100) / 100;
|
|
72
84
|
}
|
|
73
85
|
} else if (match = string.match(rgba)) {
|
|
74
86
|
for (i = 0; i < 3; i++) {
|
|
@@ -100,9 +112,11 @@ cs.get.rgb = function (string) {
|
|
|
100
112
|
rgb[3] = 1;
|
|
101
113
|
|
|
102
114
|
return rgb;
|
|
115
|
+
} else {
|
|
116
|
+
return null;
|
|
103
117
|
}
|
|
104
118
|
|
|
105
|
-
for (i = 0; i <
|
|
119
|
+
for (i = 0; i < 3; i++) {
|
|
106
120
|
rgb[i] = clamp(rgb[i], 0, 255);
|
|
107
121
|
}
|
|
108
122
|
rgb[3] = clamp(rgb[3], 0, 1);
|
|
@@ -115,12 +129,12 @@ cs.get.hsl = function (string) {
|
|
|
115
129
|
return null;
|
|
116
130
|
}
|
|
117
131
|
|
|
118
|
-
var hsl = /^hsla?\(\s*([+-]
|
|
132
|
+
var hsl = /^hsla?\(\s*([+-]?(?:\d*\.)?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
|
119
133
|
var match = string.match(hsl);
|
|
120
134
|
|
|
121
135
|
if (match) {
|
|
122
136
|
var alpha = parseFloat(match[4]);
|
|
123
|
-
var h = (
|
|
137
|
+
var h = (parseFloat(match[1]) + 360) % 360;
|
|
124
138
|
var s = clamp(parseFloat(match[2]), 0, 100);
|
|
125
139
|
var l = clamp(parseFloat(match[3]), 0, 100);
|
|
126
140
|
var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1);
|
|
@@ -151,8 +165,18 @@ cs.get.hwb = function (string) {
|
|
|
151
165
|
return null;
|
|
152
166
|
};
|
|
153
167
|
|
|
154
|
-
cs.to.hex = function (
|
|
155
|
-
|
|
168
|
+
cs.to.hex = function () {
|
|
169
|
+
var rgba = swizzle(arguments);
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
'#' +
|
|
173
|
+
hexDouble(rgba[0]) +
|
|
174
|
+
hexDouble(rgba[1]) +
|
|
175
|
+
hexDouble(rgba[2]) +
|
|
176
|
+
(rgba[3] < 1
|
|
177
|
+
? (hexDouble(Math.round(rgba[3] * 255)))
|
|
178
|
+
: '')
|
|
179
|
+
);
|
|
156
180
|
};
|
|
157
181
|
|
|
158
182
|
cs.to.rgb = function () {
|