mirror of
https://github.com/janishutz/color-thief.git
synced 2025-11-27 06:44:25 +00:00
done
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import quantize from '../node_modules/@lokesh.dhakar/quantize/dist/index.mjs';
|
||||
import core from './core.js';
|
||||
|
||||
/*
|
||||
* Color Thief v2.4.0
|
||||
/**
|
||||
* Color Thief v3.0.0
|
||||
* by Lokesh Dhakar - http://www.lokeshdhakar.com
|
||||
*
|
||||
* Thanks
|
||||
@@ -17,50 +17,61 @@ import core from './core.js';
|
||||
* Released under the MIT license
|
||||
* https://raw.githubusercontent.com/lokesh/color-thief/master/LICENSE
|
||||
*
|
||||
* @license
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
/*
|
||||
CanvasImage Class
|
||||
Class that wraps the html image element and canvas.
|
||||
/**
|
||||
@class CanvasImage that wraps the html image element and canvas.
|
||||
It also simplifies some of the canvas context manipulation
|
||||
with a set of helper functions.
|
||||
*/
|
||||
class CanvasImage {
|
||||
canvas: HTMLCanvasElement;
|
||||
|
||||
const CanvasImage = function (image: HTMLImageElement) {
|
||||
this.canvas = document.createElement('canvas');
|
||||
this.context = this.canvas.getContext('2d');
|
||||
this.width = this.canvas.width = image.naturalWidth;
|
||||
this.height = this.canvas.height = image.naturalHeight;
|
||||
this.context.drawImage(image, 0, 0, this.width, this.height);
|
||||
};
|
||||
context: CanvasRenderingContext2D;
|
||||
|
||||
CanvasImage.prototype.getImageData = function () {
|
||||
return this.context.getImageData(0, 0, this.width, this.height);
|
||||
};
|
||||
width: number;
|
||||
|
||||
interface ColorThiefResult {
|
||||
r: number;
|
||||
g: number;
|
||||
b: number;
|
||||
height: number;
|
||||
|
||||
constructor ( image: HTMLImageElement ) {
|
||||
this.canvas = document.createElement( 'canvas' );
|
||||
this.context = this.canvas.getContext( '2d' );
|
||||
this.width = this.canvas.width = image.naturalWidth;
|
||||
this.height = this.canvas.height = image.naturalHeight;
|
||||
this.context.drawImage( image, 0, 0, this.width, this.height );
|
||||
}
|
||||
|
||||
getImageData = function () {
|
||||
return this.context.getImageData( 0, 0, this.width, this.height );
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
interface ColorThiefResult {
|
||||
'r': number;
|
||||
'g': number;
|
||||
'b': number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @class The main ColorThief class
|
||||
*/
|
||||
class ColorThief {
|
||||
/**
|
||||
* Use the median cut algorithm provided by quantize.js to cluster similar
|
||||
* colors and return the base color from the largest cluster.
|
||||
* @param {HTMLImageElement} sourceImage:HTMLImageElement
|
||||
* @param {number} quality Quality is an optional argument. It needs to be an integer. 1 is the highest quality settings.
|
||||
* 10 is the default. There is a trade-off between quality and speed. The bigger the number, the
|
||||
* @param {number?} quality (Optional) 1 = highest quality, 10 = default. The bigger the number, the
|
||||
* faster a color will be returned but the greater the likelihood that it will not be the visually
|
||||
* most dominant color.
|
||||
* @returns {ColorThiefResult} returns {r: num, g: num, b: num}
|
||||
*/
|
||||
getColor(
|
||||
getColor (
|
||||
sourceImage: HTMLImageElement,
|
||||
quality: number = 10,
|
||||
): ColorThiefResult {
|
||||
const palette = this.getPalette(sourceImage, 5, quality);
|
||||
const palette = this.getPalette( sourceImage, 5, quality );
|
||||
return palette[0];
|
||||
}
|
||||
|
||||
@@ -69,23 +80,23 @@ class ColorThief {
|
||||
* @param {HTMLImageElement} sourceImage The image you want to have processed, as an HTMLImageElement
|
||||
* @param {number?} colorCount colorCount determines the size of the palette; the number of colors returned. If not set, it
|
||||
* defaults to 10.
|
||||
* @param {number?} quality quality is an optional argument. It needs to be an integer. 1 is the highest quality settings.
|
||||
* 10 is the default. There is a trade-off between quality and speed. The bigger the number, the
|
||||
* faster the palette generation but the greater the likelihood that colors will be missed.
|
||||
* @returns {ColorThiefResult[]} returns array[ {r: num, g: num, b: num}, {r: num, g: num, b: num}, ...]
|
||||
* @param {number?} quality (Optional) 1 = highest quality, 10 = default. The bigger the number, the
|
||||
* faster a color will be returned but the greater the likelihood that it will not be the visually
|
||||
* most dominant color.
|
||||
* @returns {ColorThiefResult[] | null} returns array[ {r: num, g: num, b: num}, {r: num, g: num, b: num}, ...]
|
||||
*/
|
||||
getPalette(
|
||||
getPalette (
|
||||
sourceImage: HTMLImageElement,
|
||||
colorCount?: number,
|
||||
quality: number = 10,
|
||||
): ColorThiefResult[] {
|
||||
const options = core.validateOptions({
|
||||
): ColorThiefResult[] | null {
|
||||
const options = core.validateOptions( {
|
||||
colorCount,
|
||||
quality,
|
||||
});
|
||||
} );
|
||||
|
||||
// Create custom CanvasImage object
|
||||
const image = new CanvasImage(sourceImage);
|
||||
const image = new CanvasImage( sourceImage );
|
||||
const imageData = image.getImageData();
|
||||
const pixelCount = image.width * image.height;
|
||||
|
||||
@@ -97,83 +108,127 @@ class ColorThief {
|
||||
|
||||
// Send array to quantize function which clusters values
|
||||
// using median cut algorithm
|
||||
const cmap = quantize(pixelArray, options.colorCount);
|
||||
const palette = cmap ? cmap.palette() : null;
|
||||
const cmap = quantize( pixelArray, options.colorCount );
|
||||
const palette: null | ColorThiefResult[] = cmap ? cmap.palette() : null;
|
||||
|
||||
return palette;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
* @param {any} imageUrl
|
||||
* @param {any} callback
|
||||
* @param {any} quality
|
||||
* @returns {any}
|
||||
* [ DEPRECATED ] Get the dominant color of an image, which is fetched from a URL.
|
||||
* @param {string} imageUrl
|
||||
* @param {( color: ColorThiefResult, url: string ) => void} callback The callback function called when the image has finished processing
|
||||
* @param {number?} quality (Optional) 1 = highest quality, 10 = default. The bigger the number, the
|
||||
* faster a color will be returned but the greater the likelihood that it will not be the visually
|
||||
* most dominant color.
|
||||
* @returns {void}
|
||||
* @deprecated since Version 3.0, use getColorFromURLPromise instead
|
||||
*/
|
||||
getColorFromUrl(imageUrl, callback, quality) {
|
||||
const sourceImage = document.createElement('img');
|
||||
getColorFromUrl ( imageUrl: string, callback: ( color: ColorThiefResult, url: string ) => void, quality: number = 10 ): void {
|
||||
const sourceImage = document.createElement( 'img' );
|
||||
|
||||
sourceImage.addEventListener('load', () => {
|
||||
const palette = this.getPalette(sourceImage, 5, quality);
|
||||
sourceImage.addEventListener( 'load', () => {
|
||||
const palette = this.getPalette( sourceImage, 5, quality );
|
||||
const dominantColor = palette[0];
|
||||
callback(dominantColor, imageUrl);
|
||||
});
|
||||
callback( dominantColor, imageUrl );
|
||||
} );
|
||||
sourceImage.src = imageUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
* @param {string} imageUrl
|
||||
* @param {string} callback
|
||||
* [ DEPRECATED ] Get Image Data as a Base64 string from a URL
|
||||
* @param {string} imageUrl The URL to the image. Has to be a full URL, if not relative
|
||||
* @param {( data: string ) => void} callback The callback function called, once download is complete. Will only be called if successful at downloading
|
||||
* @returns {void}
|
||||
* @deprecated since Version 3.0, use getImageDataFromURL instead
|
||||
*/
|
||||
getImageData(imageUrl: string, callback) {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', imageUrl, true);
|
||||
xhr.responseType = 'arraybuffer';
|
||||
|
||||
fetch(imageUrl).then((res) => {
|
||||
if (res.status === 200) {
|
||||
res.arrayBuffer().then((response) => {
|
||||
const uInt8Array = new Uint8Array(response);
|
||||
let binaryString = new Array(uInt8Array.length);
|
||||
for (let i = 0; i < uInt8Array.length; i++) {
|
||||
binaryString[i] = String.fromCharCode(uInt8Array[i]);
|
||||
getImageData ( imageUrl: string, callback: ( data: string ) => void ): void {
|
||||
fetch( imageUrl ).then( ( res ) => {
|
||||
if ( res.status === 200 ) {
|
||||
res.arrayBuffer().then( ( response ) => {
|
||||
const uInt8Array = new Uint8Array( response );
|
||||
const binaryString = new Array( uInt8Array.length );
|
||||
for ( let i = 0; i < uInt8Array.length; i++ ) {
|
||||
binaryString[i] = String.fromCharCode( uInt8Array[i] );
|
||||
}
|
||||
let data = binaryString.join('');
|
||||
let base64 = window.btoa(data);
|
||||
});
|
||||
const data = binaryString.join( '' );
|
||||
callback( 'data:image/png;base64,' + window.btoa( data ) );
|
||||
} );
|
||||
}
|
||||
});
|
||||
|
||||
xhr.onload = function () {
|
||||
if (this.status == 200) {
|
||||
let uInt8Array = new Uint8Array(this.response);
|
||||
|
||||
callback('data:image/png;base64,' + base64);
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
* @param {any} imageUrl
|
||||
* @param {any} callback
|
||||
* @param {any} quality
|
||||
* @returns {any}
|
||||
* Get Image Data as a Base64 string from a URL
|
||||
* @param {string} imageUrl The URL to the image. Has to be a full URL, if not relative
|
||||
* @returns {Promise<string>} returns a Promise resolving to a string with the base64 string
|
||||
*/
|
||||
getColorAsync = function (imageUrl, callback, quality) {
|
||||
getImageDataFromURL ( imageUrl: string ): Promise<string> {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
fetch( imageUrl ).then( ( res ) => {
|
||||
if ( res.status === 200 ) {
|
||||
res.arrayBuffer().then( ( response ) => {
|
||||
const uInt8Array = new Uint8Array( response );
|
||||
const binaryString = new Array( uInt8Array.length );
|
||||
for ( let i = 0; i < uInt8Array.length; i++ ) {
|
||||
binaryString[i] = String.fromCharCode( uInt8Array[i] );
|
||||
}
|
||||
const data = binaryString.join( '' );
|
||||
resolve( 'data:image/png;base64,' + window.btoa( data ) );
|
||||
} );
|
||||
} else {
|
||||
reject( 'ERR_LOADING' );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* [ DEPRECATED ] Same as getColor, but async
|
||||
* @param {string} imageUrl
|
||||
* @param {( data: ColorThiefResult ) => void} callback
|
||||
* @param {number?} quality (Optional) 1 = highest quality, 10 = default. The bigger the number, the
|
||||
* faster a color will be returned but the greater the likelihood that it will not be the visually
|
||||
* most dominant color.
|
||||
* @returns {void}
|
||||
* @deprecated since Version 3.0, in favour of getColorPromise. Only retained for compatibility
|
||||
*/
|
||||
getColorAsync = ( imageUrl: string, callback: ( data: ColorThiefResult, img: HTMLImageElement ) => void, quality: number | null ): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||
const thief = this;
|
||||
this.getImageData(imageUrl, function (imageData) {
|
||||
const sourceImage = document.createElement('img');
|
||||
sourceImage.addEventListener('load', function () {
|
||||
const palette = thief.getPalette(sourceImage, 5, quality);
|
||||
const dominantColor = palette[0];
|
||||
callback(dominantColor, this);
|
||||
});
|
||||
this.getImageDataFromURL( imageUrl ).then( ( imageData ) => {
|
||||
const sourceImage = document.createElement( 'img' );
|
||||
sourceImage.addEventListener( 'load', () => {
|
||||
const palette = thief.getPalette( sourceImage, 5, quality );
|
||||
callback( palette[ 0 ], sourceImage );
|
||||
} );
|
||||
sourceImage.src = imageData;
|
||||
});
|
||||
} );
|
||||
};
|
||||
|
||||
/**
|
||||
* Same as getColor, but promise-based
|
||||
* @param {string} imageUrl
|
||||
* @param {number?} quality (Optional) 1 = highest quality, 10 = default. The bigger the number, the
|
||||
* faster a color will be returned but the greater the likelihood that it will not be the visually
|
||||
* most dominant color.
|
||||
* @returns {Promise<{ 'color': ColorThiefResult, 'img': HTMLImageElement }>} Returns a promise resolving to an object containing the color and the image element
|
||||
*/
|
||||
getColorPromise = ( imageUrl: string, quality: number | null ): Promise<{ 'color': ColorThiefResult, 'img': HTMLImageElement }> => {
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||
const thief = this;
|
||||
this.getImageDataFromURL( imageUrl ).then( ( imageData ) => {
|
||||
const sourceImage = document.createElement( 'img' );
|
||||
sourceImage.addEventListener( 'load', () => {
|
||||
const palette = thief.getPalette( sourceImage, 5, quality );
|
||||
resolve( { 'color': palette[ 0 ], 'img': sourceImage } );
|
||||
} );
|
||||
sourceImage.src = imageData;
|
||||
} ).catch( ( e ) => {
|
||||
reject( e );
|
||||
} );
|
||||
} );
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user