Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

颜色生成 #90

Open
Sunny-117 opened this issue Nov 3, 2022 · 6 comments
Open

颜色生成 #90

Sunny-117 opened this issue Nov 3, 2022 · 6 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@yang-xianzhu
Copy link

function getRandomColor(){
  return `#${Math.floor(Math.random() * 0xffffff).toString(16)}`
}

@veneno-o
Copy link
Contributor

function main(){
    return `#${rand(250)}${rand(250)}${rand(250)}`;
    function rand(n){
        return (Math.floor(Math.random() * n) + 1).toString(16);
    }
}

@lewis617
Copy link

'#'+Math.random().toString(16).substr(-6)

@cscty
Copy link

cscty commented Jul 24, 2023

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

@QdabuliuQ
Copy link

function color() {
    const _color = () => {
        let val = (Math.floor(Math.random() * 255) + 1).toString(16)
        return val.padStart(2, '0')
    }
    return `#${_color()}${_color()}${_color()}`
}

@kangkang123269
Copy link

kangkang123269 commented Sep 8, 2023

  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})`;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants