{"version":3,"file":"Grayscale.min.mjs","names":[],"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      const r = data[i];\n      const g = data[i + 1];\n      const b = data[i + 2];\n      switch (this.mode) {\n        case 'average':\n          value = (r + g + b) / 3;\n          break;\n        case 'lightness':\n          value = (Math.min(r, g, b) + Math.max(r, g, b)) / 2;\n          break;\n        case 'luminosity':\n          value = 0.21 * r + 0.72 * g + 0.07 * b;\n          break;\n      }\n\n      data[i + 2] = data[i + 1] = data[i] = 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"],"mappings":"sRAsBA,IAAa,EAAb,cAA+B,CAAA,CAe7B,UAAA,CAAY,UAAA,CAAW,KAAE,IAAA,CACvB,IAAK,IAAW,EAAP,EAAI,EAAkB,EAAI,EAAK,OAAQ,GAAK,EAAG,CACtD,IAAM,EAAI,EAAK,GACT,EAAI,EAAK,EAAI,GACb,EAAI,EAAK,EAAI,GACnB,OAAQ,KAAK,KAAb,CACE,IAAK,UACH,GAAS,EAAI,EAAI,GAAK,EACtB,MACF,IAAK,YACH,GAAS,KAAK,IAAI,EAAG,EAAG,EAAA,CAAK,KAAK,IAAI,EAAG,EAAG,EAAA,EAAM,EAClD,MACF,IAAK,aACH,EAAQ,IAAO,EAAI,IAAO,EAAI,IAAO,EAIzC,EAAK,EAAI,GAAK,EAAK,EAAI,GAAK,EAAK,GAAK,GAI1C,aAAA,CACE,MAAO,GAAG,KAAK,KAAA,GAAQ,KAAK,OAG9B,mBAAA,CACE,OAAO,EAAe,KAAK,MAS7B,gBACE,EACA,EAAA,CAGA,EAAG,UAAU,EAAiB,MADjB,EAAA,CASf,gBAAA,CACE,MAAA,CAAO,IAAA,EAAA,EA7DF,OAAO,YAAA,CAAA,EAAA,EAEP,WAhBgD,CACvD,KAAM,UAAA,CAAA,CAAA,EAAA,EAiBC,mBAAmB,CAAC,QAAA,CAAA,CA6D7B,EAAc,SAAS,EAAA,CAAA,OAAA,KAAA"}