color-string 1.5.1 → 1.5.5
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/index.js +16 -15
- package/package.json +1 -1
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,7 +47,7 @@ cs.get.rgb = function (string) {
|
|
|
46
47
|
return null;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
var abbr = /^#([a-f0-9]{3})
|
|
50
|
+
var abbr = /^#([a-f0-9]{3,4})$/i;
|
|
50
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*)?\)$/;
|
|
@@ -57,29 +58,29 @@ cs.get.rgb = function (string) {
|
|
|
57
58
|
var i;
|
|
58
59
|
var hexAlpha;
|
|
59
60
|
|
|
60
|
-
if (match = string.match(
|
|
61
|
+
if (match = string.match(hex)) {
|
|
61
62
|
hexAlpha = match[2];
|
|
62
63
|
match = match[1];
|
|
63
64
|
|
|
64
65
|
for (i = 0; i < 3; i++) {
|
|
65
|
-
|
|
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);
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
if (hexAlpha) {
|
|
69
|
-
rgb[3] =
|
|
72
|
+
rgb[3] = parseInt(hexAlpha, 16) / 255;
|
|
70
73
|
}
|
|
71
|
-
} else if (match = string.match(
|
|
72
|
-
hexAlpha = match[2];
|
|
74
|
+
} else if (match = string.match(abbr)) {
|
|
73
75
|
match = match[1];
|
|
76
|
+
hexAlpha = match[3];
|
|
74
77
|
|
|
75
78
|
for (i = 0; i < 3; i++) {
|
|
76
|
-
|
|
77
|
-
var i2 = i * 2;
|
|
78
|
-
rgb[i] = parseInt(match.slice(i2, i2 + 2), 16);
|
|
79
|
+
rgb[i] = parseInt(match[i] + match[i], 16);
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
if (hexAlpha) {
|
|
82
|
-
rgb[3] =
|
|
83
|
+
rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255;
|
|
83
84
|
}
|
|
84
85
|
} else if (match = string.match(rgba)) {
|
|
85
86
|
for (i = 0; i < 3; i++) {
|
|
@@ -115,7 +116,7 @@ cs.get.rgb = function (string) {
|
|
|
115
116
|
return null;
|
|
116
117
|
}
|
|
117
118
|
|
|
118
|
-
for (i = 0; i <
|
|
119
|
+
for (i = 0; i < 3; i++) {
|
|
119
120
|
rgb[i] = clamp(rgb[i], 0, 255);
|
|
120
121
|
}
|
|
121
122
|
rgb[3] = clamp(rgb[3], 0, 1);
|
|
@@ -128,12 +129,12 @@ cs.get.hsl = function (string) {
|
|
|
128
129
|
return null;
|
|
129
130
|
}
|
|
130
131
|
|
|
131
|
-
var hsl = /^hsla?\(\s*([+-]
|
|
132
|
+
var hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
|
132
133
|
var match = string.match(hsl);
|
|
133
134
|
|
|
134
135
|
if (match) {
|
|
135
136
|
var alpha = parseFloat(match[4]);
|
|
136
|
-
var h = (
|
|
137
|
+
var h = (parseFloat(match[1]) + 360) % 360;
|
|
137
138
|
var s = clamp(parseFloat(match[2]), 0, 100);
|
|
138
139
|
var l = clamp(parseFloat(match[3]), 0, 100);
|
|
139
140
|
var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1);
|
|
@@ -149,7 +150,7 @@ cs.get.hwb = function (string) {
|
|
|
149
150
|
return null;
|
|
150
151
|
}
|
|
151
152
|
|
|
152
|
-
var hwb = /^hwb\(\s*([+-]?\d
|
|
153
|
+
var hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
|
|
153
154
|
var match = string.match(hwb);
|
|
154
155
|
|
|
155
156
|
if (match) {
|