gka / chroma.js

JavaScript library for all kinds of color manipulations
https://gka.github.io/chroma.js/
Other
10.08k stars 543 forks source link

How to get closest 'name' to a chroma value? #248

Closed davidjgonzalez closed 3 years ago

davidjgonzalez commented 3 years ago

I am deriving colors from an image, and I would like to get the closed "named" color (in the event that the exact color does not have a 'named`' color, and only returns the hex value).

Is this possible via an API call? And/or is there a way to easily do this (ex. would i have to get the color, and add 1 to r/g/b until that resolves to a name? and then decrement - and pick the closer of the two)

meodai commented 3 years ago

@davidjgonzalez there is no method that does that. Because there are X ways to determine what color is closest to another. However I made a library: https://github.com/meodai/ClosestVector that I am using to do exactly this. The demo page has an example with colors: https://meodai.github.io/ClosestVector/ (https://github.com/meodai/ClosestVector/blob/master/index.html#L372)

If you don't want to implement it yourself, we also have a public rest-api: https://api.color.pizza/v1/212121,0f0,f00,f0f099?noduplicates=true (closest color name function. https://github.com/meodai/color-names/blob/master/scripts/nameColors.js)

There is the list of the names w3c names used in chroma js https://github.com/gka/chroma.js/blob/master/src/colors/w3cx11.js or something with more names: https://github.com/meodai/color-names (used by the API)

davidjgonzalez commented 3 years ago

@meodai Thanks! I was actually just about to come back and post a followup as I finished up my day: I ended up finding this module: https://www.npmjs.com/package/color-namer that works pretty well, it has a number of color lists (basic, roygbiv, pantone, etc.) so you can get a pretty interesting granularity of colors (though I think most ppl have no idea what some of them are :))

meodai commented 3 years ago

@davidjgonzalez I am the maintainer of "color-names" so of course I am biased :D

davidjgonzalez commented 3 years ago

@meodai ooh! i clicked through and it looks nice! ill take a closer look - and i just might have to switch over... !