A function that encapsulates a random color

z Some browsers do not support the RGB attribute, in order to achieve a function. Then we need to encapsulate the function ourselves! ! !

function randomColor(){
         var r = Math.floor(Math.random()*256);
         var g = Math.floor(Math.random()*256);
         var b = Math.floor(Math.random()*256);
         return "rgb("+r+","+g+","+b+")";
};

Hex color function:

function randomColor16(){
         var r = Math.random(255).toString(16);
         var g = Math.random(255).toString(16);
         var b = Math.random(255).toString(16);
         //255数字转化为16进制
         if(r.length<2) r = "0"+r;
         if(g.length<2) g = "0"+g;
         if(b.length<2) b = "0"+b;
         return "#" + r + g + b;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325093562&siteId=291194637