Skip to content

Commit

Permalink
Preparing pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
John Merchant committed Jul 13, 2019
1 parent 5f91f52 commit 5a6ecae
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1,687 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var chroma = require('chroma-js')
var WeakMap = require("es6-weak-map")

// These `require` statements are all explicit
// to keep the browserify build from breaking
Expand All @@ -13,11 +14,11 @@ var lists = {
x11: require('./lib/colors/x11')
}

let cache = new WeakMap()
var cache = new WeakMap()
var namer = module.exports = function(color, options) {
options = options || {}

const cacheKey = {color, options}
var cacheKey = {color, options}
if (cache.has(cacheKey)) {
return cache.get(cacheKey)
}
Expand All @@ -33,7 +34,7 @@ var namer = module.exports = function(color, options) {
}
results[key] = lists[key]
.map (function(name) {
name.distance = chroma.deltaE(color, chroma(name.hex))
name.distance = options.distance === 'deltaE' ? chroma.deltaE(color, chroma(name.hex)) : chroma.distance(color, chroma(name.hex))
return name
})
.sort (function(a, b) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"mocha": "^6.1.4"
},
"dependencies": {
"chroma-js": "^1.3.4"
"chroma-js": "^1.3.4",
"es6-weak-map": "^2.0.3"
}
}
11 changes: 9 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ suite('namer', function () {
assert.equal(names.basic[0].distance, 0)
})

test('matches inexact colors', function() {
var names = namer('FF0001')
test('matches inexact colors with deltaE', function() {
var names = namer('FF0001', { distance: 'deltaE' })
assert.equal(names.basic[0].hex, '#FF0000')
assert.equal(names.basic[0].name, 'red')
assert.equal(Math.floor(names.basic[0].distance*100), 13)
})

test('matches inexact colors with deltaE', function() {
var names = namer('FF0001')
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')
Expand Down
Loading

0 comments on commit 5a6ecae

Please sign in to comment.