From 85d197baf575920d87cc8da0544309695672b91a Mon Sep 17 00:00:00 2001 From: Lokesh Dhakar Date: Sat, 3 Aug 2019 23:09:08 -0700 Subject: [PATCH] build: Package core functions in ct-node for now. No build process. Revisit post release --- src/color-thief-node.js | 50 ++++++++++++++++++++++++++++++++++++++--- src/core-node.js | 49 ---------------------------------------- 2 files changed, 47 insertions(+), 52 deletions(-) delete mode 100644 src/core-node.js diff --git a/src/color-thief-node.js b/src/color-thief-node.js index d0e6af4..2833cec 100644 --- a/src/color-thief-node.js +++ b/src/color-thief-node.js @@ -1,6 +1,50 @@ const getPixels = require('get-pixels'); const quantize = require('quantize'); -const core = require('./core-node.js'); + +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; +} + +function validateOptions(options) { + let { colorCount, quality } = options; + + if (typeof colorCount === 'undefined' || !Number.isInteger(colorCount)) { + colorCount = 10; + } else if (colorCount === 1 ) { + throw new Error('colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()'); + } else { + colorCount = Math.max(colorCount, 2); + colorCount = Math.min(colorCount, 20); + } + + if (typeof quality === 'undefined' || Number.isInteger(quality)) { + quality = 10; + } else if (quality < 1) { + quality = 10; + } + + return { + colorCount, + quality + } +} function loadImg(img) { return new Promise((resolve, reject) => { @@ -28,7 +72,7 @@ function getColor(img, quality) { } function getPalette(img, colorCount = 10, quality = 10) { - const options = core.validateOptions({ + const options = validateOptions({ colorCount, quality }); @@ -37,7 +81,7 @@ function getPalette(img, colorCount = 10, quality = 10) { loadImg(img) .then(imgData => { const pixelCount = imgData.shape[0] * imgData.shape[1]; - const pixelArray = core.createPixelArray(imgData.data, pixelCount, options.quality); + const pixelArray = createPixelArray(imgData.data, pixelCount, options.quality); const cmap = quantize(pixelArray, options.colorCount); const palette = cmap? cmap.palette() : null; diff --git a/src/core-node.js b/src/core-node.js deleted file mode 100644 index 7ade2cf..0000000 --- a/src/core-node.js +++ /dev/null @@ -1,49 +0,0 @@ -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; -} - -function validateOptions(options) { - let { colorCount, quality } = options; - - if (typeof colorCount === 'undefined' || !Number.isInteger(colorCount)) { - colorCount = 10; - } else if (colorCount === 1 ) { - throw new Error('colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()'); - } else { - colorCount = Math.max(colorCount, 2); - colorCount = Math.min(colorCount, 20); - } - - if (typeof quality === 'undefined' || Number.isInteger(quality)) { - quality = 10; - } else if (quality < 1) { - quality = 10; - } - - return { - colorCount, - quality - } -} - -module.exports = { - createPixelArray, - validateOptions -};