/**
 * @desc 颜色相关工具方法
 * @author 邓欢
 */
/**
 * @desc 生成CSS rgba颜色字符串
 * @param color 十六进制颜色字符串 (eg: #fff, #ffffff)
 * @param alpha 颜色的Alpha通道
 * @returns rgba颜色字符串 (eg: "rgba(255, 255, 255, 0.8)")
 */
export function rgba(color, alpha = 1) {
    const numericAlpha = typeof alpha === 'number' ? alpha : Number.parseFloat(alpha);
    if (numericAlpha >= 1) {
        return color;
    }
    const [r, g, b, a] = rgbaValues(color);
    return `rgba(${r}, ${g}, ${b}, ${a * Math.min(Math.max(numericAlpha, 0), 1)})`;
}
/**
 * @returns [number, number, number, number] (red, green, blue, alpha);
 */
export function rgbaValues(hexString) {
    if (/^#([\da-f]{3}|[\da-f]{4})$/gi.test(hexString)) {
        const rgb = Number.parseInt(hexString.slice(1, 4), 16);
        const alhpaString = hexString.slice(4);
        const alpha = alhpaString.length > 0 ? Number.parseInt(alhpaString, 16) / 15 : 1;
        const red = Math.trunc(rgb / (16 * 16));
        const green = Math.trunc(rgb / 16) % 16;
        const blue = rgb % 16;
        return [red * 16 + red, green * 16 + green, blue * 16 + blue, alpha];
    }
    if (/^#([\da-f]{6}|[\da-f]{8})$/gi.test(hexString)) {
        const base = 16 * 16;
        const rgb = Number.parseInt(hexString.slice(1, 7), 16);
        const alphaString = hexString.slice(7);
        const alpha = alphaString.length > 0
            ? Number.parseInt(alphaString, 16) / (base - 1)
            : 1;
        const red = Math.trunc(rgb / (base * base));
        const green = Math.trunc(rgb / base) % base;
        const blue = rgb % base;
        return [red, green, blue, alpha];
    }
    throw new Error(`bad color format with ${hexString}`);
}
