export type ColorSpace = ( pixel : number [ ] ) => number [ ] ; export default class ColorSpaces { public static rgb : ColorSpace = ( pixel ) : number [ ] => { return pixel ; } ; public static rainbow : ColorSpace = ( pixel ) : number [ ] => { return [ ColorSpaces . yuv ( pixel ) [ 0 ] , 0 , 0 ] ; } ; public static yuv : ColorSpace = ( pixel ) : number [ ] => { return [ pixel [ 0 ] * 0.299 + pixel [ 1 ] * 0.587 + pixel [ 2 ] * 0.114 , pixel [ 0 ] * - 0.14713 + pixel [ 1 ] * - 0.28886 + pixel [ 2 ] * 0.436 , pixel [ 0 ] * 0.615 + pixel [ 1 ] * - 0.51499 + pixel [ 2 ] * - 0.10001 ] ; } ; public static yCbCr : ColorSpace = ( pixel ) : number [ ] => { const [ r , g , b ] = pixel ; return [ 0.299 * r + 0.587 * g + 0.114 * b , - 0.16874 * r - 0.33126 * g + 0.5 * b , 0.5 * r - 0.41869 * g - 0.08131 * b ] ; } ; public static xyz : ColorSpace = ( pixel ) : number [ ] => { let r = pixel [ 0 ] ; let g = pixel [ 1 ] ; let b = pixel [ 2 ] ; r = r > 0.04045 ? ( ( r + 0.055 ) / 1.055 ) ** 2.4 : r / 12.92 ; g = g > 0.04045 ? ( ( g + 0.055 ) / 1.055 ) ** 2.4 : g / 12.92 ; b = b > 0.04045 ? ( ( b + 0.055 ) / 1.055 ) ** 2.4 : b / 12.92 ; return [ r * 41.24 + g * 35.76 + b * 18.05 , r * 21.26 + g * 71.52 + b * 7.22 , r * 1.93 + g * 11.92 + b * 95.05 ] ; } ; public static lab : ColorSpace = ( pixel ) : number [ ] => { const xyzPixel = ColorSpaces . xyz ( pixel ) ; const refX = 95.047 ; const refY = 100.0 ; const refZ = 108.883 ; const pow = 1 / 3 ; const bla = 16 / 116 ; let x = xyzPixel [ 0 ] / refX ; let y = xyzPixel [ 1 ] / refY ; let z = xyzPixel [ 2 ] / refZ ; x = x > 0.008856 ? x ** pow : 7.787 * x + bla ; y = y > 0.008856 ? y ** pow : 7.787 * y + bla ; z = z > 0.008856 ? z ** pow : 7.787 * z + bla ; return [ 116 * y - 16 , 500 * ( x - y ) , 200 * ( y - z ) ] ; } ; public static okLab : ColorSpace = ( pixel ) : number [ ] => { const r = pixel [ 0 ] ; const g = pixel [ 1 ] ; const b = pixel [ 2 ] ; const l = 0.412165612 * r + 0.536275208 * g + 0.0514575653 * b ; const m = 0.211859107 * r + 0.6807189584 * g + 0.107406579 * b ; const s = 0.0883097947 * r + 0.2818474174 * g + 0.6302613616 * b ; return [ 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s , 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s , 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s ] ; } ; public static all = { rgb : ColorSpaces . rgb , yuv : ColorSpaces . yuv , rainbow : ColorSpaces . rainbow , ycbcr : ColorSpaces . yCbCr , xyz : ColorSpaces . xyz , lab : ColorSpaces . lab , oklab : ColorSpaces . okLab } ; }