随机生成色值

145 阅读1分钟

方式一 生成RGB

function color_random() {//rgb颜色随机
    const r = Math.floor(Math.random() * 256);
    const g = Math.floor(Math.random() * 256);
    const b = Math.floor(Math.random() * 256);
    return `rgb(${r},${g},${b})`;
 }

方式二 生成16进制

function color_random() {//十六进制颜色随机
    const r = Math.floor(Math.random() * 256);
    const g = Math.floor(Math.random() * 256);
    const b = Math.floor(Math.random() * 256);
    const color = `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
    return color;
}

方式三 生成16进制

function color_random1() {
    return ("#" + parseInt(Math.random() * 0xffffff).toString(16));
 }