import { mix } from "@thi.ng/math/mix" ; import type { ColorMatrix , ReadonlyColor } from "./api.js" ; import { RGB_LUMINANCE_REC709 , WHITE } from "./api/constants.js" ; import { __mulM45 , __mulV45 } from "./internal/matrix-ops.js" ; const S0 = 0.072 ; const S1 = 0.213 ; const S2 = 0.285 ; const S3 = 0.715 ; const S4 = 0.787 ; const S5 = 0.928 ; const S6 = 0.14 ; const S7 = 0.143 ; const S8 = 0.283 ; param out param mat param src param clampOut export const transform = __mulV45 ; /**
 * Concatenates given color matrices by pairwise multiplying them in
 * left-right order. Returns combined result matrix to be used with
 *  transform .
 *
 *  remarks Using  concat is the recommended way when applying multiple
 * color transformations in sequence. Since the transforms are combined
 * into a single matrix, it is faster than multiple, individual
 *  transform calls and will also produce more correct results,
 * since result color clamping is only applied once at the end (by
 * default, unless disabled).
 *
 *  param mat param xs export const concat = ( mat , ... xs ) => xs . reduce ( __mulM45 , mat ) ; export const IDENTITY = [ 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; param src export const subtractMat = ( src = WHITE ) : ColorMatrix => [ - 1 , 0 , 0 , 0 , src [ 0 ] , 0 , - 1 , 0 , 0 , src [ 1 ] , 0 , 0 , - 1 , 0 , src [ 2 ] , 0 , 0 , 0 , 1 , 0 ] ; param x export const brightnessMat = ( x ) : ColorMatrix => [ 1 , 0 , 0 , 0 , x , 0 , 1 , 0 , 0 , x , 0 , 0 , 1 , 0 , x , 0 , 0 , 0 , 1 , 0 ] ; export const contrastMat = ( x , o = 0.5 * ( 1 - x ) ) : ColorMatrix => [ x , 0 , 0 , 0 , o , 0 , x , 0 , 0 , o , 0 , 0 , x , 0 , o , 0 , 0 , 0 , 1 , 0 ] ; export const exposureMat = ( x ) : ColorMatrix => [ x , 0 , 0 , 0 , 0 , 0 , x , 0 , 0 , 0 , 0 , 0 , x , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; export const saturationMat = ( x ) : ColorMatrix => [ S1 + S4 * x , S3 - S3 * x , S0 - S0 * x , 0 , 0 , S1 - S1 * x , S3 + S2 * x , S0 - S0 * x , 0 , 0 , S1 - S1 * x , S3 - S3 * x , S0 + S5 * x , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; export const hueRotateMat = ( theta ) : ColorMatrix => { const s = Math . sin ( theta ) ; const c = Math . cos ( theta ) ; return [ S1 + c * S4 - s * S1 , S3 - c * S3 - s * S3 , S0 - c * S0 + s * S5 , 0 , 0 , S1 - c * S1 + s * S7 , S3 + c * S2 + s * S6 , S0 - c * S0 - s * S8 , 0 , 0 , S1 - c * S1 - s * S4 , S3 - c * S3 + s * S3 , S0 + c * S5 + s * S0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; } ; export const temperatureMat = ( x ) : ColorMatrix => [ 1 + x , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 - x , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; export const sepiaMat = ( x = 1 ) : ColorMatrix => [ mix ( 1 , 0.393 , x ) , 0.769 * x , 0.189 * x , 0 , 0 , 0.349 * x , mix ( 1 , 0.686 , x ) , 0.168 * x , 0 , 0 , 0.272 * x , 0.534 * x , mix ( 1 , 0.131 , x ) , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; export const tintMat = ( x ) : ColorMatrix => [ 1 + x , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 + x , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; remarks See  RGB_LUMINANCE_REC709 for default coefficients
 *
 *  param coeffs param offset export const grayscaleMat = ( [ r , g , b ] = RGB_LUMINANCE_REC709 , offset = 0 ) : ColorMatrix => [ r , g , b , 0 , offset , r , g , b , 0 , offset , r , g , b , 0 , offset , 0 , 0 , 0 , 1 , 0 ] ; remarks See  RGB_LUMINANCE_REC709 for default coefficients
 *
 *  param coeffs export const luminanceAlphaMat = ( [ r , g , b ] = RGB_LUMINANCE_REC709 ) : ColorMatrix => [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , r , g , b , 0 , 0 ] ;