I try to get the most matching color name depending on an given hex-value. For example if we have the hex-color #f00 we've to get the colorname red.
'#ff0000' => 'red' '#000000' => 'black' '#ffff00' => 'yellow' I use currently the levenshtein-distance algorithm to get the closest color name, works well so far, but sometimes not as expected.
For example:
'#0769ad' => 'chocolate' '#00aaee' => 'mediumspringgreen' So any ideas how to get the result closer?
Here's what I made to get the closest color:
Array.closest = (function () { // http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript function levDist(s, t) { if (!s.length) return t.length; if (!t.length) return s.length; return Math.min( levDist(s.substring(1), t) + 1, levDist(t.substring(1), s) + 1, levDist(s.substring(1), t.substring(1)) + (s[0] !== t[0] ? 1 : 0) ); } return function (arr, str) { // http://stackoverflow.com/q/11919065/1250044#comment16113902_11919065 return arr.sort(function (a, b) { return levDist(a, str) - levDist(b, str); }); }; }()); http://jsfiddle.net/ARTsinn/JUZVd/2/
Another thing is the performance! It seems that it there's somewehere a really big issue that makes this really slow (is it the algorithm?).