2016-09-21 99 views
-1

我想用平顏色爲每個類別和它必須是隨機獨特生成隨機平顏色用PHP

我發現那個網站。 =>http://flatcolors.net/random.php

這正是我需要的!

我如何用Php生成?這將是真棒,如果它是產生像「基地平光顏色(例如,平綠)」,並與其他類似的不同顏色25等(例如,平綠另一個音)

+1

你是什麼意思的「平面顏色」?爲什麼你不能簡單地創建一個隨機的顏色值,並通過你檢查過的已經使用過的顏色的簡單數組來防止重新使用?那麼這裏你的具體問題是什麼? – arkascha

+1

你到目前爲止已經嘗試過實現這個目標嗎? –

+0

@arkascha是的,我可以創建一個隨機顏色,但它也給我一些不好的顏色。你會明白,如果你知道平面顏色是什麼意思。所以我想我必須過濾我的隨機顏色,但如何?。也許我無法解釋清楚,因爲我的英語.. –

回答

0

你好,B.Esen

RGB color model一個顏色有3個分量,代表該顏色中紅色,綠色和藍色的量。所以爲了產生一個隨機顏色,你所要做的就是生成0到255的3個隨機數,然後轉換爲16的基數,並將結果連成像這樣的#ff23ab,其中ff => 255(紅色的量), 23(基數16)=> 35(綠色數量)和ab(基數16)是171(藍色數量)。在光譜的「邊緣」找到基色,其中r,g,b的值爲0或255,不包括白色和黑色,其中全部爲0或255.

因此,您有以下基色

  1. 0000FF - >藍色
  2. 0000FF - >藍色
  3. 00FF00 - >綠色
  4. FF0000 - >紅色
  5. FFFF00 - >黃色
  6. ˚F f00ff - >品紅
  7. 00FFFF - >青色

我不知道你所說的「平色」是什麼,但低於類應爲您提供一個體面的配置的顏色產生,使用理論以產生沒有灰度變化的顏色(至少一個發生器是0)。請記住,下面的代碼不是「生產就緒」,並且旨在具有教學目的。在生產中使用它之前,您需要使其十分安全(設置邊界,檢查除零等)。

class ColorGenerator 
{ 
    /** 
    * Used to set the lower limit of RGB values. 
    * The higher this value is the fewer gray tone will be generated 70+ to 100 recommended 
    * 
    * @var int 
    */ 
    protected static $lowerLimit = 70; 

    /** 
    * Used to set the higher limit of RGB values. 
    * The higher this value is the fewer gray tone will be generated 180+ to 255 recommended 
    * 
    * @var int 
    */ 
    protected static $upperLimit = 255; 

    /** 
    * Distance between 2 selected values. 
    * Colors like ff0000 amd ff0001 are basically the same when it comes to human eye perception 
    * increasing this value will result in more different color but will lower the color pool 
    * 
    * @var int 
    */ 
    protected static $colorGap = 20; 

    /** 
    * Colors already generated 
    * 
    * @var array 
    */ 
    protected static $generated = array(); 

    /** 
    * @return string 
    */ 
    public static function generate() 
    { 
     $failCount = 0; 
     do { 
     $redVector = rand(0, 1); 
     $greenVector = rand(0, 1); 
     $blueVector = rand(!($redVector || $greenVector), (int)(($redVector xor $greenVector) || !($redVector || $greenVector))); 
     $quantiles = floor((self::$upperLimit - self::$lowerLimit)/self::$colorGap); 

     $red = $redVector * (rand(0, $quantiles) * self::$colorGap + self::$lowerLimit); 
     $green = $greenVector * (rand(0, $quantiles) * self::$colorGap + self::$lowerLimit); 
     $blue = $blueVector * (rand(0, $quantiles) * self::$colorGap + self::$lowerLimit); 
     $failCount++; 
     } while (isset(self::$generated["$red,$green,$blue"]) && $failCount < 1000); 

     return self::rgb($red, $green, $blue); 
    } 

    /** 
    * @param int $red 
    * @param int $green 
    * @param int $blue 
    * @return string 
    */ 
    protected static function rgb($red, $green, $blue) 
    { 
     $red = base_convert($red, 10, 16); 
     $red = str_pad($red, 2, '0', STR_PAD_LEFT); 

     $green = base_convert($green, 10, 16); 
     $green = str_pad($green, 2, '0', STR_PAD_LEFT); 

     $blue = base_convert($blue, 10, 16); 
     $blue = str_pad($blue, 2, '0', STR_PAD_LEFT); 

     return '#' . $red . $green . $blue; 
    } 
} 

希望這會有所幫助。快樂編碼

Alexandru Cosoi

+0

謝謝!這實際上是我需要的:) –