mirror of
https://github.com/janishutz/color-thief.git
synced 2025-11-25 05:44:24 +00:00
refactor: Use let and const instead of var where appropriate
This commit is contained in:
@@ -28,7 +28,7 @@ import core from './core.js';
|
|||||||
with a set of helper functions.
|
with a set of helper functions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var CanvasImage = function (image) {
|
const 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');
|
||||||
this.width = this.canvas.width = image.width;
|
this.width = this.canvas.width = image.width;
|
||||||
@@ -56,8 +56,8 @@ var ColorThief = function () {};
|
|||||||
*
|
*
|
||||||
* */
|
* */
|
||||||
ColorThief.prototype.getColor = function(sourceImage, quality = 10) {
|
ColorThief.prototype.getColor = function(sourceImage, quality = 10) {
|
||||||
var palette = this.getPalette(sourceImage, 5, quality);
|
const palette = this.getPalette(sourceImage, 5, quality);
|
||||||
var dominantColor = palette[0];
|
const dominantColor = palette[0];
|
||||||
return dominantColor;
|
return dominantColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -84,26 +84,26 @@ ColorThief.prototype.getPalette = function(sourceImage, colorCount, quality) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Create custom CanvasImage object
|
// Create custom CanvasImage object
|
||||||
var image = new CanvasImage(sourceImage);
|
const image = new CanvasImage(sourceImage);
|
||||||
var imageData = image.getImageData();
|
const imageData = image.getImageData();
|
||||||
var pixelCount = image.width * image.height;
|
const pixelCount = image.width * image.height;
|
||||||
|
|
||||||
const pixelArray = core.createPixelArray(imageData.data, pixelCount, options.quality);
|
const pixelArray = core.createPixelArray(imageData.data, pixelCount, options.quality);
|
||||||
|
|
||||||
// Send array to quantize function which clusters values
|
// Send array to quantize function which clusters values
|
||||||
// using median cut algorithm
|
// using median cut algorithm
|
||||||
var cmap = quantize(pixelArray, options.colorCount);
|
const cmap = quantize(pixelArray, options.colorCount);
|
||||||
var palette = cmap? cmap.palette() : null;
|
const palette = cmap? cmap.palette() : null;
|
||||||
|
|
||||||
return palette;
|
return palette;
|
||||||
};
|
};
|
||||||
|
|
||||||
ColorThief.prototype.getColorFromUrl = function(imageUrl, callback, quality) {
|
ColorThief.prototype.getColorFromUrl = function(imageUrl, callback, quality) {
|
||||||
let sourceImage = document.createElement("img");
|
const sourceImage = document.createElement("img");
|
||||||
var thief = this;
|
|
||||||
sourceImage.addEventListener('load' , function(){
|
sourceImage.addEventListener('load' , () => {
|
||||||
var palette = thief.getPalette(sourceImage, 5, quality);
|
const palette = this.getPalette(sourceImage, 5, quality);
|
||||||
var dominantColor = palette[0];
|
const dominantColor = palette[0];
|
||||||
callback(dominantColor, imageUrl);
|
callback(dominantColor, imageUrl);
|
||||||
});
|
});
|
||||||
sourceImage.src = imageUrl
|
sourceImage.src = imageUrl
|
||||||
@@ -119,7 +119,7 @@ ColorThief.prototype.getImageData = function(imageUrl, callback) {
|
|||||||
let uInt8Array = new Uint8Array(this.response);
|
let uInt8Array = new Uint8Array(this.response);
|
||||||
i = uInt8Array.length;
|
i = uInt8Array.length;
|
||||||
let binaryString = new Array(i);
|
let binaryString = new Array(i);
|
||||||
for (var i = 0; i < uInt8Array.length; i++){
|
for (let i = 0; i < uInt8Array.length; i++){
|
||||||
binaryString[i] = String.fromCharCode(uInt8Array[i]);
|
binaryString[i] = String.fromCharCode(uInt8Array[i]);
|
||||||
}
|
}
|
||||||
let data = binaryString.join('');
|
let data = binaryString.join('');
|
||||||
@@ -131,12 +131,12 @@ ColorThief.prototype.getImageData = function(imageUrl, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ColorThief.prototype.getColorAsync = function(imageUrl, callback, quality) {
|
ColorThief.prototype.getColorAsync = function(imageUrl, callback, quality) {
|
||||||
var thief = this;
|
const thief = this;
|
||||||
this.getImageData(imageUrl, function(imageData){
|
this.getImageData(imageUrl, function(imageData){
|
||||||
let sourceImage = document.createElement("img");
|
const sourceImage = document.createElement("img");
|
||||||
sourceImage.addEventListener('load' , function(){
|
sourceImage.addEventListener('load' , function(){
|
||||||
var palette = thief.getPalette(sourceImage, 5, quality);
|
const palette = thief.getPalette(sourceImage, 5, quality);
|
||||||
var dominantColor = palette[0];
|
const dominantColor = palette[0];
|
||||||
callback(dominantColor, this);
|
callback(dominantColor, this);
|
||||||
});
|
});
|
||||||
sourceImage.src = imageData;
|
sourceImage.src = imageData;
|
||||||
|
|||||||
Reference in New Issue
Block a user