color-string 0.2.1 → 0.3.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.

Potentially problematic release.


This version of color-string might be problematic. Click here for more details.

package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # 0.3.0
2
+
3
+ - Fixed: HSL alpha channel ([#16](https://github.com/harthur/color-string/pull/16))
4
+ - Fixed: ability to parse signed number ([#15](https://github.com/harthur/color-string/pull/15))
5
+ - Removed: component.json
6
+ - Removed: browser build
7
+ - Added: license field to package.json ([#17](https://github.com/harthur/color-string/pull/17))
8
+
9
+ ---
10
+
11
+ Check out commit logs for earlier releases
package/README.md CHANGED
@@ -31,6 +31,6 @@ colorString.hslString([360, 100, 100]) // "hsl(360, 100%, 100%)"
31
31
  For [node](http://nodejs.org) with [npm](http://npmjs.org):
32
32
 
33
33
  npm install color-string
34
-
34
+
35
35
  ### browser
36
- Download the latest [color-string.js](http://github.com/harthur/color-string/downloads). The `colorString` object is exported.
36
+ Download the latest [color-string.js](https://github.com/harthur/color-string/tree/gh-pages). The `colorString` object is exported.
package/color-string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /* MIT license */
2
- var convert = require("color-convert");
2
+ var colorNames = require('color-name');
3
3
 
4
4
  module.exports = {
5
5
  getRgba: getRgba,
@@ -26,8 +26,8 @@ function getRgba(string) {
26
26
  }
27
27
  var abbr = /^#([a-fA-F0-9]{3})$/,
28
28
  hex = /^#([a-fA-F0-9]{6})$/,
29
- rgba = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d\.]+)\s*)?\)$/,
30
- per = /^rgba?\(\s*([\d\.]+)\%\s*,\s*([\d\.]+)\%\s*,\s*([\d\.]+)\%\s*(?:,\s*([\d\.]+)\s*)?\)$/,
29
+ rgba = /^rgba?\(\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/,
30
+ per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/,
31
31
  keyword = /(\D+)/;
32
32
 
33
33
  var rgb = [0, 0, 0],
@@ -61,7 +61,7 @@ function getRgba(string) {
61
61
  if (match[1] == "transparent") {
62
62
  return [0, 0, 0, 0];
63
63
  }
64
- rgb = convert.keyword2rgb(match[1]);
64
+ rgb = colorNames[match[1]];
65
65
  if (!rgb) {
66
66
  return;
67
67
  }
@@ -76,7 +76,7 @@ function getRgba(string) {
76
76
  else {
77
77
  a = scale(a, 0, 1);
78
78
  }
79
- rgb.push(a);
79
+ rgb[3] = a;
80
80
  return rgb;
81
81
  }
82
82
 
@@ -84,13 +84,14 @@ function getHsla(string) {
84
84
  if (!string) {
85
85
  return;
86
86
  }
87
- var hsl = /^hsla?\(\s*(\d+)\s*,\s*([\d\.]+)%\s*,\s*([\d\.]+)%\s*(?:,\s*([\d\.]+)\s*)?\)/;
87
+ var hsl = /^hsla?\(\s*([+-]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)/;
88
88
  var match = string.match(hsl);
89
89
  if (match) {
90
+ var alpha = parseFloat(match[4]);
90
91
  var h = scale(parseInt(match[1]), 0, 360),
91
92
  s = scale(parseFloat(match[2]), 0, 100),
92
93
  l = scale(parseFloat(match[3]), 0, 100),
93
- a = scale(parseFloat(match[4]) || 1, 0, 1);
94
+ a = scale(isNaN(alpha) ? 1 : alpha, 0, 1);
94
95
  return [h, s, l, a];
95
96
  }
96
97
  }
@@ -99,13 +100,14 @@ function getHwb(string) {
99
100
  if (!string) {
100
101
  return;
101
102
  }
102
- var hwb = /^hwb\(\s*(\d+)\s*,\s*([\d\.]+)%\s*,\s*([\d\.]+)%\s*(?:,\s*([\d\.]+)\s*)?\)/;
103
+ var hwb = /^hwb\(\s*([+-]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)/;
103
104
  var match = string.match(hwb);
104
105
  if (match) {
106
+ var alpha = parseFloat(match[4]);
105
107
  var h = scale(parseInt(match[1]), 0, 360),
106
108
  w = scale(parseFloat(match[2]), 0, 100),
107
109
  b = scale(parseFloat(match[3]), 0, 100),
108
- a = scale(parseFloat(match[4]) || 1, 0, 1);
110
+ a = scale(isNaN(alpha) ? 1 : alpha, 0, 1);
109
111
  return [h, w, b, a];
110
112
  }
111
113
  }
@@ -198,7 +200,7 @@ function hwbString(hwb, alpha) {
198
200
  }
199
201
 
200
202
  function keyword(rgb) {
201
- return convert.rgb2keyword(rgb.slice(0, 3));
203
+ return reverseNames[rgb.slice(0, 3)];
202
204
  }
203
205
 
204
206
  // helpers
@@ -210,3 +212,10 @@ function hexDouble(num) {
210
212
  var str = num.toString(16).toUpperCase();
211
213
  return (str.length < 2) ? "0" + str : str;
212
214
  }
215
+
216
+
217
+ //create a list of reverse color names
218
+ var reverseNames = {};
219
+ for (var name in colorNames) {
220
+ reverseNames[colorNames[name]] = name;
221
+ }
package/package.json CHANGED
@@ -1,19 +1,30 @@
1
1
  {
2
- "name": "color-string",
3
- "description": "Parser and generator for CSS color strings",
4
- "version": "0.2.1",
5
- "author": "Heather Arthur <fayearthur@gmail.com>",
6
- "repository": {
7
- "type": "git",
8
- "url": "http://github.com/harthur/color-string.git"
9
- },
10
- "main": "./color-string",
11
- "dependencies": {
12
- "color-convert": "0.5.x"
13
- },
14
- "devDependencies": {
15
- "browserify": ">=1.0.0",
16
- "uglify-js": "1.0.x"
17
- },
18
- "keywords": ["color", "colour", "rgb", "css"]
19
- }
2
+ "name": "color-string",
3
+ "description": "Parser and generator for CSS color strings",
4
+ "version": "0.3.0",
5
+ "author": "Heather Arthur <fayearthur@gmail.com>",
6
+ "contributors": [
7
+ "Maxime Thirouin",
8
+ "Dyma Ywanov <dfcreative@gmail.com>"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "http://github.com/harthur/color-string.git"
13
+ },
14
+ "scripts": {
15
+ "test": "node test/basic.js"
16
+ },
17
+ "license": "MIT",
18
+ "main": "./color-string",
19
+ "dependencies": {
20
+ "color-name": "^1.0.0"
21
+ },
22
+ "devDependencies": {
23
+ },
24
+ "keywords": [
25
+ "color",
26
+ "colour",
27
+ "rgb",
28
+ "css"
29
+ ]
30
+ }
package/test/basic.js CHANGED
@@ -1,14 +1,30 @@
1
1
  var string = require("../color-string"),
2
2
  assert = require("assert");
3
3
 
4
+
4
5
  assert.deepEqual(string.getRgba("#fef"), [255, 238, 255, 1]);
5
6
  assert.deepEqual(string.getRgba("#fffFEF"), [255, 255, 239,1]);
6
7
  assert.deepEqual(string.getRgba("rgb(244, 233, 100)"), [244, 233, 100, 1]);
7
8
  assert.deepEqual(string.getRgba("rgb(100%, 30%, 90%)"), [255, 77, 229, 1]);
8
- assert.deepEqual(string.getRgba("blue"), [0, 0, 255, 1]);
9
9
  assert.deepEqual(string.getRgba("transparent"), [0, 0, 0, 0]);
10
10
  assert.deepEqual(string.getHsla("hsl(240, 100%, 50.5%)"), [240, 100, 50.5, 1]);
11
+ assert.deepEqual(string.getHsla("hsl(240deg, 100%, 50.5%)"), [240, 100, 50.5, 1]);
11
12
  assert.deepEqual(string.getHwb("hwb(240, 100%, 50.5%)"), [240, 100, 50.5, 1]);
13
+ assert.deepEqual(string.getHwb("hwb(240deg, 100%, 50.5%)"), [240, 100, 50.5, 1]);
14
+
15
+ // with sign
16
+ assert.deepEqual(string.getRgba("rgb(-244, +233, -100)"), [0, 233, 0, 1]);
17
+ assert.deepEqual(string.getHsla("hsl(+240, 100%, 50.5%)"), [240, 100, 50.5, 1]);
18
+ assert.deepEqual(string.getRgba("rgba(200, +20, -233, -0.0)"), [200, 20, 0, 0]);
19
+ assert.deepEqual(string.getRgba("rgba(200, +20, -233, -0.0)"), [200, 20, 0, 0]);
20
+ assert.deepEqual(string.getHsla("hsla(+200, 100%, 50%, -0.2)"), [200, 100, 50, 0]);
21
+ assert.deepEqual(string.getHwb("hwb(+240, 100%, 50.5%)"), [240, 100, 50.5, 1]);
22
+ assert.deepEqual(string.getHwb("hwb(-240deg, 100%, 50.5%)"), [0, 100, 50.5, 1]);
23
+ assert.deepEqual(string.getHwb("hwb(-240deg, 100%, 50.5%, +0.6)"), [0, 100, 50.5, 0.6]);
24
+
25
+ //subsequent return values should not change array
26
+ assert.deepEqual(string.getRgba("blue"), [0, 0, 255, 1]);
27
+ assert.deepEqual(string.getRgba("blue"), [0, 0, 255, 1]);
12
28
 
13
29
  assert.equal(string.getAlpha("rgb(244, 233, 100)"), 1);
14
30
  assert.equal(string.getAlpha("rgba(244, 233, 100, 0.5)"), 0.5);
@@ -27,6 +43,9 @@ assert.deepEqual(string.getHwb("hwb(200, 20%, 33%, 0.2)"), [200, 20, 33, 0.2]);
27
43
  assert.deepEqual(string.getRgb("#fef"), [255, 238, 255]);
28
44
  assert.deepEqual(string.getRgb("rgba(200, 20, 233, 0.2)"), [200, 20, 233]);
29
45
  assert.deepEqual(string.getHsl("hsl(240, 100%, 50.5%)"), [240, 100, 50.5]);
46
+ assert.deepEqual(string.getRgba('rgba(0,0,0,0)'), [0, 0, 0, 0]);
47
+ assert.deepEqual(string.getHsla('hsla(0,0%,0%,0)'), [0, 0, 0, 0]);
48
+ assert.deepEqual(string.getHwb("hwb(400, 10%, 200%, 0)"), [360, 10, 100, 0]);
30
49
 
31
50
  // range
32
51
  assert.deepEqual(string.getRgba("rgba(300, 600, 100, 3)"), [255, 255, 100, 1]);
package/Jakefile.js DELETED
@@ -1,54 +0,0 @@
1
- /*
2
- Turns CommonJS package into a browser file and minifies.
3
-
4
- uses node-jake http://github.com/mde/node-jake
5
- install jake with `npm install jake -g`
6
-
7
- run with `jake [build|minify|clean]`
8
- */
9
- var fs = require("fs"),
10
- path = require("path"),
11
- browserify = require("browserify");
12
-
13
- var pkg = JSON.parse(fs.readFileSync("package.json"));
14
- var prefix = pkg.name + "-" + pkg.version;
15
-
16
- task('build', [], function (dest) {
17
- console.log("building...");
18
- dest = dest || prefix + ".js";
19
-
20
- var source = browserify.bundle({
21
- name: "color-string",
22
- main: path.join(__dirname, pkg.main),
23
- shim: false
24
- });
25
- source = "var colorString = (function() {" + source + " return require('color-string')})();"
26
-
27
- fs.writeFileSync(dest, source);
28
- console.log("> " + dest);
29
- });
30
-
31
- task('minify', [], function (file, dest) {
32
- file = file || prefix + ".js";
33
- dest = dest || prefix + ".min.js";
34
-
35
- var minified = minify(fs.readFileSync(file, "utf-8"));
36
- fs.writeFileSync(dest, minified, "utf-8");
37
- console.log("> " + dest)
38
- });
39
-
40
- task('clean', [], function () {
41
- fs.unlink(prefix + ".js");
42
- fs.unlink(prefix + ".min.js");
43
- });
44
-
45
- function minify(code) {
46
- var uglifyjs = require("uglify-js"),
47
- parser = uglifyjs.parser,
48
- uglify = uglifyjs.uglify;
49
-
50
- var ast = parser.parse(code);
51
- ast = uglify.ast_mangle(ast);
52
- ast = uglify.ast_squeeze(ast);
53
- return uglify.gen_code(ast);
54
- }
package/component.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "name": "color-string",
3
- "description": "Parser and generator for CSS color strings",
4
- "version": "0.1.1",
5
- "author": "Heather Arthur <fayearthur@gmail.com>",
6
- "repository": "harthur/color-string",
7
- "scripts": ["color-string.js"],
8
- "main": "color-string.js",
9
- "dependencies": {
10
- "harthur/color-convert": "*"
11
- },
12
- "keywords": ["color", "colour", "rgb", "css"]
13
- }