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

[js] 第894天 写一个方法,生成一个随机颜色字符串,合法的颜色为 #000000-#FFFFFF #4500

Open
haizhilin2013 opened this issue Sep 25, 2021 · 4 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第894天 写一个方法,生成一个随机颜色字符串,合法的颜色为 #000000-#FFFFFF

3+1官网

我也要出题

@haizhilin2013 haizhilin2013 added the js JavaScript label Sep 25, 2021
@ochenkai
Copy link

ochenkai commented Sep 28, 2021

简单的随机从000000到FFFFFF的话,可以这么写:

randomColor() {
  var colorCount = 256*256*256
  var color = Math.floor(Math.random()*colorCount);
  color = '#'+color.toString(16)
  return color
}

@Cason35
Copy link

Cason35 commented Oct 14, 2021

function randomColor() {
let num = [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"a",
"b",
"c",
"d",
"e",
];
let color = "#";
for (let i = 0; i < 6; i++) {
let x = Math.floor(Math.random() * 15);
console.log(x);
color += num[x];
}
return color;
}

@hyj443
Copy link

hyj443 commented Oct 22, 2021

function getColor(){
    const allCount = Math.pow(16, 6)
    const colorNum = Math.random() * allCount | 0
    return "#" + colorNum.toString(16)
}

getColor()
//'#71e59a'
getColor()
//'#3d47b9'
getColor()
//'#3ef72'

@0726m
Copy link

0726m commented Sep 22, 2023

function generateRandomColor() {
// 生成 0 到 16777215 之间的随机整数
var randomInt = Math.floor(Math.random() * 16777216);

// 将整数转换为十六进制字符串,并补充前导零
var colorString = "#" + randomInt.toString(16).padStart(6, "0");

return colorString;

}

// 调用函数生成随机颜色字符串
var randomColor = generateRandomColor();
console.log(randomColor);

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

No branches or pull requests

5 participants