fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 3.34 kB
Source Map (JSON)
{"version":3,"file":"Grayscale.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":";;;;;AAWA,MAAa,yBAA4C,EACvD,MAAM,WACP;;;;;;;;AASD,IAAa,YAAb,cAA+B,WAA2C;;;;;;;CAexE,UAAU,EAAE,WAAW,EAAE,UAA4B;AACnD,OAAK,IAAI,IAAI,GAAG,OAAe,IAAI,KAAK,QAAQ,KAAK,GAAG;GACtD,MAAM,IAAI,KAAK;GACf,MAAM,IAAI,KAAK,IAAI;GACnB,MAAM,IAAI,KAAK,IAAI;AACnB,WAAQ,KAAK,MAAb;IACE,KAAK;AACH,cAAS,IAAI,IAAI,KAAK;AACtB;IACF,KAAK;AACH,cAAS,KAAK,IAAI,GAAG,GAAG,EAAE,GAAG,KAAK,IAAI,GAAG,GAAG,EAAE,IAAI;AAClD;IACF,KAAK;AACH,aAAQ,MAAO,IAAI,MAAO,IAAI,MAAO;AACrC;;AAGJ,QAAK,IAAI,KAAK,KAAK,IAAI,KAAK,KAAK,KAAK;;;CAI1C,cAAc;AACZ,SAAO,GAAG,KAAK,KAAK,GAAG,KAAK;;CAG9B,oBAAoB;AAClB,SAAO,eAAe,KAAK;;;;;;;;CAS7B,gBACE,IACA,kBACA;AAEA,KAAG,UAAU,iBAAiB,OADjB,EAC6B;;;;;;;CAQ5C,iBAAiB;AACf,SAAO;;;2BA7DF,QAAO,YAAY;2BAEnB,YAAW,uBAAuB;2BAElC,oBAAmB,CAAC,QAAQ,CAAC;AA6DtC,cAAc,SAAS,UAAU"}