// 定义一个随机颜色的函数
function getRandomColor(flag = true) {
// 如果是true就传递 #ffffff格式,否则传rgb格式
if (flag) {
let str = '#'
let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f']
// 通过for循环来补齐十六进制 #ffffff
for (let i = 1; i <= 6; i++) {
let random = Math.floor(Math.random() * arr.length)
str = str + arr[random]
}
return str
} else {
// 否则就传递 rgb (255,255,255)
let r = getRandom(0, 255)
let g = Math.floor(Math.random() * 256)
let b = getRandom(0, 255)
// console.log(`rgb(${r},${g},${b})`);
return `rgb(${r},${g},${b})`
}
}
// 调用方式,实参传false或true
getRandomColor(false)
getRandomColor(true)