import '../../../assert/js/assert.js';
import '../../../globals.js';
import { alpenglow, RenderColor, RenderProgram } from '../imports.js';
export default class RenderGradientStop {
    constructor(ratio, program) {
        this.ratio = ratio;
        this.program = program;
        assert && assert(ratio >= 0 && ratio <= 1);
    }
    static evaluate(context, stops, t) {
        let i = -1;
        while (i < stops.length - 1 && stops[i + 1].ratio < t) {
            i++;
        }
        if (i === -1) {
            return stops[0].program.evaluate(context);
        }
        else if (i === stops.length - 1) {
            return stops[i].program.evaluate(context);
        }
        else {
            const before = stops[i];
            const after = stops[i + 1];
            const ratio = (t - before.ratio) / (after.ratio - before.ratio);
            const beforeColor = before.program.evaluate(context);
            const afterColor = after.program.evaluate(context);
            return RenderColor.ratioBlend(beforeColor, afterColor, ratio);
        }
    }
    withProgram(program) {
        if (program === this.program) {
            return this;
        }
        return new RenderGradientStop(this.ratio, program);
    }
    serialize() {
        return {
            ratio: this.ratio,
            program: this.program.serialize()
        };
    }
    static deserialize(obj) {
        return new RenderGradientStop(obj.ratio, RenderProgram.deserialize(obj.program));
    }
}
alpenglow.register('RenderGradientStop', RenderGradientStop);
//# sourceMappingURL=RenderGradientStop.js.map