使用 PHP 生成随机颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7263140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random color generation using PHP
提问by Rich Darvins
I'm trying to generate random HTML colors in PHP, but I'm having trouble getting them to look similar, or in the same family. Is there some function I can use to generate colors that are "similar" to another color, besides just generating and concatenating 6 random hex digits?
我试图在 PHP 中生成随机的 HTML 颜色,但我无法让它们看起来相似,或者在同一个系列中。除了生成和连接 6 个随机十六进制数字之外,是否有一些函数可以用来生成与另一种颜色“相似”的颜色?
回答by Josh Darnell
You could
你可以
- Generate one random decimal number betweem 25 and 230 (your "base" number)
- Generate 3 random numbers between 1 and 25 (arbitrarily deciding whether they will be positive or negative)
- Add those three numbers to your base number to get three different numbers (your R, G, and B)
- Repeat steps 2 and 3 to get more, similar colors
- 在 25 和 230 之间生成一个随机十进制数(您的“基数”)
- 生成1到25之间的3个随机数(任意决定它们是正数还是负数)
- 将这三个数字与您的基数相加得到三个不同的数字(您的 R、G 和 B)
- 重复步骤 2 和 3 以获得更多相似的颜色
You could widen the range of the modifier number (the one from 1 to 25) to get more variance in your color (you'd have to change the range of your base number as well, so you stay between 0 and 255).
您可以扩大修饰符编号的范围(从 1 到 25 的范围)以获得更多的颜色差异(您还必须更改基数的范围,因此您保持在 0 到 255 之间)。
I don't know anything about PHP, which is why I'm not putting code. But I thought it was an interesting question =)
我对 PHP 一无所知,这就是我不放代码的原因。但我认为这是一个有趣的问题 =)
EDIT:I realized that generating 3 random base numbers in step 1 will get you a less muted looking (grey) color. Then you can follow steps 2 and 3 to get different shades etc. as I already mentioned (and, as @Peter mentioned, increasing the modifier number at the risk of getting less "similar" colors)
编辑:我意识到在步骤 1 中生成 3 个随机基数会让你看起来不那么柔和(灰色)。然后你可以按照步骤 2 和 3 来获得不同的色调等。正如我已经提到的(并且,正如@Peter 提到的,增加修饰符数量的风险是“相似”颜色减少)
Example output of this technique(based on two different sets of base numbers):
此技术的示例输出(基于两组不同的基数):
EDIT 2:Here is the PHP implementation of this by @Peter Ajtai
编辑 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]);'> Base Color </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/>";
}
?>
回答by Max
Found something much nicer posted on the blog of someone called Craig Lotter
:
在某人的博客上发现了一些更好的东西:Craig Lotter
$randomcolor = '#' . dechex(rand(0,10000000));
回答by dqhendricks
a convoluted class i wrote based on colors sharing a brightness. closer the range, greyer the colors. higher the range, brighter the colors.
我根据颜色共享亮度编写的一个复杂的类。范围越近,颜色越灰。范围越大,颜色越亮。
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);
}
}
回答by ricardo.silva.lg
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;}
回答by genesis
you can make your own function which will generate your own rgb color
您可以制作自己的函数,该函数将生成您自己的 rgb 颜色
http://sandbox.phpcode.eu/g/bf2a5/1
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();
?>
or bgcolor
或 bgcolor
http://sandbox.phpcode.eu/g/bf2a5/2
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();
?>
回答by cwallenpoole
A few years ago I came across this class. It lets you generate complimentary colors based on a seed value.
几年前,我遇到了这个类。它允许您根据种子值生成互补色。
If you're looking for something more general, limit yourself to a general range using rand
(obviously below 255) and the use base_convert
.
如果您正在寻找更一般的东西,请使用rand
(显然低于 255)和使用将自己限制在一般范围内base_convert
。
回答by feeela
I'd just limit the range of the rand()-params:
我只是限制了 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>';
}
回答by Clawish
RandomColorhas been ported to PHP, you can find it here. With it it's also possible to have random lightor random darkcolors.
Example Usage:
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'
));
回答by Muscaria
Another short and simple version: change the mt_rand(X, Y)values in order to generate desired colour range: (0, 255) - full range; (180, 255) - pastel palette; (0, 100) - dark palette; etc...
另一个简短的版本:更改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;
}
回答by Peter Green
Try this:
尝试这个:
//For every hex code
$random = mt_rand(0, 16777215);
$color = "#" . dechex($random);
And than you can just use it like this:
而且你可以像这样使用它:
background-color: <?php echo $color ?>;