lokesh / color-thief

Grab the color palette from an image using just Javascript. Works in the browser and in Node.
https://lokeshdhakar.com/projects/color-thief/
MIT License
12.66k stars 1.31k forks source link

Allow a Canvas as an argument in addition to an Image #250

Open alexeyr-ci opened 1 year ago

alexeyr-ci commented 1 year ago

In my use-case I already have a Canvas and want to find its dominant colors. It doesn't make much sense to convert it to an image and then draw that image on a canvas.

janishutz commented 4 months ago

That is true. I am currently working on a big PR, so I could add that as an option... What would you think the API should look like? I can think of three reasonable options:

  1. Adjust getColor to getColor( img: HMTLImageElement | HTMLCanvaasElement, [other options] )
  2. Make img argument optional and add canvas optional argument. One would have to be supplied (this would be a breaking change, as that argument shouldn't be added to the end, which is not ideal)
  3. Add a separate method, which would be called by the getColor one, if an image is specified: getColorCanvas( canvas: CanvasImage, [other options] ) getColor( img: HTMLImageElement ) which would call getColorCanvas

@alexeyr-ci

alexeyr-ci commented 4 months ago

I think 1 is clear enough, 3 is reasonable if you want to avoid 1.

janishutz commented 1 month ago

@alexeyr-ci I have implemented this, it is available in my npm package, run npm i @janishutz/colorthief to install it. You may also find the repo at https://github.com/janishutz/color-thief. The new version also features TypeScript support with TypeDefinitions of all functions, as well as updated functions with promises, which have replaced the now deprecated (but still available) async options