Valid hex colors can contain 0 to 9 and A to F so if we create a string with those characters and then shuffle it, we can grab the first 6 characters to create a random hex color code. An example is below!
code
echo '#' . substr(str_shuffle('ABCDEF0123456789'), 0, 6);
I tested this in a while loop and generated 10,000 unique colors.
code I used to generate 10,000 unique colors:
$colors = array();
while (true) {
$color = substr(str_shuffle('ABCDEF0123456789'), 0, 6);
$colors[$color] = '#' . $color;
if ( count($colors) == 10000 ) {
echo implode(PHP_EOL, $colors);
break;
}
}
Which gave me these random colors as the result.
outis pointed out that my first example couldn't generate hexadecimals such as '4488CC' so I created a function which would be able to generate hexadecimals like that.
code
function randomHex() {
$chars = 'ABCDEF0123456789';
$color = '#';
for ( $i = 0; $i < 6; $i++ ) {
$color .= $chars[rand(0, strlen($chars) - 1)];
}
return $color;
}
echo randomHex();
The second example would be better to use because it can return a lot more different results than the first example, but if you aren't going to generate a lot of color codes then the first example would work just fine.
$color = sprintf("#%06x",rand(0,16777215));
Shortest way:
echo substr(uniqid(),-6); // result: 5ebf06
This is how i do it.
<?php echo 'rgba('.rand(0,255).', '.rand(0,255).', '.rand(0,255).', 0.73)'; ?>
Web-safe colors are no longer necessary (nor a valid concept, even) as even mobile devices have 16+ bit colour these days.
See Wikipedia for more info.
In other words, use any colour from #000000 to #FFFFFF.
edit: Dear downvoters. Check the edit history for the question first.
you can use md5 for that purpose,very short
$color = substr(md5(rand()), 0, 6);
function random_color(){
return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}
I think this would be good as well it gets any color available
$color= substr(str_shuffle('AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899'), 0, 6);
As of PHP 5.3, you can use openssl_random_pseudo_bytes():
$hex_string = bin2hex(openssl_random_pseudo_bytes(3));
$rand = str_pad(dechex(rand(0x000000, 0xFFFFFF)), 6, 0, STR_PAD_LEFT);
echo('#' . $rand);
You can change rand()
in for mt_rand()
if you want, and you can put strtoupper()
around the str_pad()
to make the random number look nicer (although it’s not required).
It works perfectly and is way simpler than all the other methods described here :)
This is heavily based on the @Galen version above, however, I wanted to add range control that could limit the colour produced to be red, green, blue, lighter or darker. It might be of use to others.
function random_colour_part($lower, $upper)
{
//randomly select colour in range and convert to hexidecimal
return str_pad(dechex(mt_rand($lower, $upper)), 2, '0', STR_PAD_LEFT);
}
function random_colour($colour)
{
//loop through colour
foreach ($colour as $key => $value)
{
//retrieve each r,g,b colour range and generate random hexidecimal colour
if ($key == "r") $r = random_colour_part($value[0], $value[1]);
if ($key == "g") $g = random_colour_part($value[0], $value[1]);
if ($key == "b") $b = random_colour_part($value[0], $value[1]);
}
//return hexidecimal colour
return "#" . $r . $g . $b;
}
//generate a random red-based colour
echo random_colour(["r"=>[0,255], "g"=>[0,0], "b"=>[0,0]]);
//generate a random light green-based colour (use only half of the 255 range)
echo random_colour(["r"=>[0,0], "g"=>[127,255], "b"=>[0,0]]);
//generate a random colour of any sort
echo random_colour(["r"=>[0,255], "g"=>[0,255], "b"=>[0,255]]);
An RGB hex string is just a number from 0x0 through 0xFFFFFF, so simply generate a number in that range and convert it to hexadecimal:
function rand_color() {
return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}
or:
function rand_color() {
return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}
If someone wants to generate light colors
sprintf('#%06X', mt_rand(0xFF9999, 0xFFFF00));
Source: Stackoverflow.com