color-namer
Advanced tools
Comparing version 0.3.0 to 1.0.0
@@ -12,3 +12,3 @@ "use strict" | ||
} catch(e) { | ||
return console.log(e) | ||
return console.error(e) | ||
} | ||
@@ -38,3 +38,3 @@ | ||
// Find color names | ||
var colors = colorNamer(color.hex(), 'html') | ||
var colors = colorNamer(color.hex(), 'ntc') | ||
@@ -41,0 +41,0 @@ // Refresh list |
52
index.js
"use strict"; | ||
var presets = { | ||
var distance = require('euclidean-distance') | ||
var chroma = require('chroma-js') | ||
var color | ||
var key | ||
// These `require` statements are all explicit | ||
// to keep the browserify build from breaking | ||
var lists = { | ||
basic: require('./lib/colors/basic'), | ||
html: require('./lib/colors/html'), | ||
ntc: require('./lib/colors/ntc'), | ||
pantone: require('./lib/colors/pantone'), | ||
roygbiv: require('./lib/colors/roygbiv') | ||
roygbiv: require('./lib/colors/roygbiv'), | ||
x11: require('./lib/colors/x11') | ||
} | ||
var distance = require('euclidean-distance') | ||
var chroma = require('chroma-js') | ||
var color | ||
var namer = module.exports = function(color, names) { | ||
if (!names) | ||
names = "basic" | ||
if (typeof(names) === "string") | ||
names = presets[names] | ||
if (!Array.isArray(names)) { | ||
throw new Error("Invalid names " + JSON.stringify(names)) | ||
var namer = module.exports = function(color) { | ||
color = chroma(color) | ||
var results = {} | ||
for (key in lists) { | ||
results[key] = lists[key] | ||
.map (function(name) { | ||
name.distance = distance(color.lab(), chroma(name.hex).lab()) | ||
return name | ||
}) | ||
.sort (function(a, b) { | ||
return a.distance - b.distance | ||
}) | ||
} | ||
color = chroma(color) | ||
return names | ||
.map (function(name) { | ||
name.distance = distance(color.lab(), chroma(name.hex).lab()) | ||
return name | ||
}) | ||
.sort (function(a, b) { | ||
return a.distance - b.distance | ||
}) | ||
return results | ||
} | ||
namer.chroma = chroma | ||
namer.lists = lists |
{ | ||
"name": "color-namer", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"description": "Give me a color and I'll name it.", | ||
@@ -28,3 +28,3 @@ "main": "index.js", | ||
}, | ||
"license": "WTFPL", | ||
"license": "MIT", | ||
"devDependencies": { | ||
@@ -31,0 +31,0 @@ "browserify": "^4.2.1", |
@@ -5,6 +5,5 @@ # Color Namer | ||
Color Namer is a node module that calculates color distance using the | ||
Color Namer is an npm package for use in Node.js or the browser that calculates color distance using the | ||
[Delta-E](http://www.colorwiki.com/wiki/Delta_E%3a_The_Color_Difference) color difference technique. Given a color | ||
in [some format](https://github.com/gka/chroma.js/blob/master/doc/api.md#chromaa-b-c-a-mode) like RGB, HSL, or HSV, it uses the awesome [chroma-js](https://npmjs.org/package/chroma-js) | ||
node module to convert the color to the [L*a*b* color space](http://en.wikipedia.org/wiki/Lab_color_space), | ||
in [Hexadecimal RGB, RGBA, HSL, or HSV format](https://github.com/gka/chroma.js/blob/master/doc/api.md#chromaa-b-c-a-mode), it converts the color to the [L*a*b* color space](http://en.wikipedia.org/wiki/Lab_color_space), | ||
then calculates the color's | ||
@@ -14,2 +13,18 @@ [Euclidean distance](https://npmjs.org/package/euclidean-distance) from a set of colors with | ||
Mike Bostock of [D3](http://d3js.org/) fame [explains it well](https://gist.github.com/mbostock/3014589): | ||
> Lab and HCL color spaces are special in that the perceived difference between two colors is proportional to their Euclidean distance in color space. This special property, called perceptual uniformity, makes them ideal for accurate visual encoding of data. In contrast, the more familiar RGB and HSL color spaces distort data when used for visualization. | ||
## Lists | ||
The color names are derived from several lists: | ||
- [roygbiv](lib/colors/roygbiv.js) | ||
- [basic](lib/colors/basic.js) | ||
- [html](lib/colors/html.js) - the HTML color names. | ||
- [x11](lib/colors/x11.js) - The list that preceded the HTML color names | ||
- [pantone](lib/colors/pantone.js) | ||
- [ntc](lib/colors/ntc.js), an [astounding collection](http://chir.ag/projects/ntc/) of over 1500 named colors. | ||
## Installation | ||
@@ -23,3 +38,3 @@ | ||
Easy peasy: | ||
Require the module: | ||
@@ -31,4 +46,15 @@ ```js | ||
The function returns an array of colors objects, sorted by proximity in the [L*a*b* color space](http://en.wikipedia.org/wiki/Lab_color_space): | ||
From the above code, `names` will have a key for each list: | ||
``` | ||
names.roygbiv | ||
names.basic | ||
names.html | ||
names.x11 | ||
names.pantone | ||
names.ntc | ||
``` | ||
Each list is an array of colors, sorted by their perceptual similarity to the given color: | ||
```js | ||
@@ -67,33 +93,10 @@ [ | ||
## Sets | ||
Other input format work too like HSL, RGB, and RGBA: | ||
By default, names are chosen from a small set of [basic colors](/lib/basic-colors.js). | ||
```js | ||
// These are equivalent: | ||
namer("#FF0000") | ||
namer("#FF0000", 'basic') | ||
name("hsl(50,100%,50%)") | ||
name("rgb(255,0,0)") | ||
name("rgba(255,0,0,1)") | ||
``` | ||
To use the [HTML color names](/lib/html-colors.js): | ||
```js | ||
namer("#FF0000", 'html') | ||
``` | ||
Or good ol' [ROYGBIV](http://en.wikipedia.org/wiki/Roy_G._Biv): | ||
```js | ||
namer("#FF0000", 'roygbiv') | ||
``` | ||
Or bring your own name data: | ||
```js | ||
namer("#FF0000", [ | ||
{ name: 'black', hex: '#000' }, | ||
{ name: 'white', hex: '#FFF' } | ||
]) | ||
``` | ||
## Tests | ||
@@ -108,2 +111,2 @@ | ||
[WTFPL](http://wtfpl.org/) | ||
MIT |
@@ -12,45 +12,27 @@ "use strict"; | ||
test('returns an array of candidates with distance values', function() { | ||
test('returns an object containing named sorted arrays', function() { | ||
var names = namer('#ffa500') | ||
assert(Array.isArray(names)) | ||
assert.equal(names[0].distance, 0) | ||
assert(!Array.isArray(names)) | ||
assert(names.basic) | ||
assert(names.html) | ||
assert(names.ntc) | ||
assert(names.pantone) | ||
assert(names.roygbiv) | ||
assert(names.x11) | ||
assert.equal(names.basic[0].distance, 0) | ||
}) | ||
test('defaults to HTML color candidates', function() { | ||
var names = namer('0000FF') | ||
assert.equal(names.length, 21) | ||
}) | ||
test('accepts custom color candidates', function() { | ||
var candidates = [ | ||
{ name: 'blue', hex: '#0000ff' }, | ||
{ name: 'green', hex: '#008000' }, | ||
{ name: 'yellow', hex: '#ffff00' } | ||
] | ||
var names = namer('0000FF', candidates) | ||
assert.equal(names.length, 3) | ||
}) | ||
test('allows second argument to be a string specifying color set to use', function() { | ||
var names = namer('0000F3', 'basic') | ||
assert(names.length < 25) | ||
assert.equal(names[0].name, "blue") | ||
}) | ||
test('validates format of custom candidates', function() { | ||
assert.throws(function() { namer("00CC00", "badness") }) | ||
}) | ||
test('matches inexact colors', function() { | ||
var names = namer('FF0001') | ||
assert.equal(names[0].hex, '#FF0000') | ||
assert.equal(names[0].name, 'red') | ||
assert.equal(Math.floor(names[0].distance*100), 25) | ||
assert.equal(names.basic[0].hex, '#FF0000') | ||
assert.equal(names.basic[0].name, 'red') | ||
assert.equal(Math.floor(names.basic[0].distance*100), 25) | ||
}) | ||
test('matches exact colors', function() { | ||
var names = namer('0000FF') | ||
assert.equal(names[0].hex, '#0000FF') | ||
assert.equal(names[0].name, 'blue') | ||
assert.equal(names[0].distance, 0) | ||
assert.equal(names.basic[0].hex, '#0000FF') | ||
assert.equal(names.basic[0].name, 'blue') | ||
assert.equal(names.basic[0].distance, 0) | ||
}) | ||
@@ -60,12 +42,30 @@ | ||
var names = namer('#0000FF') | ||
assert.equal(names[0].hex, '#0000FF') | ||
assert.equal(names[0].name, 'blue') | ||
assert.equal(names[0].distance, 0) | ||
assert.equal(names.basic[0].hex, '#0000FF') | ||
assert.equal(names.basic[0].name, 'blue') | ||
assert.equal(names.basic[0].distance, 0) | ||
}) | ||
test('accepts non-hex input formats', function() { | ||
test('accepts HSL input', function() { | ||
var names = namer("hsl(50,100%,50%)") | ||
assert.equal(names[0].name, 'gold') | ||
assert.equal(names.basic[0].name, 'gold') | ||
}) | ||
suite('lists', function() { | ||
test('are available as a property of the exported module', function() { | ||
assert(namer.lists) | ||
}) | ||
test('are arrays of objects with keys `name` and `hex`', function() { | ||
for (var list in namer.lists) { | ||
namer.lists[list].forEach(function(color){ | ||
assert(color.name); | ||
assert(color.hex); | ||
}) | ||
} | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is too big to display
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
292896
22
16775
0
107