mirror of
https://github.com/janishutz/color-thief.git
synced 2025-11-25 05:44:24 +00:00
done
This commit is contained in:
1
dist/color-thief-node.cjs
vendored
Normal file
1
dist/color-thief-node.cjs
vendored
Normal file
@@ -0,0 +1 @@
|
||||
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};
|
||||
1
dist/color-thief-node.mjs
vendored
Normal file
1
dist/color-thief-node.mjs
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import{getPixels as t}from"ndarray-pixels";import o from"@lokesh.dhakar/quantize";import n from"sharp";var r=function(r,e,u){void 0===e&&(e=10),void 0===u&&(u=10);var a=function(t){var o=t.colorCount,n=t.quality;if(void 0!==o&&Number.isInteger(o)){if(1===o)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");o=Math.max(o,2),o=Math.min(o,20)}else o=10;return(void 0===n||!Number.isInteger(n)||n<1)&&(n=10),{colorCount:o,quality:n}}({colorCount:e,quality:u});return new Promise(function(e,u){(function(o){return new Promise(function(r,e){n(o).toBuffer().then(function(t){return n(t).metadata().then(function(o){return{buffer:t,format:o.format}})}).then(function(o){return t(o.buffer,o.format)}).then(r).catch(e)})})(r).then(function(t){var n=t.shape[0]*t.shape[1],r=function(t,o,n){for(var r=[],e=0;e<o;e+=n){var u=4*e,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),n,a.quality),u=o(r,a.colorCount),i=u?u.palette():null;e(i)}).catch(function(t){u(t)})})};module.exports={getColor:function(t,o){return void 0===o&&(o=10),new Promise(function(n,e){r(t,5,o).then(function(t){n(t[0])}).catch(function(t){e(t)})})},getPalette:r};
|
||||
1
dist/color-thief-node.modern.js
vendored
Normal file
1
dist/color-thief-node.modern.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import{getPixels as t}from"ndarray-pixels";import e from"@lokesh.dhakar/quantize";import o from"sharp";const r=(r,a=10,n=10)=>{const l=(t=>{let e=t.colorCount,o=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===o||!Number.isInteger(o)||o<1)&&(o=10),{colorCount:e,quality:o}})({colorCount:a,quality:n});return new Promise((a,n)=>{(e=>new Promise((r,a)=>{o(e).toBuffer().then(t=>o(t).metadata().then(e=>({buffer:t,format:e.format}))).then(({buffer:e,format:o})=>t(e,o)).then(r).catch(a)}))(r).then(t=>{const o=t.shape[0]*t.shape[1],r=((t,e,o)=>{const r=[];for(let a=0;a<e;a+=o){const e=4*a,o=t[e+0],n=t[e+1],l=t[e+2],u=t[e+3];(void 0===u||u>=125)&&(o>250&&n>250&&l>250||r.push([o,n,l]))}return r})(Array.from(t.data),o,l.quality),n=e(r,l.colorCount),u=n?n.palette():null;a(u)}).catch(t=>{n(t)})})};module.exports={getColor:(t,e=10)=>new Promise((o,a)=>{r(t,5,e).then(t=>{o(t[0])}).catch(t=>{a(t)})}),getPalette:r};
|
||||
1
dist/color-thief-node.umd.js
vendored
Normal file
1
dist/color-thief-node.umd.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!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}});
|
||||
1
dist/color-thief.cjs
vendored
Normal file
1
dist/color-thief.cjs
vendored
Normal file
File diff suppressed because one or more lines are too long
174
dist/color-thief.js
vendored
174
dist/color-thief.js
vendored
@@ -1,17 +1,40 @@
|
||||
const getPixels = require('get-pixels');
|
||||
const quantize = require('@lokesh.dhakar/quantize');
|
||||
|
||||
function createPixelArray(imgData, pixelCount, quality) {
|
||||
const pixels = imgData;
|
||||
/**
|
||||
* Color Thief Node v3.0.0
|
||||
* by Lokesh Dhakar - http://www.lokeshdhakar.com
|
||||
*
|
||||
* Thanks
|
||||
* ------
|
||||
* Nick Rabinowitz - For creating quantize.js.
|
||||
* John Schulz - For clean up and optimization. @JFSIII
|
||||
* Nathan Spady - For adding drag and drop support to the demo page.
|
||||
*
|
||||
* License
|
||||
* -------
|
||||
* Copyright Lokesh Dhakar
|
||||
* Released under the MIT license
|
||||
* https://raw.githubusercontent.com/lokesh/color-thief/master/LICENSE
|
||||
*
|
||||
* @license MIT
|
||||
*/
|
||||
// Thanks to this PR for the work of migrating to ndarray-pixels pr https://github.com/lokesh/color-thief/pull/254
|
||||
import { getPixels } from 'ndarray-pixels';
|
||||
import quantize from '@lokesh.dhakar/quantize';
|
||||
import sharp from 'sharp';
|
||||
/**
|
||||
* Create an array of arrays of pixels from an array of pixels
|
||||
* @param {number[]} pixels An array of pixels
|
||||
* @param {number} pixelCount The total number of pixels
|
||||
* @param {number} quality
|
||||
* @returns {number[][]} Returns an array of arrays of pixel values ([ r, g, b ])
|
||||
*/
|
||||
const createPixelArray = (pixels, pixelCount, quality) => {
|
||||
const pixelArray = [];
|
||||
|
||||
for (let i = 0, offset, r, g, b, a; i < pixelCount; i = i + quality) {
|
||||
offset = i * 4;
|
||||
r = pixels[offset + 0];
|
||||
g = pixels[offset + 1];
|
||||
b = pixels[offset + 2];
|
||||
a = pixels[offset + 3];
|
||||
|
||||
for (let i = 0; i < pixelCount; i = i + quality) {
|
||||
const offset = i * 4;
|
||||
const r = pixels[offset + 0];
|
||||
const g = pixels[offset + 1];
|
||||
const b = pixels[offset + 2];
|
||||
const a = pixels[offset + 3];
|
||||
// If pixel is mostly opaque and not white
|
||||
if (typeof a === 'undefined' || a >= 125) {
|
||||
if (!(r > 250 && g > 250 && b > 250)) {
|
||||
@@ -20,80 +43,99 @@ function createPixelArray(imgData, pixelCount, quality) {
|
||||
}
|
||||
}
|
||||
return pixelArray;
|
||||
}
|
||||
|
||||
function validateOptions(options) {
|
||||
let { colorCount, quality } = options;
|
||||
|
||||
};
|
||||
/**
|
||||
* Validate Color-Thief options
|
||||
* @param {{ colorCount: number; quality: number; }} options The options object
|
||||
* @returns {{ colorCount: number, quality: number }} The same object, but validated
|
||||
*/
|
||||
const validateOptions = (options) => {
|
||||
let colorCount = options.colorCount;
|
||||
let quality = options.quality;
|
||||
if (typeof colorCount === 'undefined' || !Number.isInteger(colorCount)) {
|
||||
colorCount = 10;
|
||||
} else if (colorCount === 1 ) {
|
||||
}
|
||||
else if (colorCount === 1) {
|
||||
throw new Error('colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()');
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
colorCount = Math.max(colorCount, 2);
|
||||
colorCount = Math.min(colorCount, 20);
|
||||
}
|
||||
|
||||
if (typeof quality === 'undefined' || !Number.isInteger(quality) || quality < 1) {
|
||||
if (typeof quality === 'undefined' ||
|
||||
!Number.isInteger(quality) ||
|
||||
quality < 1) {
|
||||
quality = 10;
|
||||
}
|
||||
|
||||
return {
|
||||
colorCount,
|
||||
quality
|
||||
}
|
||||
}
|
||||
|
||||
function loadImg(img) {
|
||||
quality,
|
||||
};
|
||||
};
|
||||
/**
|
||||
* Load an image from the disk an pre-process
|
||||
* @param {string} img Path to the image on the disk
|
||||
* @returns {Promise<ndarray.NdArray<Uint8Array>>} Returns the pre-processed image
|
||||
*/
|
||||
const loadImg = (img) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
getPixels(img, function(err, data) {
|
||||
if(err) {
|
||||
reject(err)
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
})
|
||||
sharp(img)
|
||||
.toBuffer()
|
||||
.then((buffer) => sharp(buffer).metadata()
|
||||
.then((metadata) => ({ buffer, 'format': metadata.format })))
|
||||
.then(({ buffer, format }) => getPixels(buffer, format))
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
function getColor(img, quality) {
|
||||
};
|
||||
/**
|
||||
* Get the dominant color of an image
|
||||
* @param {string} img Path to the image on the disk
|
||||
* @param {number?} quality (Optional) 1 = highest quality, 10 = default. The bigger the number, the
|
||||
* faster a color will be returned but the greater the likelihood that it will not be the visually
|
||||
* most dominant color.
|
||||
* @returns {Promise<ColorThiefResult>} Returns the dominant color
|
||||
*/
|
||||
const getColor = (img, quality = 10) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
getPalette(img, 5, quality)
|
||||
.then(palette => {
|
||||
resolve(palette[0]);
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
})
|
||||
.then((palette) => {
|
||||
resolve(palette[0]);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function getPalette(img, colorCount = 10, quality = 10) {
|
||||
};
|
||||
/**
|
||||
* Get the color palette of an image
|
||||
* @param {string} img Path to the image on the disk
|
||||
* @param {number?} colorCount (Optional) the target amount of colors to try and extract
|
||||
* @param {number?} quality (Optional) 1 = highest quality, 10 = default. The bigger the number, the
|
||||
* faster a color will be returned but the greater the likelihood that it will not be the visually
|
||||
* most dominant color.
|
||||
* @returns {Promise<ColorThiefResult[]>} Returns an array of colors
|
||||
*/
|
||||
const getPalette = (img, colorCount = 10, quality = 10) => {
|
||||
const options = validateOptions({
|
||||
colorCount,
|
||||
quality
|
||||
quality,
|
||||
});
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
loadImg(img)
|
||||
.then(imgData => {
|
||||
const pixelCount = imgData.shape[0] * imgData.shape[1];
|
||||
const pixelArray = createPixelArray(imgData.data, pixelCount, options.quality);
|
||||
|
||||
const cmap = quantize(pixelArray, options.colorCount);
|
||||
const palette = cmap? cmap.palette() : null;
|
||||
|
||||
resolve(palette);
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
})
|
||||
.then((imgData) => {
|
||||
const pixelCount = imgData.shape[0] * imgData.shape[1];
|
||||
const pixelArray = createPixelArray(Array.from(imgData.data), pixelCount, options.quality);
|
||||
const cmap = quantize(pixelArray, options.colorCount);
|
||||
const palette = cmap ? cmap.palette() : null;
|
||||
resolve(palette);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
module.exports = {
|
||||
getColor,
|
||||
getPalette
|
||||
getPalette,
|
||||
};
|
||||
|
||||
|
||||
3
dist/color-thief.min.js
vendored
3
dist/color-thief.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/color-thief.mjs
vendored
2
dist/color-thief.mjs
vendored
File diff suppressed because one or more lines are too long
1
dist/color-thief.modern.js
vendored
Normal file
1
dist/color-thief.modern.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
dist/color-thief.modern.mjs
vendored
1
dist/color-thief.modern.mjs
vendored
File diff suppressed because one or more lines are too long
3
dist/color-thief.umd.js
vendored
3
dist/color-thief.umd.js
vendored
File diff suppressed because one or more lines are too long
1
dist/color-thief.umd.js.map
vendored
1
dist/color-thief.umd.js.map
vendored
@@ -1 +0,0 @@
|
||||
{"version":3,"file":"color-thief.umd.js","sources":["../build/build.js"],"sourcesContent":["var fs = require('fs');\r\nconst { resolve } = require('path');\r\n\r\n/*\r\ncolor-thief.umd.js duplicated as color-thief.min.js for legacy support\r\n\r\nIn Color Thief v2.1 <= there was one distribution file (dist/color-thief.min.js)\r\nand it exposed a global variable ColorThief. Starting from v2.2, the package\r\nincludes multiple dist files for the various module systems. One of these is\r\nthe UMD format which falls back to a global variable if the requirejs AMD format\r\nis not being used. This file is called color-thief.umd.js in the dist folder. We\r\nwant to keep supporting the previous users who were loading\r\ndist/color-thief.min.js and expecting a global var. For this reason we're\r\nduplicating the UMD compatible file and giving it that name.\r\n*/\r\n\r\nconst umdRelPath = 'dist/color-thief.umd.js';\r\nconst legacyRelPath = 'dist/color-thief.min.js';\r\n\r\nconst umdPath = resolve(process.cwd(), umdRelPath);\r\nconst legacyPath = resolve(process.cwd(), legacyRelPath);\r\n\r\nfs.copyFile(umdPath, legacyPath, (err) => {\r\n if (err) throw err;\r\n console.log(`${umdRelPath} copied to ${legacyRelPath}.`);\r\n});\r\n\r\nconst srcNodeRelPath = 'src/color-thief-node.js';\r\nconst distNodeRelPath = 'dist/color-thief.js';\r\nconst srcNodePath = resolve(process.cwd(), srcNodeRelPath);\r\nconst distNodePath = resolve(process.cwd(), distNodeRelPath);\r\n\r\nfs.copyFile(srcNodePath, distNodePath, (err) => {\r\n if (err) throw err;\r\n console.log(`${srcNodeRelPath} copied to ${distNodeRelPath}.`);\r\n});\r\n"],"names":["fs","require","umdPath","resolve","process","cwd","legacyPath","copyFile","err","console","log","const","srcNodePath","distNodePath"],"mappings":"0IAAA,IAAIA,EAAKC,QAAQ,QACGA,QAAQ,gBAkBtBC,EAAUC,EAAQC,QAAQC,MAHb,2BAIbC,EAAaH,EAAQC,QAAQC,MAHb,2BAKtBL,EAAGO,SAASL,EAASI,WAAaE,MAC1BA,EAAK,MAAMA,EACfC,QAAQC,oEAGZC,IAEMC,EAAcT,EAAQC,QAAQC,MAFb,2BAGjBQ,EAAeV,EAAQC,QAAQC,MAFb,uBAIxBL,EAAGO,SAASK,EAAaC,WAAeL,MAChCA,EAAK,MAAMA,EACfC,QAAQC"}
|
||||
1
dist/core.cjs
vendored
Normal file
1
dist/core.cjs
vendored
Normal file
@@ -0,0 +1 @@
|
||||
module.exports={createPixelArray:function(e,o,r){for(var t=[],n=0;n<o;n+=r){var a=4*n,i=e[a+0],l=e[a+1],u=e[a+2],c=e[a+3];(void 0===c||c>=125)&&(i>250&&l>250&&u>250||t.push([i,l,u]))}return t},validateOptions:function(e){var o=e.colorCount,r=e.quality;if(void 0!==o&&Number.isInteger(o)){if(1===o)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");o=Math.max(o,2),o=Math.min(o,20)}else o=10;return(void 0===r||!Number.isInteger(r)||r<1)&&(r=10),{colorCount:o,quality:r}}};
|
||||
1
dist/core.mjs
vendored
Normal file
1
dist/core.mjs
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var e={createPixelArray:function(e,o,r){for(var t=[],a=0;a<o;a+=r){var n=4*a,i=e[n+0],l=e[n+1],u=e[n+2],c=e[n+3];(void 0===c||c>=125)&&(i>250&&l>250&&u>250||t.push([i,l,u]))}return t},validateOptions:function(e){var o=e.colorCount,r=e.quality;if(void 0!==o&&Number.isInteger(o)){if(1===o)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");o=Math.max(o,2),o=Math.min(o,20)}else o=10;return(void 0===r||!Number.isInteger(r)||r<1)&&(r=10),{colorCount:o,quality:r}}};export{e as default};
|
||||
1
dist/core.modern.js
vendored
Normal file
1
dist/core.modern.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var e={createPixelArray:(e,t,o)=>{const r=[];for(let l=0;l<t;l+=o){const t=4*l,o=e[t+0],a=e[t+1],n=e[t+2],i=e[t+3];(void 0===i||i>=125)&&(o>250&&a>250&&n>250||r.push([o,a,n]))}return r},validateOptions:e=>{let t=e.colorCount,o=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===o||!Number.isInteger(o)||o<1)&&(o=10),{colorCount:t,quality:o}}};export{e as default};
|
||||
1
dist/core.umd.js
vendored
Normal file
1
dist/core.umd.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e||self).ColorThief=o()}(this,function(){return{createPixelArray:function(e,o,t){for(var n=[],r=0;r<o;r+=t){var i=4*r,l=e[i+0],u=e[i+1],f=e[i+2],a=e[i+3];(void 0===a||a>=125)&&(l>250&&u>250&&f>250||n.push([l,u,f]))}return n},validateOptions:function(e){var o=e.colorCount,t=e.quality;if(void 0!==o&&Number.isInteger(o)){if(1===o)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");o=Math.max(o,2),o=Math.min(o,20)}else o=10;return(void 0===t||!Number.isInteger(t)||t<1)&&(t=10),{colorCount:o,quality:t}}}});
|
||||
Reference in New Issue
Block a user