mirror of
https://github.com/janishutz/color-thief.git
synced 2025-11-25 13:54:25 +00:00
2 lines
1.6 KiB
JavaScript
2 lines
1.6 KiB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("ndarray-pixels"),require("@lokesh.dhakar/quantize"),require("sharp")):"function"==typeof define&&define.amd?define(["ndarray-pixels","@lokesh.dhakar/quantize","sharp"],t):t((e||self).ndarrayPixels,e.quantize,e.sharp)}(this,function(e,t,n){function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=/*#__PURE__*/o(t),u=/*#__PURE__*/o(n),a=function(t,n,o){void 0===n&&(n=10),void 0===o&&(o=10);var a=function(e){var t=e.colorCount,n=e.quality;if(void 0!==t&&Number.isInteger(t)){if(1===t)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");t=Math.max(t,2),t=Math.min(t,20)}else t=10;return(void 0===n||!Number.isInteger(n)||n<1)&&(n=10),{colorCount:t,quality:n}}({colorCount:n,quality:o});return new Promise(function(n,o){(function(t){return new Promise(function(n,o){u.default(t).toBuffer().then(function(e){return u.default(e).metadata().then(function(t){return{buffer:e,format:t.format}})}).then(function(t){return e.getPixels(t.buffer,t.format)}).then(n).catch(o)})})(t).then(function(e){var t=e.shape[0]*e.shape[1],o=function(e,t,n){for(var o=[],r=0;r<t;r+=n){var u=4*r,a=e[u+0],i=e[u+1],f=e[u+2],l=e[u+3];(void 0===l||l>=125)&&(a>250&&i>250&&f>250||o.push([a,i,f]))}return o}(Array.from(e.data),t,a.quality),u=r.default(o,a.colorCount),i=u?u.palette():null;n(i)}).catch(function(e){o(e)})})};module.exports={getColor:function(e,t){return void 0===t&&(t=10),new Promise(function(n,o){a(e,5,t).then(function(e){n(e[0])}).catch(function(e){o(e)})})},getPalette:a}});
|