import { clamp } from "../../math/clamp.js";

/**
 * Converts YCbCr (Rec. 709) to RGB.
 *
 * @param {number} Y Luma (0-255)
 * @param {number} Cb Blue Difference (0-255)
 * @param {number} Cr Red Difference (0-255)
 * @returns {number} packed 24-bit integer: 0xRRGGBB
 */
export function YCbCr_to_rgb_uint24(Y, Cb, Cr) {
    const C_red = Cr - 128;
    const C_blue = Cb - 128;

    // Rec. 709 Inverse Coefficients
    // R = Y + 1.5748 * Cr
    // G = Y - 0.1873 * Cb - 0.4681 * Cr
    // B = Y + 1.8556 * Cb

    const r = clamp(Math.round(Y + 1.5748 * C_red), 0, 255);
    const g = clamp(Math.round(Y - 0.1873 * C_blue - 0.4681 * C_red), 0, 255);
    const b = clamp(Math.round(Y + 1.8556 * C_blue), 0, 255);

    return ((r << 16) | (g << 8) | b) >>> 0;
}
