A Conversation for Use PHP to Get a Random Background Colour for a Website
To really generate random colours..
Wandie Started conversation Jun 2, 2003
Why not get a random value for each of the RGB hex values?
$randomcolor = "#".dechex(rand(0,15)).dechex(rand(0,15)).dechex(rand(0,15)).dechex(rand(0,15)).dechex(rand(0,15)).dechex(rand(0,15));
One problem might be the amazingly gaudy colours you'll get!
To really generate random colours..
goweb Posted Jun 2, 2003
A better way might be:
$randomcolor = "#" . dechex(rand(0,255)) . dechex(rand(0,255)) . dechex(rand(0,255));
It would shave a bit of time off processing time.
To really generate random colours..
OwlofDoom Posted Jun 2, 2003
Surely you could do that with just one rand(), goweb...
$randomcolour = "#" . dechex(rand(0xFFFFFF));
~
PS, R'Win does mention this in the first paragraph, but dismisses it for the exact reason you stated.
To really generate random colours..
goweb Posted Jun 2, 2003
"$randomcolour = "#" . dechex(rand(0xFFFFFF));"
Except FFFFFF is already in hex form.
To really generate random colours..
OwlofDoom Posted Jun 2, 2003
Well, sticking 0x in front of a hex number turns it into an integer (internal representation) the same way just sticking a decimal integer in does. The output of rand will be an integer (internal representation) which, when converted to a string, will become a decimal integer and needs dechex() to turn it into a hex string.
Hope that makes sense!
~
To really generate random colours..
R'win Posted Jun 2, 2003
I made a script to do that a while back. It's not as neat but it gets the job done - http://richardwinskill.co.uk/php_crbg.php
To really generate random colours..
Wandie Posted Jun 3, 2003
OwlofDoom said:
PS, R'Win does mention this in the first paragraph, but dismisses it for the exact reason you stated
*blush*
To really generate random colours..
R'win Posted Jun 4, 2003
Sarcastically: I don't know. Commenting on entries without reading them first...
Key: Complain about this post
To really generate random colours..
More Conversations for Use PHP to Get a Random Background Colour for a Website
Write an Entry
"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."