color-string 1.6.0 → 1.9.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 +3 -2
- package/index.js +21 -13
- package/package.json +1 -1
- package/CHANGELOG.md +0 -18
package/README.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# color-string
|
|
2
2
|
|
|
3
|
-
[](https://travis-ci.org/Qix-/color-string)
|
|
4
|
-
|
|
5
3
|
> library for parsing and generating CSS color strings.
|
|
6
4
|
|
|
7
5
|
## Install
|
|
@@ -27,7 +25,10 @@ colorString.get('hwb(60, 3%, 60%)') // {model: 'hwb', value: [60, 3
|
|
|
27
25
|
colorString.get.rgb('#FFF') // [255, 255, 255, 1]
|
|
28
26
|
colorString.get.rgb('blue') // [0, 0, 255, 1]
|
|
29
27
|
colorString.get.rgb('rgba(200, 60, 60, 0.3)') // [200, 60, 60, 0.3]
|
|
28
|
+
colorString.get.rgb('rgba(200 60 60 / 0.3)') // [200, 60, 60, 0.3]
|
|
29
|
+
colorString.get.rgb('rgba(200 60 60 / 30%)') // [200, 60, 60, 0.3]
|
|
30
30
|
colorString.get.rgb('rgb(200, 200, 200)') // [200, 200, 200, 1]
|
|
31
|
+
colorString.get.rgb('rgb(200 200 200)') // [200, 200, 200, 1]
|
|
31
32
|
|
|
32
33
|
colorString.get.hsl('hsl(360, 100%, 50%)') // [0, 100, 50, 1]
|
|
33
34
|
colorString.get.hsl('hsl(360 100% 50%)') // [0, 100, 50, 1]
|
package/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/* MIT license */
|
|
2
2
|
var colorNames = require('color-name');
|
|
3
3
|
var swizzle = require('simple-swizzle');
|
|
4
|
+
var hasOwnProperty = Object.hasOwnProperty;
|
|
4
5
|
|
|
5
6
|
var reverseNames = {};
|
|
6
7
|
|
|
7
8
|
// create a list of reverse color names
|
|
8
9
|
for (var name in colorNames) {
|
|
9
|
-
if (
|
|
10
|
+
if (hasOwnProperty.call(colorNames, name)) {
|
|
10
11
|
reverseNames[colorNames[name]] = name;
|
|
11
12
|
}
|
|
12
13
|
}
|
|
@@ -49,9 +50,9 @@ cs.get.rgb = function (string) {
|
|
|
49
50
|
|
|
50
51
|
var abbr = /^#([a-f0-9]{3,4})$/i;
|
|
51
52
|
var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i;
|
|
52
|
-
var rgba = /^rgba?\(\s*([+-]?\d+)\s
|
|
53
|
-
var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s
|
|
54
|
-
var keyword =
|
|
53
|
+
var rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/;
|
|
54
|
+
var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/;
|
|
55
|
+
var keyword = /^(\w+)$/;
|
|
55
56
|
|
|
56
57
|
var rgb = [0, 0, 0, 1];
|
|
57
58
|
var match;
|
|
@@ -88,7 +89,11 @@ cs.get.rgb = function (string) {
|
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
if (match[4]) {
|
|
91
|
-
|
|
92
|
+
if (match[5]) {
|
|
93
|
+
rgb[3] = parseFloat(match[4]) * 0.01;
|
|
94
|
+
} else {
|
|
95
|
+
rgb[3] = parseFloat(match[4]);
|
|
96
|
+
}
|
|
92
97
|
}
|
|
93
98
|
} else if (match = string.match(per)) {
|
|
94
99
|
for (i = 0; i < 3; i++) {
|
|
@@ -96,19 +101,22 @@ cs.get.rgb = function (string) {
|
|
|
96
101
|
}
|
|
97
102
|
|
|
98
103
|
if (match[4]) {
|
|
99
|
-
|
|
104
|
+
if (match[5]) {
|
|
105
|
+
rgb[3] = parseFloat(match[4]) * 0.01;
|
|
106
|
+
} else {
|
|
107
|
+
rgb[3] = parseFloat(match[4]);
|
|
108
|
+
}
|
|
100
109
|
}
|
|
101
110
|
} else if (match = string.match(keyword)) {
|
|
102
111
|
if (match[1] === 'transparent') {
|
|
103
112
|
return [0, 0, 0, 0];
|
|
104
113
|
}
|
|
105
114
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (!rgb) {
|
|
115
|
+
if (!hasOwnProperty.call(colorNames, match[1])) {
|
|
109
116
|
return null;
|
|
110
117
|
}
|
|
111
118
|
|
|
119
|
+
rgb = colorNames[match[1]];
|
|
112
120
|
rgb[3] = 1;
|
|
113
121
|
|
|
114
122
|
return rgb;
|
|
@@ -129,12 +137,12 @@ cs.get.hsl = function (string) {
|
|
|
129
137
|
return null;
|
|
130
138
|
}
|
|
131
139
|
|
|
132
|
-
var hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d\.]+)%\s*,?\s*([+-]?[\d\.]+)%\s*(?:[,|\/]\s*([+-]?[\d
|
|
140
|
+
var 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*)?\)$/;
|
|
133
141
|
var match = string.match(hsl);
|
|
134
142
|
|
|
135
143
|
if (match) {
|
|
136
144
|
var alpha = parseFloat(match[4]);
|
|
137
|
-
var h = (parseFloat(match[1]) + 360) % 360;
|
|
145
|
+
var h = ((parseFloat(match[1]) % 360) + 360) % 360;
|
|
138
146
|
var s = clamp(parseFloat(match[2]), 0, 100);
|
|
139
147
|
var l = clamp(parseFloat(match[3]), 0, 100);
|
|
140
148
|
var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1);
|
|
@@ -150,7 +158,7 @@ cs.get.hwb = function (string) {
|
|
|
150
158
|
return null;
|
|
151
159
|
}
|
|
152
160
|
|
|
153
|
-
var hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d
|
|
161
|
+
var 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*)?\)$/;
|
|
154
162
|
var match = string.match(hwb);
|
|
155
163
|
|
|
156
164
|
if (match) {
|
|
@@ -229,6 +237,6 @@ function clamp(num, min, max) {
|
|
|
229
237
|
}
|
|
230
238
|
|
|
231
239
|
function hexDouble(num) {
|
|
232
|
-
var str = num.toString(16).toUpperCase();
|
|
240
|
+
var str = Math.round(num).toString(16).toUpperCase();
|
|
233
241
|
return (str.length < 2) ? '0' + str : str;
|
|
234
242
|
}
|
package/package.json
CHANGED
package/CHANGELOG.md
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# 0.4.0
|
|
2
|
-
|
|
3
|
-
- Changed: Invalid conversions now return `null` instead of `undefined`
|
|
4
|
-
- Changed: Moved to XO standard
|
|
5
|
-
- Fixed: a few details in package.json
|
|
6
|
-
- Fixed: readme output regarding wrapped hue values ([#21](https://github.com/MoOx/color-string/pull/21))
|
|
7
|
-
|
|
8
|
-
# 0.3.0
|
|
9
|
-
|
|
10
|
-
- Fixed: HSL alpha channel ([#16](https://github.com/harthur/color-string/pull/16))
|
|
11
|
-
- Fixed: ability to parse signed number ([#15](https://github.com/harthur/color-string/pull/15))
|
|
12
|
-
- Removed: component.json
|
|
13
|
-
- Removed: browser build
|
|
14
|
-
- Added: license field to package.json ([#17](https://github.com/harthur/color-string/pull/17))
|
|
15
|
-
|
|
16
|
-
---
|
|
17
|
-
|
|
18
|
-
Check out commit logs for earlier releases
|