Code Snippet
Random Hex Color
Technique #1
<?php
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
?>
Then echo out the $color value anywhere you need it. For example:
<body style="background: <?php echo $color; ?>;">
Technique #2
<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>
There is also a JavaScript version.
Simpler version:
<php $color = sprintf(“#%06x”,rand(0,16777215)); ?>
<body style="background: <php echo $color; ?>;”>
Thanks, I like your better. Simple.
Interestingly enough, I had to do this exact task just a few days ago. Here’s what I came up with:
$color = str_pad(dechex(rand(0,16777215),6, "0", STR_PAD_LEFT);In technique #1 you can tweak with the code to get Red, Green and Blue shades out of the code. The all you have to play with is the value upto 15.
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.