Generate random hex color code using PHP

I am working on a project where I need to generate the number of undefined random hexadecimal color codes ... how would I like to build such a function in PHP?

+55
php hex
Apr 10 2018-11-21T00:
source share
13 answers

Get a random number from 0 to 255, then convert it to hex:

function random_color_part() { return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT); } function random_color() { return random_color_part() . random_color_part() . random_color_part(); } echo random_color(); 
+83
Apr 10 2018-11-11T00:
source share

An RGB hex string is just a number from 0x0 to 0xFFFFFF, so just create a number in this range and convert it to hex:

 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)); } 
+109
Mar 28 '12 at 4:26
source share

you can use md5 for this purpose, very short

 $color = substr(md5(rand()), 0, 6); 
+44
Aug 09 2018-12-12T00:
source share
 $rand = str_pad(dechex(rand(0x000000, 0xFFFFFF)), 6, 0, STR_PAD_LEFT); echo('#' . $rand); 

You can change rand() to mt_rand() if you want, and you can put strtoupper() around str_pad() to make the random number look better (although this is not necessary).

It works fine and is much simpler than all the other methods described here :)

+29
Sep 23 '14 at 13:20
source share

Valid hexadecimal colors can contain from 0 to 9 and from A to F, so if we create a line with these characters and then mix it, we can get the first 6 characters to create a random hex color code. An example below!

the code

 echo '#' . substr(str_shuffle('ABCDEF0123456789'), 0, 6); 

I checked this in a while loop and generated 10,000 unique colors.

The 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 a result.




outis pointed out that my first example cannot generate hexadecimal numbers such as '4488CC', so I created a function that could generate hexadecimal numbers like this.

the 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 much more results than the first, but if you are not going to generate many color codes, then the first example will work just fine.

+13
Aug 12 '15 at 8:39
source share
 $color = sprintf("#%06x",rand(0,16777215)); 
+9
Sep 23 '14 at 13:19
source share

The shortest way:

 echo substr(uniqid(),-6); // result: 5ebf06 
+7
Aug 12 '16 at 17:03
source share

Starting with PHP 5.3, you can use openssl_random_pseudo_bytes () :

 $hex_string = bin2hex(openssl_random_pseudo_bytes(3)); 
+3
Feb 28 '14 at 18:55
source share

This is how I do it.

 <?php echo 'rgba('.rand(0,255).', '.rand(0,255).', '.rand(0,255).', 0.73)'; ?> 
+1
Mar 31 '17 at 20:26
source share

I think that would be good, and he got any color available.

 $color= substr(str_shuffle('AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899'), 0, 6); 
+1
Nov 22 '17 at 16:30
source share

Web-safe colors are no longer needed (nor is the actual concept, even), since even mobile devices now have 16-bit color.

See Wikipedia for more details.

In other words, use any color from # 000000 to #FFFFFF.

edit: Dear downvoters. First check the change history for the question.

0
Apr 10 2018-11-11T00:
source share

If someone wants to generate light colors

 sprintf('#%06X', mt_rand(0xFF9999, 0xFFFF00)); 
0
Jan 09 '19 at 7:53
source share
 function random_color(){ return sprintf('#%06X', mt_rand(0, 0xFFFFFF)); } 
-one
Jan 10 '18 at 12:22
source share



All Articles