import type { TClassProperties } from '../typedefs'; import { BaseFilter } from './BaseFilter'; import type { T2DPipelineState, TWebGLUniformLocationMap } from './typedefs'; import { classRegistry } from '../ClassRegistry'; import { fragmentSource } from './shaders/brightness'; export const brightnessDefaultValues: Partial> = { brightness: 0, mainParameter: 'brightness', }; /** * Brightness filter class * @example * const filter = new Brightness({ * brightness: 0.05 * }); * object.filters.push(filter); * object.applyFilters(); */ export class Brightness extends BaseFilter { /** * Brightness value, from -1 to 1. * translated to -255 to 255 for 2d * 0.0039215686 is the part of 1 that get translated to 1 in 2d * @param {Number} brightness * @default */ declare brightness: number; static type = 'Brightness'; static defaults = brightnessDefaultValues; getFragmentSource() { return fragmentSource; } /** * Apply the Brightness operation to a Uint8ClampedArray representing the pixels of an image. * * @param {Object} options * @param {ImageData} options.imageData The Uint8ClampedArray to be filtered. */ applyTo2d({ imageData: { data } }: T2DPipelineState) { if (this.brightness === 0) { return; } const brightness = Math.round(this.brightness * 255); for (let i = 0; i < data.length; i += 4) { data[i] = data[i] + brightness; data[i + 1] = data[i + 1] + brightness; data[i + 2] = data[i + 2] + brightness; } } /** * Return WebGL uniform locations for this filter's shader. * * @param {WebGLRenderingContext} gl The GL canvas context used to compile this filter's shader. * @param {WebGLShaderProgram} program This filter's compiled shader program. */ getUniformLocations( gl: WebGLRenderingContext, program: WebGLProgram ): TWebGLUniformLocationMap { return { uBrightness: gl.getUniformLocation(program, 'uBrightness'), }; } /** * Send data from this filter to its shader program's uniforms. * * @param {WebGLRenderingContext} gl The GL canvas context used to compile this filter's shader. * @param {Object} uniformLocations A map of string uniform names to WebGLUniformLocation objects */ sendUniformData( gl: WebGLRenderingContext, uniformLocations: TWebGLUniformLocationMap ) { gl.uniform1f(uniformLocations.uBrightness, this.brightness); } } classRegistry.setClass(Brightness);