Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

颜色生成

Sunny-117 opened this issue · comments

颜色生成
function getRandomColor(){
  return `#${Math.floor(Math.random() * 0xffffff).toString(16)}`
}
function main(){
    return `#${rand(250)}${rand(250)}${rand(250)}`;
    function rand(n){
        return (Math.floor(Math.random() * n) + 1).toString(16);
    }
}
'#'+Math.random().toString(16).substr(-6)
commented

Math.floor(Math.random()*16).toString(16)

function color() {
    const _color = () => {
        let val = (Math.floor(Math.random() * 255) + 1).toString(16)
        return val.padStart(2, '0')
    }
    return `#${_color()}${_color()}${_color()}`
}
  1. 二进制
function getRandomColor() {
    var color = "#";
    for (var i = 0; i < 3; i++) {
        var sub = Math.floor(Math.random() * 256).toString(2);
        color += ("00"+sub).substr(sub.length);
    }
    return color;
}
  1. rgb
function getRandomColor() {
    var r = Math.floor(Math.random()*256);          // 随机生成红色rgb值
    var g = Math.floor(Math.random()*256);          // 随机生成绿色rgb值
    var b = Math.floor(Math.random()*256);          // 随机生成蓝色rgb值

    return "rgb(" + r + "," + g + "," + b + ")";   // 返回rgb(r, g, b)格式颜色 
}
  1. hsl
function getRandomColor() {
    var h = Math.floor(Math.random() * 361);
    var s = Math.floor(Math.random() * 101) + '%';
    var l = Math.floor(Math.random() * 101) + '%';

    return `hsl(${h}, ${s}, ${l})`;
}