我正在尝试在PHP中生成随机HTML颜色,但我无法让它们看起来相似,或者在同一个系列中。是否有一些功能可用于生成与另一种颜色“相似”的颜色,除了生成和连接6个随机十六进制数字外?
答案 0 :(得分:21)
你可以
您可以扩大修改器编号的范围(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]);'> 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/>";
}
?>
答案 1 :(得分:17)
在the blog of someone called Craig Lotter
:
$randomcolor = '#' . dechex(rand(0,10000000));
答案 2 :(得分: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);
}
}
答案 3 :(得分: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;}
答案 4 :(得分:2)
你可以创建自己的函数,生成自己的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();
?>
答案 5 :(得分:2)
几年前,我遇到了this class。它允许您根据种子值生成互补色。
如果您正在寻找更一般的内容,请使用rand
(显然低于255)并使用base_convert
将自己限制在一般范围内。
答案 6 :(得分:1)
我只是限制了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>';
}
答案 7 :(得分: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'
));
答案 8 :(得分: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;
}
答案 9 :(得分: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/
要获得相似的颜色,你可以“修复”三个组件中的两个并为第三个组件创建随机值,例如:
function random_color(){
// random hue, full saturation, and slightly on the dark side
return HSLtoRGB(rand(0,360), 1, 0.3);
}
答案 10 :(得分:0)
这可能会有助sprintf("%06s\n", strtoupper(dechex(rand(0,10000000))));
答案 11 :(得分:0)
试试这个:
//For every hex code
$random = mt_rand(0, 16777215);
$color = "#" . dechex($random);
而且你可以像这样使用它:
background-color: <?php echo $color ?>;
答案 12 :(得分:0)
<?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)";
}