//把已经生成的颜色放在这个colorList里面
function getColor(colorList=[]){
let next=true;
let color;
while(next){
const r = Math.floor(Math.random()*255);
const g = Math.floor(Math.random()*255);
const b = Math.floor(Math.random()*255);
color = 'rgba('+ r +','+ g +','+ b +',0.9)';
next=colorList.indexOf(color)>=0;
}
colorList.push(color);
return color;
}
每次需要获取颜色,都调用这个方法即可.
let colorList=[];
const newColor1=getColor(colorList);
const newColor2=getColor(colorList);
const newColor3=getColor(colorList);