const getPalette = ( sourceImage , colorCount ? , quality ? ) => { if ( typeof colorCount === 'undefined' || colorCount < 2 || colorCount > 256 ) { colorCount = 10 ; } if ( typeof quality === 'undefined' || quality < 1 ) { quality = 10 ; } const image = canvasImage ( sourceImage ) ; const imageData = image . getImageData ( ) ; const pixels = imageData . data ; const pixelCount = image . getPixelCount ( ) ; const pixelArray = [ ] ; for ( let 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 ( a >= 125 ) { if ( ! ( r > 250 && g > 250 && b > 250 ) ) { pixelArray . push ( [ r , g , b ] ) ; } } } const cmap = MMCQ . quantize ( pixelArray , colorCount ) ; const palette = cmap ? cmap . palette ( ) : null ; image . removeCanvas ( ) ; return palette ; } ; const getPaletteFromUrl = ( imageUrl , quality = 10 ) => new Promise ( ( resolve , reject ) => { const sourceImage = new Image ( ) ; sourceImage . crossOrigin = 'Anonymous' ; sourceImage . addEventListener ( 'load' , ( ) => { const palette = getPalette ( sourceImage , 5 , quality ) ; resolve ( { palette , imageUrl } ) ; } ) ; sourceImage . src = imageUrl ; sourceImage . addEventListener ( 'error' , reject . bind ( this ) ) ; } ) ; const canvasImage = ( image ) => { const canvas = document . createElement ( 'canvas' ) ; const context = canvas . getContext ( '2d' ) ; document . body . appendChild ( this . canvas ) ; const width = canvas . width = image . width ; const height = this . canvas . height = image . height ; context . drawImage ( image , 0 , 0 , this . width , this . height ) ; const clear = ( ) => context . clearRect ( 0 , 0 , this . width , this . height ) ; const update = ( imageData ) => context . putImageData ( imageData , 0 , 0 ) ; const getPixelCount = ( ) => width * height ; const getImageData = ( ) => context . getImageData ( 0 , 0 , width , height ) ; const removeCanvas = ( ) => canvas . parentNode . removeChild ( canvas ) ; return { clear , update , getPixelCount , getImageData , removeCanvas } ; } ;