diff --git a/src/color-thief-node.js b/src/color-thief-node.js index 3119dd4..ac57fb0 100644 --- a/src/color-thief-node.js +++ b/src/color-thief-node.js @@ -1,9 +1,8 @@ const getPixels = require('get-pixels'); const quantize = require('quantize'); +const core = require('./core-node.js'); -const ColorThief = function () {}; - -function readImg(img) { +function loadImg(img) { return new Promise((resolve, reject) => { getPixels(img, function(err, data) { if(err) { @@ -15,22 +14,6 @@ function readImg(img) { }); } -function createPixelArray(imgData, quality) { - const pixels = imgData.data; - const pixelCount = imgData.shape[0] * imgData.shape[1]; - 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]; - pixelArray.push([r, g, b]); - } - return pixelArray; -} - function getColor(img, quality) { return new Promise((resolve, reject) => { getPalette(img, 5, quality) @@ -46,11 +29,14 @@ function getColor(img, quality) { function getPalette(img, colorCount = 10, quality = 10) { return new Promise((resolve, reject) => { - readImg(img) + loadImg(img) .then(imgData => { - const pixelArray = createPixelArray(imgData, quality); + const pixelCount = imgData.shape[0] * imgData.shape[1]; + const pixelArray = core.createPixelArray(imgData.data, pixelCount, quality); + const cmap = quantize(pixelArray, colorCount); const palette = cmap? cmap.palette() : null; + resolve(palette); }) .catch(err => { diff --git a/src/color-thief.js b/src/color-thief.js index d355bce..0728d7a 100644 --- a/src/color-thief.js +++ b/src/color-thief.js @@ -1,4 +1,5 @@ import quantize from '../node_modules/quantize/dist/index.mjs'; +import core from './core.js'; /* * Color Thief v2.2.0 @@ -60,7 +61,7 @@ var ColorThief = function () {}; * most dominant color. * * */ -ColorThief.prototype.getColor = function(sourceImage, quality) { +ColorThief.prototype.getColor = function(sourceImage, quality = 10) { var palette = this.getPalette(sourceImage, 5, quality); var dominantColor = palette[0]; return dominantColor; @@ -99,21 +100,7 @@ ColorThief.prototype.getPalette = function(sourceImage, colorCount, quality) { var pixels = imageData.data; var pixelCount = image.getPixelCount(); - // Store the RGB values in an array format suitable for quantize function - var pixelArray = []; - for (var 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]; - // If pixel is mostly opaque and not white - if (a >= 125) { - if (!(r > 250 && g > 250 && b > 250)) { - pixelArray.push([r, g, b]); - } - } - } + const pixelArray = core.createPixelArray(imageData, pixelCount, quality); // Send array to quantize function which clusters values // using median cut algorithm diff --git a/src/core-node.js b/src/core-node.js new file mode 100644 index 0000000..ab3b71f --- /dev/null +++ b/src/core-node.js @@ -0,0 +1,24 @@ +function createPixelArray(imgData, pixelCount, quality) { + const pixels = imgData; + 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]; + + // If pixel is mostly opaque and not white + if (typeof a === 'undefined' || a >= 125) { + if (!(r > 250 && g > 250 && b > 250)) { + pixelArray.push([r, g, b]); + } + } + } + return pixelArray; +} + +module.exports = { + createPixelArray +}; diff --git a/src/core.js b/src/core.js new file mode 100644 index 0000000..6cdc4bd --- /dev/null +++ b/src/core.js @@ -0,0 +1,24 @@ +function createPixelArray(imgData, pixelCount, quality) { + const pixels = imgData; + 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]; + + // If pixel is mostly opaque and not white + if (typeof a === 'undefined' || a >= 125) { + if (!(r > 250 && g > 250 && b > 250)) { + pixelArray.push([r, g, b]); + } + } + } + return pixelArray; +} + +export default { + createPixelArray, +}