跳至主要內容

颜色

chanchaw小于 1 分钟javascript

RGB转十六进制

const rgb2Hex=(r,g,b) => {
    const toHex = num => {
        const hex = num.toString(16);
        return hex.length === 1 ? `0${hex}` : hex
    }
    return `#${toHex(r)}${toHex(g)}${toHex(b)}`
}

console.log(rgb2Hex(46,32,67))// #2e2043

生成随机十六进制颜色

const randomHex = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0")}`;
    
 console.log(randomHex());
// Result: #92b008