2011-08-31 156 views
13

我试图在PHP中生成随机的HTML颜色,但是我很难让它们看起来类似,或者在同一个家族中。是否有一些函数可用于生成与另一种颜色“相似”的颜色,除了生成并连接6个随机十六进制数字?使用PHP生成随机颜色

+0

一个例子类似的颜色吗? – Tarik

+0

是的,你是什么意思?亮度范围?频谱范围? – dqhendricks

+0

想象一下一盒蜡笔,我在想所有的光线颜色,例如浅蓝色,浅绿色等,或所有“褪色”颜色。我不是100%确定技术术语,但希望能回答你的问题。 –

回答

2

,你可以使自己的功能,这将

http://sandbox.phpcode.eu/g/bf2a5/1

<?php 
function gen(){ 
    for($i=1;$i<200;$i++){ 
     echo "<div style='color:rgb($i,$i,0);'>hello</div>"; 
    } 
} 

gen(); 
?> 

或BGCOLOR

http://sandbox.phpcode.eu/g/bf2a5/2

<?php 
function gen(){ 
    for($i=1;$i<200;$i++){ 
     echo "<div style='background-color:rgb($i,$i,0);'>hello</div>"; 
    } 
} 

gen(); 
?> 
+0

这实际上是我已经在做的事情,并且生成的颜色不能很好地协同工作。 –

+1

这不是随机的。它只是一串红色/绿色的顺序色调。 –

+0

@MarcB:嗯,我想$ I可以兰特()omed – genesis

2

几年前,我碰到生成自己的RGB颜色this class。它可以让你根据种子值生成免费的颜色。

如果您正在寻找更一般的东西,请使用rand(明显低于255)和使用base_convert将自己限制在一般范围内。

21

你可以

  1. 生成一个随机十进制数betweem 25和230(你的“基地”)
  2. 生成1到25之间的3张随机数(任意决定他们是否会正或负)
  3. 那些三个数字添加到你的碱基数以获得三个不同的数字(您的R,G,和B)
  4. 重复步骤2和3,以获得更多,相似的颜色

您可以扩大修饰符号的范围(从1到25的范围)以获得更多颜色方差(您必须更改基准号的范围,以便保持0到255之间的范围)。

我对PHP一无所知,这就是为什么我没有放置代码。但我认为这是一个有趣的问题=)

编辑:我意识到,在步骤1中生成3个随机基数会让你看起来不那么沉静(灰色)的颜色。然后,您可以按照步骤2和步骤3获得不同的色调等,正如我已经提到的那样(并且,@Peter提到,增加修饰符号可能会减少“相似”颜色)

此示例输出(基于两组不同基数的)技术

random, similar colors

编辑2:这里是由@Peter Ajtai

PHP实现的这
<?php 
$spread = 25; 
for ($row = 0; $row < 100; ++$row) { 
     for($c=0;$c<3;++$c) { 
     $color[$c] = rand(0+$spread,255-$spread); 
    } 
    echo "<div style='float:left; background-color:rgb($color[0],$color[1],$color[2]);'>&nbsp;Base Color&nbsp;</div><br/>"; 
    for($i=0;$i<92;++$i) { 
    $r = rand($color[0]-$spread, $color[0]+$spread); 
    $g = rand($color[1]-$spread, $color[1]+$spread); 
    $b = rand($color[2]-$spread, $color[2]+$spread);  
    echo "<div style='background-color:rgb($r,$g,$b); width:10px; height:10px; float:left;'></div>"; 
    }  
    echo "<br/>"; 
} 
?> 
+0

有趣,我会给这个镜头,谢谢。 –

+0

我现在离开我的电脑,但只是意识到这可能只是产生随机阴影的灰色= /如何existential。 – jadarnel27

+0

浅灰色,但不完全是灰色。你可以增加25以减少灰色,但是它们不那么“相关” - 我认为你的方法基本上是第五个在这里==> http://codepad.viper-7。com/tCtTdb –

1

我只是限制兰特的范围() - 参数数量:

// full color palette (32 bit) 
for($index = 0; $index < 30; $index++) 
{ 
    echo '<div style="background-color: #' . dechex(rand(0,16777215)) . '; display: inline-block; width: 50px; height: 50px;"></div>'; 
} 
echo '<br />'; 

// pastell colors 
for($index = 0; $index < 30; $index++) 
{ 
    echo '<div style="background-color: rgb(' . rand(128,255) . ',' . rand(128,255) . ',' . rand(128,255) . '); display: inline-block; width: 50px; height: 50px;"></div>'; 
} 
echo '<br />'; 

// dark colors 
for($index = 0; $index < 30; $index++) 
{ 
    echo '<div style="background-color: rgb(' . rand(0,128) . ',' . rand(0,128) . ',' . rand(0,128) . '); display: inline-block; width: 50px; height: 50px;"></div>'; 
} 
echo '<br />'; 

// shades of blue 
for($index = 0; $index < 30; $index++) 
{ 
    echo '<div style="background-color: rgb(' . rand(0,56) . ',' . rand(0,56) . ',' . rand(0,255) . '); display: inline-block; width: 50px; height: 50px;"></div>'; 
} 
echo '<br />'; 

// shades of green 
for($index = 0; $index < 30; $index++) 
{ 
    echo '<div style="background-color: rgb(' . rand(0,56) . ',' . rand(0,255) . ',' . rand(0,56) . '); display: inline-block; width: 50px; height: 50px;"></div>'; 
} 
echo '<br />'; 

// shades of red 
for($index = 0; $index < 30; $index++) 
{ 
    echo '<div style="background-color: rgb(' . rand(0,255) . ',' . rand(0,56) . ',' . rand(0,56) . '); display: inline-block; width: 50px; height: 50px;"></div>'; 
} 
3

一个令人费解的类我写的基于共享的颜色亮度。距离越近,颜色越浅。范围越高,颜色越亮。

class colorGenerator 
{ 

    protected $rangeLower, $rangeHeight; 
    private $range = 100; 

    function __construct($range_lower = 80, $range_higher = 160) 
    { 
     // range of rgb values 
     $this->rangeLower = $range_lower; 
     $this->rangeHeight = $range_higher - $range_lower; 
    } 

    protected function randomColor() 
    { 
     // generate random color in range 
     return $this->generateColor(rand(0, 100)); 
    } 

    protected function generateColor($value) 
    { 
     // generate color based on value between 0 and 100 
     // closer the number, more similar the colors. 0 is red. 50 is green. 100 is blue. 
     $color_range = $this->range/3; 
     $color  = new stdClass(); 
     $color->red = $this->rangeLower; 
     $color->green = $this->rangeLower; 
     $color->blue = $this->rangeLower; 
     if ($value < $color_range * 1) { 
      $color->red += $color_range - $value; 
      $color->green += $value; 
     } else if ($value < $color_range * 2) { 
      $color->green += $color_range - $value; 
      $color->blue += $value; 
     } else if ($value < $color_range * 3) { 
      $color->blue += $color_range - $value; 
      $color->red += $value; 
     } 
     $color->red = round($color->red); 
     $color->blue = round($color->blue); 
     $color->green = round($color->green); 
     // returns color object with properties red green and blue. 
     return $color; 
    } 

    protected function RGBColor($stdClass) 
    { 
     $RGB = "rgb({$stdClass->red}, {$stdClass->blue}, {$stdClass->green})"; 
     return $RGB; 
    } 

    function CreateColor($value) { 
     $stdClass = $this->generateColor($value); 
     return $this->RGBColor($stdClass); 
    } 

    function CreateRandomColor($value) { 
     $stdClass = $this->randomColor($value); 
     return $this->RGBColor($stdClass); 
    } 
} 
17

找到东西好得多上the blog of someone called Craig Lotter:

$randomcolor = '#' . dechex(rand(0,10000000)); 
+2

对于CSS,我们可以使用更严格的命令:''#'。 dechex(兰特(256,16777215))'。这将在#100和#ffffff之间生成十六进制颜色。 –

0

张贴如果你想创建类似的色彩,它会更容易使用HSV,而不是RGB,但你需要在它们之间进行转换。

PHP HSV to RGB formula comprehension

和/或

http://www.brandonheyer.com/2013/03/27/convert-hsl-to-rgb-and-rgb-to-hsl-via-php/

在ordet得到类似的颜色,你 '修复' 2的三个组成部分和第三创建随机值,例如:

function random_color(){ 

    // random hue, full saturation, and slightly on the dark side 
    return HSLtoRGB(rand(0,360), 1, 0.3); 
} 
0

这可能有帮助sprintf("%06s\n", strtoupper(dechex(rand(0,10000000))));

3
function randomColor() { 
$str = '#'; 
for ($i = 0; $i < 6; $i++) { 
    $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;} 
1

RandomColor已被移植到PHP,你可以找到它here。有了它,也可以随机有或随机颜色。
实例应用:

include('RandomColor.php'); 
use \Colors\RandomColor; 
// Returns a hex code for a 'truly random' color 
RandomColor::one(array(
    'luminosity' => 'random', 
    'hue' => 'random' 
)); 
// Returns a hex code for a light blue 
RandomColor::one(array(
    'luminosity' => 'light', 
    'hue' => 'blue' 
)); 
1

另一个短且简单的版本: 改变mt_rand(X,Y),以产生期望的颜色范围值:(0,255) - 全范围; (180,255) - 柔和的调色板; (0,100) - 黑色调色板;等等......

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

试试这个:

//For every hex code 
$random = mt_rand(0, 16777215); 
$color = "#" . dechex($random); 

而且比你可以使用它像这样:

background-color: <?php echo $color ?>;