import IImageData from './IImageData' ; /**
 * Utility for calculations involving pixels
 */ export default class Pixels { public static emptyPixel : number [ ] = [ 0 , 0 , 0 , 0 ] ; param { number [ ] } one param { number [ ] } other return { number [ ] } public static add ( one , other ) { return [ Pixels . cap ( one [ 0 ] + other [ 0 ] ) , Pixels . cap ( one [ 1 ] + other [ 1 ] ) , Pixels . cap ( one [ 2 ] + other [ 2 ] ) , Pixels . cap ( one [ 3 ] + other [ 3 ] ) ] ; } public static poke ( imageData , x , y , pixel ) { if ( pixel !== undefined ) { const i = this . coordsToIndex ( imageData , x , y ) ; if ( i !== undefined ) { imageData . data [ i ] = pixel [ 0 ] ; imageData . data [ i + 1 ] = pixel [ 1 ] ; imageData . data [ i + 2 ] = pixel [ 2 ] ; imageData . data [ i + 3 ] = pixel [ 3 ] ; } } } public static peek ( imageData , x , y ) { return this . peekAtIndex ( imageData , this . coordsToIndex ( imageData , x , y ) ) ; } private static peekAtIndex ( imageData , index ) { if ( index !== undefined ) { return [ imageData . data [ index ] , imageData . data [ index + 1 ] , imageData . data [ index + 2 ] , imageData . data [ index + 3 ] ] ; } return Pixels . emptyPixel ; } private static coordsToIndex ( imageData , x , y ) { const result = Math . floor ( y ) * ( imageData . width << 2 ) + ( x << 2 ) ; return result < imageData . data . length ? result : undefined ; } private static cap ( pixelChannel ) { return Math . min ( 255 , Math . max ( 0 , pixelChannel ) ) ; } }