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
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);
}
}
You could
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).
I don't know anything about PHP, which is why I'm not putting code. But I thought it was an interesting question =)
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)
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
<?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/>";
}
?>
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...
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;
}
If you want to create similar looking colors, it would be easier to use HSV instead of RGB, but you'll need to convert between them.
PHP HSV to RGB formula comprehension
and / or
http://www.brandonheyer.com/2013/03/27/convert-hsl-to-rgb-and-rgb-to-hsl-via-php/
In ordet to get similar colors, you 'fix' 2 of three components and create random values for the third, for example:
function random_color(){
// random hue, full saturation, and slightly on the dark side
return HSLtoRGB(rand(0,360), 1, 0.3);
}
This might help 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)";
}