'use strict';

/**
 * colorBlindness.ts — Color blindness simulation modes for editors.
 *
 * Simulates various types of color vision deficiency on canvas content
 * to help designers create accessible graphics.
 */
// ── Info ─────────────────────────────────────────────────────────
const COLOR_BLINDNESS_TYPES = [
    { type: "none", label: "Normal Vision", description: "Full color vision", prevalence: "~92%" },
    { type: "deuteranomaly", label: "Deuteranomaly", description: "Reduced sensitivity to green (most common)", prevalence: "~5%" },
    { type: "protanomaly", label: "Protanomaly", description: "Reduced sensitivity to red", prevalence: "~1.3%" },
    { type: "deuteranopia", label: "Deuteranopia", description: "No green cone cells", prevalence: "~1.2%" },
    { type: "protanopia", label: "Protanopia", description: "No red cone cells", prevalence: "~1%" },
    { type: "tritanomaly", label: "Tritanomaly", description: "Reduced sensitivity to blue", prevalence: "~0.01%" },
    { type: "tritanopia", label: "Tritanopia", description: "No blue cone cells", prevalence: "~0.003%" },
    { type: "achromatopsia", label: "Achromatopsia", description: "Monochromatic vision (no color)", prevalence: "~0.003%" },
    { type: "achromatomaly", label: "Achromatomaly", description: "Almost no color vision", prevalence: "~0.001%" },
];
const SIMULATION_MATRICES = {
    none: [1, 0, 0, 0, 1, 0, 0, 0, 1],
    protanopia: [0.567, 0.433, 0.000, 0.558, 0.442, 0.000, 0.000, 0.242, 0.758],
    deuteranopia: [0.625, 0.375, 0.000, 0.700, 0.300, 0.000, 0.000, 0.300, 0.700],
    tritanopia: [0.950, 0.050, 0.000, 0.000, 0.433, 0.567, 0.000, 0.475, 0.525],
    protanomaly: [0.817, 0.183, 0.000, 0.333, 0.667, 0.000, 0.000, 0.125, 0.875],
    deuteranomaly: [0.800, 0.200, 0.000, 0.258, 0.742, 0.000, 0.000, 0.142, 0.858],
    tritanomaly: [0.967, 0.033, 0.000, 0.000, 0.733, 0.267, 0.000, 0.183, 0.817],
    achromatopsia: [0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114],
    achromatomaly: [0.618, 0.320, 0.062, 0.163, 0.775, 0.062, 0.163, 0.320, 0.516],
};
// ── Simulation ──────────────────────────────────────────────────
/** Apply color blindness simulation to ImageData in-place. */
function simulateColorBlindness(imageData, type) {
    if (type === "none")
        return imageData;
    const m = SIMULATION_MATRICES[type];
    const data = imageData.data;
    for (let i = 0; i < data.length; i += 4) {
        const r = data[i];
        const g = data[i + 1];
        const b = data[i + 2];
        data[i] = Math.min(255, Math.max(0, Math.round(m[0] * r + m[1] * g + m[2] * b)));
        data[i + 1] = Math.min(255, Math.max(0, Math.round(m[3] * r + m[4] * g + m[5] * b)));
        data[i + 2] = Math.min(255, Math.max(0, Math.round(m[6] * r + m[7] * g + m[8] * b)));
        // Alpha unchanged
    }
    return imageData;
}
/** Create a new ImageData with color blindness simulation (non-destructive). */
function simulateColorBlindnessCopy(source, type) {
    const copy = new ImageData(new Uint8ClampedArray(source.data), source.width, source.height);
    return simulateColorBlindness(copy, type);
}
/** Apply color blindness filter to a canvas. */
function applyColorBlindnessFilter(canvas, type) {
    if (type === "none")
        return;
    const ctx = canvas.getContext("2d");
    if (!ctx)
        return;
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    simulateColorBlindness(imageData, type);
    ctx.putImageData(imageData, 0, 0);
}
/** Simulate color blindness on a single RGB color. */
function simulateColor(r, g, b, type) {
    if (type === "none")
        return [r, g, b];
    const m = SIMULATION_MATRICES[type];
    return [
        Math.min(255, Math.max(0, Math.round(m[0] * r + m[1] * g + m[2] * b))),
        Math.min(255, Math.max(0, Math.round(m[3] * r + m[4] * g + m[5] * b))),
        Math.min(255, Math.max(0, Math.round(m[6] * r + m[7] * g + m[8] * b))),
    ];
}
/** Generate an SVG filter for color blindness simulation. */
function colorBlindnessSvgFilter(type, filterId = "cb-filter") {
    if (type === "none")
        return "";
    const m = SIMULATION_MATRICES[type];
    return `<filter id="${filterId}">
  <feColorMatrix type="matrix" values="
    ${m[0]} ${m[1]} ${m[2]} 0 0
    ${m[3]} ${m[4]} ${m[5]} 0 0
    ${m[6]} ${m[7]} ${m[8]} 0 0
    0 0 0 1 0" />
</filter>`;
}

exports.COLOR_BLINDNESS_TYPES = COLOR_BLINDNESS_TYPES;
exports.applyColorBlindnessFilter = applyColorBlindnessFilter;
exports.colorBlindnessSvgFilter = colorBlindnessSvgFilter;
exports.simulateColor = simulateColor;
exports.simulateColorBlindness = simulateColorBlindness;
exports.simulateColorBlindnessCopy = simulateColorBlindnessCopy;
//# sourceMappingURL=colorBlindness.js.map
