color-namer
Advanced tools
Comparing version 1.3.0 to 1.4.0
11
index.js
"use strict"; | ||
var chroma = require('chroma-js') | ||
var WeakMap = require("es6-weak-map") | ||
@@ -16,5 +17,12 @@ // These `require` statements are all explicit | ||
var cache = new WeakMap() | ||
var namer = module.exports = function(color, options) { | ||
options = options || {} | ||
var cacheKey = {color, options} | ||
if (cache.has(cacheKey)) { | ||
return cache.get(cacheKey) | ||
} | ||
var deltaE = String(options.distance).toLowerCase() === 'deltae'; | ||
color = chroma(color) | ||
@@ -31,3 +39,3 @@ var results = {} | ||
.map (function(name) { | ||
name.distance = chroma.distance(color, chroma(name.hex)) | ||
name.distance = deltaE ? chroma.deltaE(color, chroma(name.hex)) : chroma.distance(color, chroma(name.hex)) | ||
return name | ||
@@ -39,2 +47,3 @@ }) | ||
} | ||
cache.set(cacheKey, results) | ||
return results | ||
@@ -41,0 +50,0 @@ } |
{ | ||
"name": "color-namer", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Give me a color and I'll name it.", | ||
"repository": "https://github.com/zeke/color-namer", | ||
"repository": "https://github.com/colorjs/color-namer", | ||
"main": "index.js", | ||
@@ -35,4 +35,5 @@ "scripts": { | ||
"dependencies": { | ||
"chroma-js": "^1.3.4" | ||
"chroma-js": "^1.3.4", | ||
"es6-weak-map": "^2.0.3" | ||
} | ||
} |
@@ -115,2 +115,12 @@ # Color Namer [![Build Status](https://travis-ci.org/zeke/color-namer.svg?branch=master)](https://travis-ci.org/zeke/color-namer) | ||
### options.distance | ||
If 'deltaE', use the [Delta-E](http://zschuessler.github.io/DeltaE/learn/) distance function, otherwise uses default distance function. | ||
```js | ||
var names = namer(color, { pick: ['basic'], distance: 'deltaE' }); | ||
// output: { basic: [...] } | ||
``` | ||
### | ||
## Tests | ||
@@ -117,0 +127,0 @@ |
@@ -24,2 +24,9 @@ "use strict"; | ||
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', function() { | ||
@@ -26,0 +33,0 @@ var names = namer('FF0001') |
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
296303
16815
135
2
23
+ Addedes6-weak-map@^2.0.3
+ Addedd@1.0.2(transitive)
+ Addedes5-ext@0.10.64(transitive)
+ Addedes6-iterator@2.0.3(transitive)
+ Addedes6-symbol@3.1.4(transitive)
+ Addedes6-weak-map@2.0.3(transitive)
+ Addedesniff@2.0.1(transitive)
+ Addedevent-emitter@0.3.5(transitive)
+ Addedext@1.7.0(transitive)
+ Addednext-tick@1.1.0(transitive)
+ Addedtype@2.7.3(transitive)