繁体   English   中英

使用 PHP 生成随机颜色

[英]Random color generation using PHP

我试图在 PHP 中生成随机的 HTML 颜色,但我无法让它们看起来相似,或者在同一个系列中。 除了生成和连接 6 个随机十六进制数字之外,是否有一些函数可以用来生成与另一种颜色“相似”的颜色?

你可以

  1. 在 25 和 230 之间生成一个随机十进制数(您的“基数”)
  2. 生成1到25之间的3个随机数(任意决定它们是正数还是负数)
  3. 将这三个数字与您的基数相加得到三个不同的数字(您的 R、G 和 B)
  4. 重复步骤 2 和 3 以获得更多相似的颜色

您可以扩大修饰符编号的范围(从 1 到 25 的范围)以获得更多的颜色差异(您还必须更改基数的范围,因此您保持在 0 到 255 之间)。

我对 PHP 一无所知,这就是我不放代码的原因。 但我认为这是一个有趣的问题 =)

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

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

随机,相似的颜色

编辑 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/>";
}
?>

一个叫Craig Lotter的人的博客上发现了一些更好的帖子

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

我根据颜色共享亮度编写的一个复杂的类。 范围越近,颜色越灰。 范围越大,颜色越亮。

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);
    }
}
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;}

您可以制作自己的函数,该函数将生成您自己的 rgb 颜色

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(); 
?>

几年前,我遇到了这个类 它允许您根据种子值生成互补色。

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

RandomColor已经被移植到 PHP,你可以在这里找到它。 有了它,也可以有随机的浅色或随机的深色
示例用法:

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'
));

另一个简短的版本:更改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;
}

试试这个:

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

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

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

我只是限制了 rand() 参数的范围:

// 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>';
}

如果您想创建外观相似的颜色,使用HSV而不是RGB会更容易,但您需要在它们之间进行转换。

PHP HSV转RGB公式理解

和/或

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

为了获得相似的颜色,您“修复”三个组件中的 2 个并为第三个创建随机值,例如:

function random_color(){

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

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

<?php 
function randomColor(){
    $rand1 = mt_rand(0, 255);
    $rand2 = mt_rand(0, 255);
    $rand3 = mt_rand(0, 255);
    return "rgb(".$rand1.",".$rand2.",".$rand3.", 0.3)";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM