Skip to content

Commit

Permalink
Add memoization, use deltaE
Browse files Browse the repository at this point in the history
  • Loading branch information
John Merchant committed Jul 6, 2019
1 parent c83f32e commit 1201d45
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ var lists = {
x11: require('./lib/colors/x11')
}

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

const cacheKey = JSON.stringify({color, options});
if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}

color = chroma(color)
var results = {}
for (var key in lists) {
Expand All @@ -27,13 +33,14 @@ var namer = module.exports = function(color, options) {
}
results[key] = lists[key]
.map (function(name) {
name.distance = chroma.distance(color, chroma(name.hex))
name.distance = chroma.deltaE(color, chroma(name.hex))
return name
})
.sort (function(a, b) {
return a.distance - b.distance
})
}
cache.set(cacheKey, results)
return results
}

Expand Down

0 comments on commit 1201d45

Please sign in to comment.