import { clamp } from "./Interpolation.js";
export class Color {
    r;
    g;
    b;
    a;
    constructor(r = 1, g = 1, b = 1, a = 1) {
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
        for (const channel of [r, g, b, a]) {
            if (!Number.isFinite(channel))
                throw new RangeError("Color channels must be finite.");
        }
    }
    static fromSRGB(r, g, b, a = 1) {
        return new Color(srgbToLinear(r), srgbToLinear(g), srgbToLinear(b), a);
    }
    toSRGB() {
        return new Color(linearToSrgb(this.r), linearToSrgb(this.g), linearToSrgb(this.b), this.a);
    }
    clamp01() {
        return new Color(clamp(this.r), clamp(this.g), clamp(this.b), clamp(this.a));
    }
    lerp(to, t) {
        return new Color(this.r + (to.r - this.r) * t, this.g + (to.g - this.g) * t, this.b + (to.b - this.b) * t, this.a + (to.a - this.a) * t);
    }
    equals(color, epsilon = 1e-10) {
        return Math.abs(this.r - color.r) <= epsilon && Math.abs(this.g - color.g) <= epsilon && Math.abs(this.b - color.b) <= epsilon && Math.abs(this.a - color.a) <= epsilon;
    }
}
export function srgbToLinear(value) {
    const v = clamp(value);
    return v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
}
export function linearToSrgb(value) {
    const v = clamp(value);
    return v <= 0.0031308 ? v * 12.92 : 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
}
//# sourceMappingURL=Color.js.map