import { Filter } from '../types/filter.type' ; import { normalizeNumberPercentage } from '../utils/filter.utils' ; export const contrast = ( context , amount = '1' ) => { amount = normalizeNumberPercentage ( amount ) ; if ( amount === 1 ) { return context ; } if ( amount < 0 ) { amount = 0 ; } const { height , width } = context . canvas ; const imageData = context . getImageData ( 0 , 0 , width , height ) ; const { data } = imageData ; const { length } = data ; for ( let i = 0 ; i < length ; i += 4 ) { data [ i + 0 ] = ( ( ( ( data [ i + 0 ] / 255 ) - .5 ) * amount ) + .5 ) * 255 ; data [ i + 1 ] = ( ( ( ( data [ i + 1 ] / 255 ) - .5 ) * amount ) + .5 ) * 255 ; data [ i + 2 ] = ( ( ( ( data [ i + 2 ] / 255 ) - .5 ) * amount ) + .5 ) * 255 ; } context . putImageData ( imageData , 0 , 0 ) ; return context ; } ;