import { Filter } from '../types/filter.type' ; import { normalizeNumberPercentage } from '../utils/filter.utils' ; export const sepia = ( context , amount = '0' ) => { amount = normalizeNumberPercentage ( amount ) ; if ( amount <= 0 ) { return context ; } if ( amount > 1 ) { amount = 1 ; } 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 ) { const r = data [ i + 0 ] ; const g = data [ i + 1 ] ; const b = data [ i + 2 ] ; data [ i + 0 ] = ( 0.393 * r + 0.769 * g + 0.189 * b ) * amount + r * ( 1 - amount ) ; data [ i + 1 ] = ( 0.349 * r + 0.686 * g + 0.168 * b ) * amount + g * ( 1 - amount ) ; data [ i + 2 ] = ( 0.272 * r + 0.534 * g + 0.131 * b ) * amount + b * ( 1 - amount ) ; } context . putImageData ( imageData , 0 , 0 ) ; return context ; } ;