十六进制颜色,方法1 const randomHex = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0")}`; console.log(randomHex()); 十六进制颜色,方法2 const randomColor = () => `#${Math.random().toString(16).substr(2, 6)}`; console.log(randomColor()); RGB格式 function rgb(){//rgb颜色随机 var r = Math.floor(Math.random()*256); var g = Math.floor(Math.random()*256); var b = Math.floor(Math.random()*256); var rgb = '('+r+','+g+','+b+')'; return rgb; } 颜色RGB转十六进制 const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); rgbToHex(0, 51, 255);