import '../../../assert/js/assert.js';
import '../../../globals.js';
// Copyright 2023-2024, University of Colorado Boulder
/**
 * RenderProgram to convert sRGB => linear sRGB
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 */
import { alpenglow, RenderColor, RenderColorSpaceConversion, RenderInstruction } from '../imports.js';
import Vector4 from '../../../dot/js/Vector4.js';
export default class RenderSRGBToLinearSRGB extends RenderColorSpaceConversion {
    constructor(program) {
        super(program, RenderColor.sRGBToLinear);
    }
    getName() {
        return 'RenderSRGBToLinearSRGB';
    }
    withChildren(children) {
        assert && assert(children.length === 1);
        return new RenderSRGBToLinearSRGB(children[0]);
    }
    writeInstructions(instructions) {
        this.program.writeInstructions(instructions);
        instructions.push(RenderInstructionSRGBToLinearSRGB.INSTANCE);
    }
}
alpenglow.register('RenderSRGBToLinearSRGB', RenderSRGBToLinearSRGB);
const scratchVector = new Vector4(0, 0, 0, 0);
export class RenderInstructionSRGBToLinearSRGB extends RenderInstruction {
    toString() {
        return 'RenderInstructionSRGBToLinearSRGB()';
    }
    equals(other, areLocationsEqual) {
        return other instanceof RenderInstructionSRGBToLinearSRGB;
    }
    execute(stack, context, executor) {
        // https://entropymine.com/imageworsener/srgbformula/ (a more precise formula for sRGB)
        // sRGB to Linear
        // 0 ≤ S ≤ 0.0404482362771082 : L = S/12.92
        // 0.0404482362771082 < S ≤ 1 : L = ((S+0.055)/1.055)^2.4
        stack.readTop(scratchVector);
        stack.writeTopValues(scratchVector.x <= 0.0404482362771082 ? scratchVector.x / 12.92 : Math.pow((scratchVector.x + 0.055) / 1.055, 2.4), scratchVector.y <= 0.0404482362771082 ? scratchVector.y / 12.92 : Math.pow((scratchVector.y + 0.055) / 1.055, 2.4), scratchVector.z <= 0.0404482362771082 ? scratchVector.z / 12.92 : Math.pow((scratchVector.z + 0.055) / 1.055, 2.4), scratchVector.w);
    }
    writeBinary(encoder, getOffset) {
        encoder.pushU32(RenderInstruction.SRGBToLinearSRGBCode);
    }
    getBinaryLength() {
        return 1;
    }
}
RenderInstructionSRGBToLinearSRGB.INSTANCE = new RenderInstructionSRGBToLinearSRGB();
//# sourceMappingURL=RenderSRGBToLinearSRGB.js.map