项目需求:
要求随机生成颜色
HEX编码格式
快速了解色彩原理可查看:https://zhuanlan.zhihu.com/p/583033395
function randColor(){
$str = '#';
for($i = 0; $i < 6; $i++){ //对应HEX编码六位字符
$randNum = rand(0,15);
switch ($randNum) {
case 10: $randNum = 'A'; break;
case 11: $randNum = 'B'; break;
case 12: $randNum = 'C'; break;
case 13: $randNum = 'D'; break;
case 14: $randNum = 'E'; break;
case 15: $randNum = 'F'; break;
}
$str .= $randNum;
}
return $str;
}
$color = randColor();
print_r($color);
// 随机输出结果为:
// #32D37C
// #599AE5