import { Color } from 'jess' ; function colorBlend ( mode , color1 , color2 ) { if ( ! ( color1 instanceof Color ) || ! ( color2 instanceof Color ) ) { throw { message : 'Both arguments must be colors.' } ; } const ab = color1 . alpha ; let cb ; let cs ; const as = color2 . alpha ; let cr ; const rgba = [ ] ; let ar = as + ab * ( 1 - as ) ; for ( let i = 0 ; i < 3 ; i ++ ) { cb = color1 . rgb [ i ] / 255 ; cs = color2 . rgb [ i ] / 255 ; cr = mode ( cb , cs ) ; if ( ar ) { cr = ( as * cs + ab * ( cb - as * ( cb + cs - cr ) ) ) / ar ; } rgba [ i ] = cr * 255 ; } rgba [ 3 ] = ar ; return new Color ( { value : '' , rgba } ) ; } const colorBlendModeFunctions = { multiply : function ( cb , cs ) { return cb * cs ; } , screen : function ( cb , cs ) { return cb + cs - cb * cs ; } , overlay : function ( cb , cs ) { cb *= 2 ; return ( cb <= 1 ) ? colorBlendModeFunctions . multiply ( cb , cs ) : colorBlendModeFunctions . screen ( cb - 1 , cs ) ; } , softlight : function ( cb , cs ) { let d = 1 ; let e = cb ; if ( cs > 0.5 ) { e = 1 ; d = ( cb > 0.25 ) ? Math . sqrt ( cb ) : ( ( 16 * cb - 12 ) * cb + 4 ) * cb ; } return cb - ( 1 - 2 * cs ) * e * ( d - cb ) ; } , hardlight : function ( cb , cs ) { return colorBlendModeFunctions . overlay ( cs , cb ) ; } , difference : function ( cb , cs ) { return Math . abs ( cb - cs ) ; } , exclusion : function ( cb , cs ) { return cb + cs - 2 * cb * cs ; } , average : function ( cb , cs ) { return ( cb + cs ) / 2 ; } , negation : function ( cb , cs ) { return 1 - Math . abs ( cb + cs - 1 ) ; } } ; export const multiply = colorBlend . bind ( null , colorBlendModeFunctions . multiply ) ; export const screen = colorBlend . bind ( null , colorBlendModeFunctions . screen ) ; export const overlay = colorBlend . bind ( null , colorBlendModeFunctions . overlay ) ; export const softlight = colorBlend . bind ( null , colorBlendModeFunctions . softlight ) ; export const hardlight = colorBlend . bind ( null , colorBlendModeFunctions . hardlight ) ; export const difference = colorBlend . bind ( null , colorBlendModeFunctions . difference ) ; export const exclusion = colorBlend . bind ( null , colorBlendModeFunctions . exclusion ) ; export const average = colorBlend . bind ( null , colorBlendModeFunctions . average ) ; export const negation = colorBlend . bind ( null , colorBlendModeFunctions . negation ) ;