A Conversation for Use PHP to Get a Random Background Colour for a Website

To really generate random colours..

Post 1

Wandie

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..

Post 2

goweb

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..

Post 3

OwlofDoom

Surely you could do that with just one rand(), goweb...

$randomcolour = "#" . dechex(rand(0xFFFFFF));

~ smiley - towel

PS, R'Win does mention this in the first paragraph, but dismisses it for the exact reason you stated. smiley - biggrin


To really generate random colours..

Post 4

OwlofDoom

s/goweb/Wandie&goweb/


To really generate random colours..

Post 5

goweb

"$randomcolour = "#" . dechex(rand(0xFFFFFF));"

Except FFFFFF is already in hex form.


To really generate random colours..

Post 6

OwlofDoom

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!

~ smiley - towel


To really generate random colours..

Post 7

R'win

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..

Post 8

Wandie

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..

Post 9

R'win

Sarcastically: I don't know. Commenting on entries without reading them first...


Key: Complain about this post

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."

Write an entry
Read more