color-string 1.3.1 → 1.5.2

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.

Files changed (3) hide show
  1. package/README.md +4 -0
  2. package/index.js +36 -13
  3. 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
@@ -46,8 +46,8 @@ cs.get.rgb = function (string) {
46
46
  return null;
47
47
  }
48
48
 
49
- var abbr = /^#([a-fA-F0-9]{3})$/;
50
- var hex = /^#([a-fA-F0-9]{6})$/;
49
+ var abbr = /^#([a-f0-9]{3,4})$/i;
50
+ var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i;
51
51
  var rgba = /^rgba?\(\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
52
52
  var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/;
53
53
  var keyword = /(\D+)/;
@@ -55,20 +55,31 @@ cs.get.rgb = function (string) {
55
55
  var rgb = [0, 0, 0, 1];
56
56
  var match;
57
57
  var i;
58
+ var hexAlpha;
58
59
 
59
- if (match = string.match(abbr)) {
60
+ if (match = string.match(hex)) {
61
+ hexAlpha = match[2];
60
62
  match = match[1];
61
63
 
62
64
  for (i = 0; i < 3; i++) {
63
- rgb[i] = parseInt(match[i] + match[i], 16);
65
+ // https://jsperf.com/slice-vs-substr-vs-substring-methods-long-string/19
66
+ var i2 = i * 2;
67
+ rgb[i] = parseInt(match.slice(i2, i2 + 2), 16);
64
68
  }
65
- } else if (match = string.match(hex)) {
69
+
70
+ if (hexAlpha) {
71
+ rgb[3] = Math.round((parseInt(hexAlpha, 16) / 255) * 100) / 100;
72
+ }
73
+ } else if (match = string.match(abbr)) {
66
74
  match = match[1];
75
+ hexAlpha = match[3];
67
76
 
68
77
  for (i = 0; i < 3; i++) {
69
- // https://jsperf.com/slice-vs-substr-vs-substring-methods-long-string/19
70
- var i2 = i * 2;
71
- rgb[i] = parseInt(match.slice(i2, i2 + 2), 16);
78
+ rgb[i] = parseInt(match[i] + match[i], 16);
79
+ }
80
+
81
+ if (hexAlpha) {
82
+ rgb[3] = Math.round((parseInt(hexAlpha + hexAlpha, 16) / 255) * 100) / 100;
72
83
  }
73
84
  } else if (match = string.match(rgba)) {
74
85
  for (i = 0; i < 3; i++) {
@@ -100,9 +111,11 @@ cs.get.rgb = function (string) {
100
111
  rgb[3] = 1;
101
112
 
102
113
  return rgb;
114
+ } else {
115
+ return null;
103
116
  }
104
117
 
105
- for (i = 0; i < rgb.length; i++) {
118
+ for (i = 0; i < 3; i++) {
106
119
  rgb[i] = clamp(rgb[i], 0, 255);
107
120
  }
108
121
  rgb[3] = clamp(rgb[3], 0, 1);
@@ -151,16 +164,26 @@ cs.get.hwb = function (string) {
151
164
  return null;
152
165
  };
153
166
 
154
- cs.to.hex = function (rgb) {
155
- return '#' + hexDouble(rgb[0]) + hexDouble(rgb[1]) + hexDouble(rgb[2]);
167
+ cs.to.hex = function () {
168
+ var rgba = swizzle(arguments);
169
+
170
+ return (
171
+ '#' +
172
+ hexDouble(rgba[0]) +
173
+ hexDouble(rgba[1]) +
174
+ hexDouble(rgba[2]) +
175
+ (rgba[3] < 1
176
+ ? (hexDouble(Math.round(rgba[3] * 255)))
177
+ : '')
178
+ );
156
179
  };
157
180
 
158
181
  cs.to.rgb = function () {
159
182
  var rgba = swizzle(arguments);
160
183
 
161
184
  return rgba.length < 4 || rgba[3] === 1
162
- ? 'rgb(' + rgba[0] + ', ' + rgba[1] + ', ' + rgba[2] + ')'
163
- : 'rgba(' + rgba[0] + ', ' + rgba[1] + ', ' + rgba[2] + ', ' + rgba[3] + ')';
185
+ ? 'rgb(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ')'
186
+ : 'rgba(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ', ' + rgba[3] + ')';
164
187
  };
165
188
 
166
189
  cs.to.rgb.percent = function () {
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": "1.3.1",
4
+ "version": "1.5.2",
5
5
  "author": "Heather Arthur <fayearthur@gmail.com>",
6
6
  "contributors": [
7
7
  "Maxime Thirouin",