Closed GMartigny closed 3 years ago
Hi! I would try to find a good way to implement that. The hardest part, I guess, is implementing a logic to find the closest Hex/RGB value.
This option imply looping through all existing colors, computing a distance to the requested value and keeping the lowest result. With 147 distincts colors, it shouldn't be to bad for performance.
Thanks! Will try to implement that in a week. How do you think it better to call the new parameter?
colord("#fe0000").toName({ closest: true }); // "yellow"
colord("#fe0000").toName({ approximate: true }); // "yellow"
colord("#fe0000").toName({ strict: false }); // "yellow"
// ???
It depends on what you think the default behavior should be. I think that it should be changed with a true
value.
If the default behavior is having a proximity search, then you should go for strict
or exact
to disable it.
Otherwise, if the default is NOT doing the search, then closest
, nearest
or approximate
sounds better.
Hi! Sorry for the super long delay (I had a wedding and a lot of other things last months). I finally found time to add the functionality:
// available in v2.5
colord("#fe0000").toName({ closest: true }); // "red"
Works perfectly, nice job !
When parsing to name using the plugin, if the color is not exactly the right value the result is
undefined
. At user level, it can be expected that the library always return a color name.If failing to find a perfect match, the lib could search for the closest color. This behavior could also be activated by a parameter to the
toName
function.