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.34k stars 1.31k forks source link

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

Open alexeyr-ci opened 9 months ago

alexeyr-ci commented 9 months 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.

simplePCBuilding commented 2 weeks 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 2 weeks ago

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