随机颜色函数

 1     // 1、定义一个随机颜色函数,如果输入true(默认),
 2     // 返回#ffffff;;false返回rbg
 3     function getRandomColor(flag = true) {
 4         if(flag){
 5           const arr = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
 6           //要用let;;字符型是常变量,const只用于复杂数据类型
 7           let color = '#'
 8           for (let i = 1;i <= 6;i++){
 9             color = color + arr[parseInt(Math.random()*arr.length)]
10           }
11           return color
12         } 
13 
14         else{
15           const a1 = parseInt(Math.random() * 256)
16           const a2 = parseInt(Math.random() * 256)
17           const a3 = parseInt(Math.random() * 256)
18           return `rgb(${a1},${a2},${a3})`
19         }
20     }

 

posted @ 2023-05-10 11:30  Agiser0  阅读(56)  评论(0)    收藏  举报