param { PixelImage } pixelImage param { number } width param { number } height param { number } resX param { number } resY export default class ColorMap { public colors : number [ ] [ ] ; public width : number ; public height : number ; public resX : number ; public resY : number ; constructor ( widthVal , heightVal , resXVal = widthVal , resYVal = heightVal ) { this . colors = [ ] ; this . width = widthVal ; this . height = heightVal ; this . resX = resXVal ; this . resY = resYVal ; } param { number } x param { number } y param { number } paletteIndex public put ( x , y , paletteIndex ) { if ( ! this . isInRange ( x , y ) ) { return ; } const rx = this . mapX ( x ) ; if ( this . colors [ rx ] === undefined ) { this . colors [ rx ] = [ ] ; } this . colors [ rx ] [ this . mapY ( y ) ] = paletteIndex ; } /**
     * Get the palette index at x, y coordinate.
     * TODO: rename to getIndex
     */ public get ( x , y ) { const mX = this . mapX ( x ) ; if ( this . colors [ mX ] !== undefined ) { return this . colors [ mX ] [ this . mapY ( y ) ] ; } return undefined ; } public forEachCell ( callback ) { for ( let x = 0 ; x < this . width ; x += this . resX ) { for ( let y = 0 ; y < this . height ; y += this . resY ) { callback ( x , y ) ; } } } param x param y param callback public forEachPixelInCell ( x , y , callback ) { for ( let ix = x ; ix < x + this . resX ; ix += 1 ) { for ( let iy = y ; iy < y + this . resY ; iy += 1 ) { callback ( ix , iy ) ; } } } private isInRange ( x , y ) { return x >= 0 && x < this . width && y >= 0 && y < this . height ; } private mapX ( x ) { return Math . floor ( x / this . resX ) ; } private mapY ( y ) { return Math . floor ( y / this . resY ) ; } }