refactor: Exploring idea of shared core lib

This commit is contained in:
Lokesh Dhakar
2019-07-21 21:54:25 -07:00
parent 71e007c1ec
commit e9b323a6b2
4 changed files with 58 additions and 37 deletions

View File

@@ -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