2013-10-30 58 views

回答

1

沒有PHP內置函數

$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'); 
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)]; 

然後回聲出$顏色值的任何地方,你需要它。

+0

完美,就像我需要的一樣 –

0

你可以試試這個

 <?php 


     /** 
     * Get random color hex value 
     * 
     * @param int $max_r Maximum value for the red color 
     * @param int $max_g Maximum value for the green color 
     * @param int $max_b Maximum value for the blue color 
     * @return string 
     */ 

     echo getRandomColorHex(); 

     function getRandomColorHex($max_r = 255, $max_g = 255, $max_b = 255) 
     { 
      // ensure that values are in the range between 0 and 255 
      $max_r = max(0, min($max_r, 255)); 
      $max_g = max(0, min($max_g, 255)); 
      $max_b = max(0, min($max_b, 255)); 

      // generate and return the random color 
      return str_pad(dechex(rand(0, $max_r)), 2, '0', STR_PAD_LEFT) . 
       str_pad(dechex(rand(0, $max_g)), 2, '0', STR_PAD_LEFT) . 
       str_pad(dechex(rand(0, $max_b)), 2, '0', STR_PAD_LEFT); 
     } 
0
sprintf('%06x', mt_rand(0, 1<<24 - 1)); 

顏色是紅,綠,藍,所以我們選擇一個隨機的24位數字8位。然後我們將其填充並以十六進制打印。

0
$random_color = dechex(rand(0,255*255*255)); 
相關問題