mirror of
https://github.com/janishutz/color-thief.git
synced 2025-11-25 22:04:25 +00:00
style: Fixing var declarations and aligning to eslint rules
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
It also simplifies some of the canvas context manipulation
|
It also simplifies some of the canvas context manipulation
|
||||||
with a set of helper functions.
|
with a set of helper functions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var CanvasImage = function (image) {
|
var CanvasImage = function (image) {
|
||||||
this.canvas = document.createElement('canvas');
|
this.canvas = document.createElement('canvas');
|
||||||
this.context = this.canvas.getContext('2d');
|
this.context = this.canvas.getContext('2d');
|
||||||
@@ -121,7 +122,7 @@ ColorThief.prototype.getPalette = function(sourceImage, colorCount, quality) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ColorThief.prototype.getColorFromUrl = function(imageUrl, callback, quality) {
|
ColorThief.prototype.getColorFromUrl = function(imageUrl, callback, quality) {
|
||||||
sourceImage = document.createElement("img");
|
let sourceImage = document.createElement("img");
|
||||||
var thief = this;
|
var thief = this;
|
||||||
sourceImage.addEventListener('load' , function(){
|
sourceImage.addEventListener('load' , function(){
|
||||||
var palette = thief.getPalette(sourceImage, 5, quality);
|
var palette = thief.getPalette(sourceImage, 5, quality);
|
||||||
@@ -133,19 +134,19 @@ ColorThief.prototype.getColorFromUrl = function(imageUrl, callback, quality) {
|
|||||||
|
|
||||||
|
|
||||||
ColorThief.prototype.getImageData = function(imageUrl, callback) {
|
ColorThief.prototype.getImageData = function(imageUrl, callback) {
|
||||||
xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', imageUrl, true);
|
xhr.open('GET', imageUrl, true);
|
||||||
xhr.responseType = 'arraybuffer'
|
xhr.responseType = 'arraybuffer'
|
||||||
xhr.onload = function(e) {
|
xhr.onload = function() {
|
||||||
if (this.status == 200) {
|
if (this.status == 200) {
|
||||||
uInt8Array = new Uint8Array(this.response)
|
let uInt8Array = new Uint8Array(this.response)
|
||||||
i = uInt8Array.length
|
i = uInt8Array.length
|
||||||
binaryString = new Array(i);
|
let binaryString = new Array(i);
|
||||||
for (var i = 0; i < uInt8Array.length; i++){
|
for (var i = 0; i < uInt8Array.length; i++){
|
||||||
binaryString[i] = String.fromCharCode(uInt8Array[i])
|
binaryString[i] = String.fromCharCode(uInt8Array[i])
|
||||||
}
|
}
|
||||||
data = binaryString.join('')
|
let data = binaryString.join('')
|
||||||
base64 = window.btoa(data)
|
let base64 = window.btoa(data)
|
||||||
callback ("data:image/png;base64,"+base64)
|
callback ("data:image/png;base64,"+base64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,7 +156,7 @@ ColorThief.prototype.getImageData = function(imageUrl, callback) {
|
|||||||
ColorThief.prototype.getColorAsync = function(imageUrl, callback, quality) {
|
ColorThief.prototype.getColorAsync = function(imageUrl, callback, quality) {
|
||||||
var thief = this;
|
var thief = this;
|
||||||
this.getImageData(imageUrl, function(imageData){
|
this.getImageData(imageUrl, function(imageData){
|
||||||
sourceImage = document.createElement("img");
|
let sourceImage = document.createElement("img");
|
||||||
sourceImage.addEventListener('load' , function(){
|
sourceImage.addEventListener('load' , function(){
|
||||||
var palette = thief.getPalette(sourceImage, 5, quality);
|
var palette = thief.getPalette(sourceImage, 5, quality);
|
||||||
var dominantColor = palette[0];
|
var dominantColor = palette[0];
|
||||||
@@ -225,10 +226,10 @@ var newPixels = myPixels.map(function(p) {
|
|||||||
*/
|
*/
|
||||||
var MMCQ = (function() {
|
var MMCQ = (function() {
|
||||||
// private constants
|
// private constants
|
||||||
var sigbits = 5,
|
var sigbits = 5;
|
||||||
rshift = 8 - sigbits,
|
var rshift = 8 - sigbits;
|
||||||
maxIterations = 1000,
|
var maxIterations = 1000;
|
||||||
fractByPopulations = 0.75;
|
var fractByPopulations = 0.75;
|
||||||
|
|
||||||
// get reduced-space color index for a pixel
|
// get reduced-space color index for a pixel
|
||||||
function getColorIndex(r, g, b) {
|
function getColorIndex(r, g, b) {
|
||||||
@@ -237,8 +238,8 @@ var MMCQ = (function() {
|
|||||||
|
|
||||||
// Simple priority queue
|
// Simple priority queue
|
||||||
function PQueue(comparator) {
|
function PQueue(comparator) {
|
||||||
var contents = [],
|
var contents = [];
|
||||||
sorted = false;
|
var sorted = false;
|
||||||
|
|
||||||
function sort() {
|
function sort() {
|
||||||
contents.sort(comparator);
|
contents.sort(comparator);
|
||||||
@@ -292,11 +293,11 @@ var MMCQ = (function() {
|
|||||||
return vbox._volume;
|
return vbox._volume;
|
||||||
},
|
},
|
||||||
count: function(force) {
|
count: function(force) {
|
||||||
var vbox = this,
|
var vbox = this;
|
||||||
histo = vbox.histo;
|
var histo = vbox.histo;
|
||||||
if (!vbox._count_set || force) {
|
if (!vbox._count_set || force) {
|
||||||
var npix = 0,
|
var npix = 0;
|
||||||
index, i, j, k;
|
var index; var i; var j; var k;
|
||||||
for (i = vbox.r1; i <= vbox.r2; i++) {
|
for (i = vbox.r1; i <= vbox.r2; i++) {
|
||||||
for (j = vbox.g1; j <= vbox.g2; j++) {
|
for (j = vbox.g1; j <= vbox.g2; j++) {
|
||||||
for (k = vbox.b1; k <= vbox.b2; k++) {
|
for (k = vbox.b1; k <= vbox.b2; k++) {
|
||||||
@@ -315,16 +316,17 @@ var MMCQ = (function() {
|
|||||||
return new VBox(vbox.r1, vbox.r2, vbox.g1, vbox.g2, vbox.b1, vbox.b2, vbox.histo);
|
return new VBox(vbox.r1, vbox.r2, vbox.g1, vbox.g2, vbox.b1, vbox.b2, vbox.histo);
|
||||||
},
|
},
|
||||||
avg: function(force) {
|
avg: function(force) {
|
||||||
var vbox = this,
|
var vbox = this;
|
||||||
histo = vbox.histo;
|
var histo = vbox.histo;
|
||||||
if (!vbox._avg || force) {
|
if (!vbox._avg || force) {
|
||||||
var ntot = 0,
|
var ntot = 0;
|
||||||
mult = 1 << (8 - sigbits),
|
var mult = 1 << (8 - sigbits);
|
||||||
rsum = 0,
|
var rsum = 0;
|
||||||
gsum = 0,
|
var gsum = 0;
|
||||||
bsum = 0,
|
var bsum = 0;
|
||||||
hval,
|
var hval;
|
||||||
i, j, k, histoindex;
|
var i, j, k;
|
||||||
|
var histoindex;
|
||||||
for (i = vbox.r1; i <= vbox.r2; i++) {
|
for (i = vbox.r1; i <= vbox.r2; i++) {
|
||||||
for (j = vbox.g1; j <= vbox.g2; j++) {
|
for (j = vbox.g1; j <= vbox.g2; j++) {
|
||||||
for (k = vbox.b1; k <= vbox.b2; k++) {
|
for (k = vbox.b1; k <= vbox.b2; k++) {
|
||||||
@@ -340,7 +342,7 @@ var MMCQ = (function() {
|
|||||||
if (ntot) {
|
if (ntot) {
|
||||||
vbox._avg = [~~(rsum/ntot), ~~(gsum/ntot), ~~(bsum/ntot)];
|
vbox._avg = [~~(rsum/ntot), ~~(gsum/ntot), ~~(bsum/ntot)];
|
||||||
} else {
|
} else {
|
||||||
// console.log('empty box');
|
// console.log('empty box');
|
||||||
vbox._avg = [
|
vbox._avg = [
|
||||||
~~(mult * (vbox.r1 + vbox.r2 + 1) / 2),
|
~~(mult * (vbox.r1 + vbox.r2 + 1) / 2),
|
||||||
~~(mult * (vbox.g1 + vbox.g2 + 1) / 2),
|
~~(mult * (vbox.g1 + vbox.g2 + 1) / 2),
|
||||||
@@ -351,10 +353,10 @@ var MMCQ = (function() {
|
|||||||
return vbox._avg;
|
return vbox._avg;
|
||||||
},
|
},
|
||||||
contains: function(pixel) {
|
contains: function(pixel) {
|
||||||
var vbox = this,
|
var vbox = this;
|
||||||
rval = pixel[0] >> rshift,
|
var rval = pixel[0] >> rshift;
|
||||||
gval = pixel[1] >> rshift,
|
var gval = pixel[1] >> rshift;
|
||||||
bval = pixel[2] >> rshift;
|
var bval = pixel[2] >> rshift;
|
||||||
return (rval >= vbox.r1 && rval <= vbox.r2 &&
|
return (rval >= vbox.r1 && rval <= vbox.r2 &&
|
||||||
gval >= vbox.g1 && gval <= vbox.g2 &&
|
gval >= vbox.g1 && gval <= vbox.g2 &&
|
||||||
bval >= vbox.b1 && bval <= vbox.b2);
|
bval >= vbox.b1 && bval <= vbox.b2);
|
||||||
@@ -393,8 +395,10 @@ var MMCQ = (function() {
|
|||||||
return this.nearest(color);
|
return this.nearest(color);
|
||||||
},
|
},
|
||||||
nearest: function(color) {
|
nearest: function(color) {
|
||||||
var vboxes = this.vboxes,
|
var vboxes = this.vboxes;
|
||||||
d1, d2, pColor;
|
var d1;
|
||||||
|
var d2;
|
||||||
|
var pColor;
|
||||||
for (var i=0; i<vboxes.size(); i++) {
|
for (var i=0; i<vboxes.size(); i++) {
|
||||||
d2 = Math.sqrt(
|
d2 = Math.sqrt(
|
||||||
Math.pow(color[0] - vboxes.peek(i).color[0], 2) +
|
Math.pow(color[0] - vboxes.peek(i).color[0], 2) +
|
||||||
@@ -419,8 +423,8 @@ var MMCQ = (function() {
|
|||||||
vboxes[0].color = [0,0,0];
|
vboxes[0].color = [0,0,0];
|
||||||
|
|
||||||
// force lightest color to white if everything > 251
|
// force lightest color to white if everything > 251
|
||||||
var idx = vboxes.length-1,
|
var idx = vboxes.length-1;
|
||||||
highest = vboxes[idx].color;
|
var highest = vboxes[idx].color;
|
||||||
if (highest[0] > 251 && highest[1] > 251 && highest[2] > 251)
|
if (highest[0] > 251 && highest[1] > 251 && highest[2] > 251)
|
||||||
vboxes[idx].color = [255,255,255];
|
vboxes[idx].color = [255,255,255];
|
||||||
}
|
}
|
||||||
@@ -429,9 +433,9 @@ var MMCQ = (function() {
|
|||||||
// histo (1-d array, giving the number of pixels in
|
// histo (1-d array, giving the number of pixels in
|
||||||
// each quantized region of color space), or null on error
|
// each quantized region of color space), or null on error
|
||||||
function getHisto(pixels) {
|
function getHisto(pixels) {
|
||||||
var histosize = 1 << (3 * sigbits),
|
var histosize = 1 << (3 * sigbits);
|
||||||
histo = new Array(histosize),
|
var histo = new Array(histosize);
|
||||||
index, rval, gval, bval;
|
var index; var rval; var gval; var bval;
|
||||||
pixels.forEach(function(pixel) {
|
pixels.forEach(function(pixel) {
|
||||||
rval = pixel[0] >> rshift;
|
rval = pixel[0] >> rshift;
|
||||||
gval = pixel[1] >> rshift;
|
gval = pixel[1] >> rshift;
|
||||||
@@ -443,10 +447,10 @@ var MMCQ = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function vboxFromPixels(pixels, histo) {
|
function vboxFromPixels(pixels, histo) {
|
||||||
var rmin=1000000, rmax=0,
|
var rmin=1000000; var rmax=0;
|
||||||
gmin=1000000, gmax=0,
|
var gmin=1000000; var gmax=0;
|
||||||
bmin=1000000, bmax=0,
|
var bmin=1000000; var bmax=0;
|
||||||
rval, gval, bval;
|
var rval; var gval; var bval;
|
||||||
// find min/max
|
// find min/max
|
||||||
pixels.forEach(function(pixel) {
|
pixels.forEach(function(pixel) {
|
||||||
rval = pixel[0] >> rshift;
|
rval = pixel[0] >> rshift;
|
||||||
@@ -465,19 +469,19 @@ var MMCQ = (function() {
|
|||||||
function medianCutApply(histo, vbox) {
|
function medianCutApply(histo, vbox) {
|
||||||
if (!vbox.count()) return;
|
if (!vbox.count()) return;
|
||||||
|
|
||||||
var rw = vbox.r2 - vbox.r1 + 1,
|
var rw = vbox.r2 - vbox.r1 + 1;
|
||||||
gw = vbox.g2 - vbox.g1 + 1,
|
var gw = vbox.g2 - vbox.g1 + 1;
|
||||||
bw = vbox.b2 - vbox.b1 + 1,
|
var bw = vbox.b2 - vbox.b1 + 1;
|
||||||
maxw = pv.max([rw, gw, bw]);
|
var maxw = pv.max([rw, gw, bw]);
|
||||||
// only one pixel, no split
|
// only one pixel, no split
|
||||||
if (vbox.count() == 1) {
|
if (vbox.count() == 1) {
|
||||||
return [vbox.copy()];
|
return [vbox.copy()];
|
||||||
}
|
}
|
||||||
/* Find the partial sum arrays along the selected axis. */
|
/* Find the partial sum arrays along the selected axis. */
|
||||||
var total = 0,
|
var total = 0;
|
||||||
partialsum = [],
|
var partialsum = [];
|
||||||
lookaheadsum = [],
|
var lookaheadsum = [];
|
||||||
i, j, k, sum, index;
|
var i; var j; var k; var sum; var index;
|
||||||
if (maxw == rw) {
|
if (maxw == rw) {
|
||||||
for (i = vbox.r1; i <= vbox.r2; i++) {
|
for (i = vbox.r1; i <= vbox.r2; i++) {
|
||||||
sum = 0;
|
sum = 0;
|
||||||
@@ -521,9 +525,9 @@ var MMCQ = (function() {
|
|||||||
lookaheadsum[i] = total-d;
|
lookaheadsum[i] = total-d;
|
||||||
});
|
});
|
||||||
function doCut(color) {
|
function doCut(color) {
|
||||||
var dim1 = color + '1',
|
var dim1 = color + '1';
|
||||||
dim2 = color + '2',
|
var dim2 = color + '2';
|
||||||
left, right, vbox1, vbox2, d2, count2=0;
|
var left; var right; var vbox1; var vbox2; var d2; var count2=0;
|
||||||
for (i = vbox[dim1]; i <= vbox[dim2]; i++) {
|
for (i = vbox[dim1]; i <= vbox[dim2]; i++) {
|
||||||
if (partialsum[i] > total / 2) {
|
if (partialsum[i] > total / 2) {
|
||||||
vbox1 = vbox.copy();
|
vbox1 = vbox.copy();
|
||||||
@@ -561,8 +565,8 @@ var MMCQ = (function() {
|
|||||||
|
|
||||||
// XXX: check color content and convert to grayscale if insufficient
|
// XXX: check color content and convert to grayscale if insufficient
|
||||||
|
|
||||||
var histo = getHisto(pixels),
|
var histo = getHisto(pixels);
|
||||||
histosize = 1 << (3 * sigbits);
|
// histosize = 1 << (3 * sigbits);
|
||||||
|
|
||||||
// check that we aren't below maxcolors already
|
// check that we aren't below maxcolors already
|
||||||
var nColors = 0;
|
var nColors = 0;
|
||||||
@@ -572,15 +576,15 @@ var MMCQ = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get the beginning vbox from the colors
|
// get the beginning vbox from the colors
|
||||||
var vbox = vboxFromPixels(pixels, histo),
|
var vbox = vboxFromPixels(pixels, histo);
|
||||||
pq = new PQueue(function(a,b) { return pv.naturalOrder(a.count(), b.count()); });
|
var pq = new PQueue(function(a,b) { return pv.naturalOrder(a.count(), b.count()); });
|
||||||
pq.push(vbox);
|
pq.push(vbox);
|
||||||
|
|
||||||
// inner function to do the iteration
|
// inner function to do the iteration
|
||||||
function iter(lh, target) {
|
function iter(lh, target) {
|
||||||
var ncolors = 1,
|
var ncolors = 1;
|
||||||
niters = 0,
|
var niters = 0;
|
||||||
vbox;
|
var vbox;
|
||||||
while (niters < maxIterations) {
|
while (niters < maxIterations) {
|
||||||
vbox = lh.pop();
|
vbox = lh.pop();
|
||||||
if (!vbox.count()) { /* just put it back */
|
if (!vbox.count()) { /* just put it back */
|
||||||
@@ -589,9 +593,9 @@ var MMCQ = (function() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// do the cut
|
// do the cut
|
||||||
var vboxes = medianCutApply(histo, vbox),
|
var vboxes = medianCutApply(histo, vbox);
|
||||||
vbox1 = vboxes[0],
|
var vbox1 = vboxes[0];
|
||||||
vbox2 = vboxes[1];
|
var vbox2 = vboxes[1];
|
||||||
|
|
||||||
if (!vbox1) {
|
if (!vbox1) {
|
||||||
// console.log("vbox1 not defined; shouldn't happen!");
|
// console.log("vbox1 not defined; shouldn't happen!");
|
||||||
|
|||||||
Reference in New Issue
Block a user