php 生成随机颜色的方法

随机生成颜色值(例如 #FF00FF):

方法1:

function randColor(){
    mt_srand((double)microtime()*1000000);
    $c = '';
    while(strlen($c)<6){
        $c .= sprintf("%02X", mt_rand(0, 255));
    }
    return '#'.$c;
}

方法2:

function randColor() { 
  $str='0123456789ABCDEF'; 
  $strs='#'; 
  $len=strlen($str); 
  for($i=1;$i<=6;$i++){ 
     $num=rand(0,$len-1);   
     $strs=$strs.$str[$num];  
  } 
  return $strs; 
}

方法3:

function randColor(){
    $cs = array();
    for($i = 0;$i<6;$i++){
        $cs[] = dechex(rand(0,15)); //dechex()把十进制转换为十六进制
    }
    $c = implode('',$cs);
    return '#'.$c;
}

参考:php生成随机颜色方法汇总

发布了52 篇原创文章 · 获赞 15 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_41408081/article/details/103507899