import chroma from 'chroma-js';
// Helper function to convert RGB to LAB (using a standard formula)
function rgbToLab(_r, _g, _b) {
    let r = _r / 255;
    let g = _g / 255;
    let b = _b / 255;
    // Assume sRGB
    r = r > 0.04045 ? ((r + 0.055) / 1.055) ** 2.4 : r / 12.92;
    g = g > 0.04045 ? ((g + 0.055) / 1.055) ** 2.4 : g / 12.92;
    b = b > 0.04045 ? ((b + 0.055) / 1.055) ** 2.4 : b / 12.92;
    const x = (r * 0.4124564) + (g * 0.3575761) + (b * 0.1804375);
    const y = (r * 0.2126729) + (g * 0.7151522) + (b * 0.072175);
    const z = (r * 0.0193339) + (g * 0.119192) + (b * 0.9503041);
    const xyz = {
        x: x * 100,
        y: y * 100,
        z: z * 100
    };
    xyz.x /= 95.047;
    xyz.y /= 100;
    xyz.z /= 108.883;
    const LAB_FT = (6 / 29) ** 3;
    xyz.x = xyz.x > LAB_FT ? xyz.x ** (1 / 3) : (7.787 * xyz.x) + (16 / 116);
    xyz.y = xyz.y > LAB_FT ? xyz.y ** (1 / 3) : (7.787 * xyz.y) + (16 / 116);
    xyz.z = xyz.z > LAB_FT ? xyz.z ** (1 / 3) : (7.787 * xyz.z) + (16 / 116);
    const lab = {
        l: (116 * xyz.y) - 16,
        a: 500 * (xyz.x - xyz.y),
        b: 200 * (xyz.y - xyz.z)
    };
    return lab;
}
// Helper function to convert LAB to LCH
function labToLch(l, a, b) {
    const c = Math.sqrt(a * a + b * b);
    let h = Math.atan2(b, a);
    h = h > 0 ? (h / Math.PI) * 180 : 360 - (Math.abs(h) / Math.PI) * 180;
    return {
        l,
        c,
        h
    };
}
function labToXyz({ l, a, b }) {
    const fy = (l + 16) / 116;
    const fx = a / 500 + fy;
    const fz = fy - b / 200;
    const xr = fx ** 3 > 0.008856 ? fx ** 3 : (116 * fx - 16) / 903.3;
    const yr = l > 7.9996 ? fy ** 3 : l / 903.3;
    const zr = fz ** 3 > 0.008856 ? fz ** 3 : (116 * fz - 16) / 903.3;
    const x = xr * 95.047;
    const y = yr * 100;
    const z = zr * 108.883;
    return {
        x,
        y,
        z
    };
}
function xyzToRgb({ x, y, z }) {
    let r = x * 3.2406 + y * -1.5372 + z * -0.4986;
    let g = x * -0.9689 + y * 1.8758 + z * 0.0415;
    let b = x * 0.0557 + y * -0.204 + z * 1.057;
    r = r > 0.0031308 ? (1.055 * (r ** (1 / 2.4))) - 0.055 : 12.92 * r;
    g = g > 0.0031308 ? (1.055 * (g ** (1 / 2.4))) - 0.055 : 12.92 * g;
    b = b > 0.0031308 ? (1.055 * (b ** (1 / 2.4))) - 0.055 : 12.92 * b;
    return {
        r: Math.round(Math.max(0, Math.min(1, r)) * 255),
        g: Math.round(Math.max(0, Math.min(1, g)) * 255),
        b: Math.round(Math.max(0, Math.min(1, b)) * 255)
    };
}
function lchToLab({ l, c, h }) {
    const hr = (h / 360) * 2 * Math.PI;
    return {
        l,
        a: Math.cos(hr) * c,
        b: Math.sin(hr) * c
    };
}
export function lchToRgb(lch) {
    const lab = lchToLab(lch);
    const xyz = labToXyz(lab);
    return xyzToRgb(xyz);
}
export function generateMaterialPalette(hexColor) {
    const [r, g, b] = chroma(hexColor).rgb();
    const rgb = {
        r,
        g,
        b
    };
    const labColor = rgbToLab(rgb.r, rgb.g, rgb.b);
    const lchColor = labToLch(labColor.l, labColor.a, labColor.b);
    const palette = {};
    const lightnessSteps = [
        50,
        100,
        200,
        300,
        400,
        500,
        600,
        700,
        800,
        900
    ];
    lightnessSteps.forEach((step) => {
        // Calculate the lightness for each palette color
        const adjustedL = Math.max(0, Math.min(100, lchColor.l + (step - 500) / 5));
        // Convert LCH back to RGB and then to HEX
        const rgbColor = lchToRgb({
            l: adjustedL,
            c: lchColor.c,
            h: lchColor.h
        });
        const hexShade = chroma(rgbColor.r, rgbColor.g, rgbColor.b).hex();
        palette[`${step}`] = hexShade;
    });
    return palette;
}
//# sourceMappingURL=Utils.Material.2014.js.map