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