{"version":3,"file":"Grayscale.min.mjs","sources":["../../../src/filters/Grayscale.ts"],"sourcesContent":["import { BaseFilter } from './BaseFilter';\nimport type { T2DPipelineState, TWebGLUniformLocationMap } from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { fragmentSource } from './shaders/grayscale';\n\nexport type TGrayscaleMode = 'average' | 'lightness' | 'luminosity';\n\ntype GrayscaleOwnProps = {\n  mode: TGrayscaleMode;\n};\n\nexport const grayscaleDefaultValues: GrayscaleOwnProps = {\n  mode: 'average',\n};\n\n/**\n * Grayscale image filter class\n * @example\n * const filter = new Grayscale();\n * object.filters.push(filter);\n * object.applyFilters();\n */\nexport class Grayscale extends BaseFilter<'Grayscale', GrayscaleOwnProps> {\n  declare mode: TGrayscaleMode;\n\n  static type = 'Grayscale';\n\n  static defaults = grayscaleDefaultValues;\n\n  static uniformLocations = ['uMode'];\n\n  /**\n   * Apply the Grayscale operation to a Uint8Array representing the pixels of an image.\n   *\n   * @param {Object} options\n   * @param {ImageData} options.imageData The Uint8Array to be filtered.\n   */\n  applyTo2d({ imageData: { data } }: T2DPipelineState) {\n    for (let i = 0, value: number; i < data.length; i += 4) {\n      switch (this.mode) {\n        case 'average':\n          value = (data[i] + data[i + 1] + data[i + 2]) / 3;\n          break;\n        case 'lightness':\n          value =\n            (Math.min(data[i], data[i + 1], data[i + 2]) +\n              Math.max(data[i], data[i + 1], data[i + 2])) /\n            2;\n          break;\n        case 'luminosity':\n          value = 0.21 * data[i] + 0.72 * data[i + 1] + 0.07 * data[i + 2];\n          break;\n      }\n\n      data[i] = value;\n      data[i + 1] = value;\n      data[i + 2] = value;\n    }\n  }\n\n  getCacheKey() {\n    return `${this.type}_${this.mode}`;\n  }\n\n  getFragmentSource() {\n    return fragmentSource[this.mode];\n  }\n\n  /**\n   * Send data from this filter to its shader program's uniforms.\n   *\n   * @param {WebGLRenderingContext} gl The GL canvas context used to compile this filter's shader.\n   * @param {Object} uniformLocations A map of string uniform names to WebGLUniformLocation objects\n   */\n  sendUniformData(\n    gl: WebGLRenderingContext,\n    uniformLocations: TWebGLUniformLocationMap,\n  ) {\n    const mode = 1;\n    gl.uniform1i(uniformLocations.uMode, mode);\n  }\n\n  /**\n   * Grayscale filter isNeutralState implementation\n   * The filter is never neutral\n   * on the image\n   **/\n  isNeutralState() {\n    return false;\n  }\n}\n\nclassRegistry.setClass(Grayscale);\n"],"names":["grayscaleDefaultValues","mode","Grayscale","BaseFilter","applyTo2d","_ref","imageData","data","value","i","length","this","Math","min","max","getCacheKey","concat","type","getFragmentSource","fragmentSource","sendUniformData","gl","uniformLocations","uniform1i","uMode","isNeutralState","_defineProperty","classRegistry","setClass"],"mappings":"0PAWO,MAAMA,EAA4C,CACvDC,KAAM,WAUD,MAAMC,UAAkBC,EAe7BC,SAAAA,CAASC,GAA4C,IAAzCC,WAAWC,KAAEA,IAA0BF,EACjD,IAAK,IAAWG,EAAPC,EAAI,EAAkBA,EAAIF,EAAKG,OAAQD,GAAK,EAAG,CACtD,OAAQE,KAAKV,MACX,IAAK,UACHO,GAASD,EAAKE,GAAKF,EAAKE,EAAI,GAAKF,EAAKE,EAAI,IAAM,EAChD,MACF,IAAK,YACHD,GACGI,KAAKC,IAAIN,EAAKE,GAAIF,EAAKE,EAAI,GAAIF,EAAKE,EAAI,IACvCG,KAAKE,IAAIP,EAAKE,GAAIF,EAAKE,EAAI,GAAIF,EAAKE,EAAI,KAC1C,EACF,MACF,IAAK,aACHD,EAAQ,IAAOD,EAAKE,GAAK,IAAOF,EAAKE,EAAI,GAAK,IAAOF,EAAKE,EAAI,GAIlEF,EAAKE,GAAKD,EACVD,EAAKE,EAAI,GAAKD,EACdD,EAAKE,EAAI,GAAKD,CAChB,CACF,CAEAO,WAAAA,GACE,MAAAC,GAAAA,OAAUL,KAAKM,UAAID,OAAIL,KAAKV,KAC9B,CAEAiB,iBAAAA,GACE,OAAOC,EAAeR,KAAKV,KAC7B,CAQAmB,eAAAA,CACEC,EACAC,GAGAD,EAAGE,UAAUD,EAAiBE,MADjB,EAEf,CAOAC,cAAAA,GACE,OAAO,CACT,EACDC,EApEYxB,EAAS,OAGN,aAAWwB,EAHdxB,EAAS,WAKFF,GAAsB0B,EAL7BxB,EAOe,mBAAA,CAAC,UA+D7ByB,EAAcC,SAAS1B"}