fuzzy-color
Advanced tools
Comparing version 0.0.1 to 0.0.2
37
index.js
module.exports = parse; | ||
function parse(str) { | ||
function parse(str, assumeType) { | ||
return rgb(str) | ||
|| rgba(str) | ||
|| other(str) | ||
|| hex(str); | ||
|| hex(str) | ||
|| assume(str, assumeType); | ||
} | ||
@@ -63,3 +64,3 @@ | ||
string: 'rgba(' + rgb.join(',') + ')', | ||
raw: rgb.map(function(i){ return parseInt(i, 10); }), | ||
raw: rgb.map(function(i){ return parseFloat(i); }), | ||
type: 'rgba' | ||
@@ -73,2 +74,32 @@ }; | ||
/** | ||
* Parse 0, 0, 0 | 0, 0, 0, 0 | 0 0 0 | 0 0 0 0 | ||
* | ||
* @param {String} str | ||
* @param {String} type - the type to assume number only values are. eg 'rgb', 'hsv', 'hsl' | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
function assume(str, type) { | ||
var result = str.replace(/,/g, '').replace(/ /g, ' ').split(' '); | ||
if (result.length >= 3) { | ||
if (result.length === 4) { | ||
return { | ||
string: 'rgba(' + result.join(',') + ')', | ||
raw: result.map(function(i){ return parseFloat(i); }), | ||
type: 'rgba' | ||
}; | ||
} else { | ||
return { | ||
string: type + '(' + result.join(',') + ')', | ||
raw: result.map(function(i){ return parseInt(i, 10); }), | ||
type: type | ||
}; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Parse #000 and #000000 | ||
@@ -75,0 +106,0 @@ * |
{ | ||
"name": "fuzzy-color", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Fuzzy color is a utility that takes a string and does its absolute best to parse a meaningful color value from it.", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/jonnyscholes/fuzzy-color", |
49
tests.js
@@ -39,3 +39,47 @@ var test = require('tape'); | ||
test('RGBA test with .5 opacity', function (t) { | ||
t.plan(1); | ||
var rgb = fuzzyColor('rgba(0,0,0,.5)'); | ||
t.deepEqual(rgb, { | ||
string: 'rgba(0,0,0,.5)', | ||
raw: [0, 0, 0,.5], | ||
type: 'rgba' | ||
}); | ||
}); | ||
test('assumeType=rgb with valid rgb value test - "0, 0, 0"', function (t) { | ||
t.plan(1); | ||
var rgb = fuzzyColor('0, 0, 0', 'rgb'); | ||
t.deepEqual(rgb, { | ||
string: 'rgb(0,0,0)', | ||
raw: [0, 0, 0], | ||
type: 'rgb' | ||
}); | ||
}); | ||
test('assumeType=rgb with valid rgb value test and no commas - "0 0 0"', function (t) { | ||
t.plan(1); | ||
var rgb = fuzzyColor('0 0 0', 'rgb'); | ||
t.deepEqual(rgb, { | ||
string: 'rgb(0,0,0)', | ||
raw: [0, 0, 0], | ||
type: 'rgb' | ||
}); | ||
}); | ||
test('assumeType=rgb with valid rgbA value test - "0, 0, 0, .5"', function (t) { | ||
t.plan(1); | ||
var rgb = fuzzyColor('0, 0, 0, .5', 'rgb'); | ||
t.deepEqual(rgb, { | ||
string: 'rgba(0,0,0,.5)', | ||
raw: [0, 0, 0, .5], | ||
type: 'rgba' | ||
}); | ||
}); | ||
test('3 char HEX test', function (t) { | ||
@@ -86,5 +130,2 @@ t.plan(1); | ||
}); | ||
}); | ||
// support R:252 G:252 B:252 | ||
// support (R145 / G145 / B149) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39912
266