mirror of
https://github.com/janishutz/color-thief.git
synced 2025-11-26 06:14:25 +00:00
refactor: Exploring idea of shared core lib
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user