JS color hexadecimal, rgba mutual conversion

1. Hexadecimal to rgba

Hexadecimal color template

https://www.cnblogs.com/cainiao-chuanqi/p/11301471.html

 convert code

function hexToRgba(hex){
        const rgba = [];
        hex = hex.replace('#', '').padEnd(8, 'F');
        for (let i = 0; i < hex.length; i+=2) {
            rgba.push(parseInt(hex.slice(i, i+2), 16))
        }
        return rgba;
    }

2. Convert rgba to hexadecimal

function rgbaToHex(rgba){
    let hex = '#';
    for (const i of rgba) {
        hex += i.toString(16).padStart(2, '0');
    }
    return hex;
}

Guess you like

Origin blog.csdn.net/WwLK123/article/details/131196873