Eejut Posted May 14, 2010 Share Posted May 14, 2010 I must be doing something wrong here.. So I've figured the decimal (not rgb "decimal") equivalent of ffffff (white) is 16777215 Basic test here.. in theory.. every time the page refreshes it should generate a random colour (UK spelling) from 0 (black) to ffffff (white).. but it doesn't seem to be covering the entire spectrum.. it only seems to be generating shades of green, brown, orange, red <?php $colour = rand(0,16777215); $colour = dechex($colour); echo "<body bgcolor='#$colour'>"; ?> Maths isn't my strongest subject.. just wondering where I'm going wrong here Quote Link to comment Share on other sites More sharing options...
bocasz Posted May 14, 2010 Share Posted May 14, 2010 $hex = dechex(rand(0,255)) . dechex(rand(0,255)) . dechex(rand(0,255)); it's going to return the hex equivalent of a random color. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 14, 2010 Share Posted May 14, 2010 I modified the OP's code slightly to get <?php $colour = rand(0,16777215); $hex_color = dechex($colour); ?> <html> <head> <title>Random Background Color</title> <style type="text/css"> body { background-color: white; } h1 { text-align: center; } .box { background-color: #<?php echo $hex_color ?>; border:1px solid black; width: 50%; margin-right: auto; margin-left: auto; } </style> </head> <body> <div class="box"><h1>This is a test<br><?php echo $hex_color ?></h1></div> </body> </html> Works fine. Ken Quote Link to comment Share on other sites More sharing options...
Eejut Posted May 14, 2010 Author Share Posted May 14, 2010 Hiya Ken, it's not generating any yellows, pinks, greys, blues etc here.. all I seem to get is variations of green, orange, red and brown lol I'll try what you've put there bocasz, I was wondering if there was an efficient way of doing it like that.. this looks like what I needed Thanks dudes :-) Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 14, 2010 Share Posted May 14, 2010 @ken, Did you ever get any results with blue? If I run the code in a loop I get some interesting results. Here is your code run in a loop 20 times: for($i=0; $i<20; $i++) { $colour = rand(0,16777215); $hex_color = dechex($colour); echo "$colour : $hex_color<br />\n"; } As you can see in the results none of the results have any "blue" value, i.e. all the hex numbers end in 00. I'm guessing this has something to do with the rand() function?? 15226880 : e85800 1960448 : 1dea00 6886400 : 691400 3536384 : 35f600 5124096 : 4e3000 4907520 : 4ae200 2010112 : 1eac00 14134784 : d7ae00 3770368 : 398800 5200384 : 4f5a00 9815040 : 95c400 14476800 : dce600 8544256 : 826000 2314752 : 235200 6708224 : 665c00 16621056 : fd9e00 8435712 : 80b800 4114944 : 3eca00 11039744 : a87400 7460352 : 71d600 Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 14, 2010 Share Posted May 14, 2010 bocasz's solution seems to work much better, but has one flaw. If the number for any color space is less than 16 the resulting $hex value is only a single digit. This is probably overkill, but this seems to give expected results: function getRandHexValue() { $red = str_pad(dechex(rand(0,255)), 2, '0', STR_PAD_LEFT); $green = str_pad(dechex(rand(0,255)), 2, '0', STR_PAD_LEFT); $blue = str_pad(dechex(rand(0,255)), 2, '0', STR_PAD_LEFT); return $red.$green.$blue; } $colour = getRandHexValue(); echo "<body bgcolor='#$colour'>"; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 14, 2010 Share Posted May 14, 2010 Here's my code using both methods: <?php $colour = rand(0,16777215); $hex_color = dechex($colour); $hex = sprintf('%02X',rand(0,255)) . sprintf('%02X',rand(0,255)) . sprintf('%02X',rand(0,255)); ?> <html> <head> <title>Random Background Color</title> <style type="text/css"> body { background-color: white; } h1 { text-align: center; } .box { background-color: #<?php echo $hex_color ?>; border:1px solid black; width: 50%; margin-right: auto; margin-left: auto; } </style> </head> <body> <div class="box"><h1>This is a test<br><?php echo $hex_color . '(' . $colour . ')' ?></h1></div> <div class="box" style="background-color:#<?php echo $hex ?>"><h1>This is using <?php echo $hex ?></h1></div> </body> </html> As you can see, I fixed the problem of the single digit HEX value by using sprintf to format the HEX number. Ken Quote Link to comment Share on other sites More sharing options...
Eejut Posted May 14, 2010 Author Share Posted May 14, 2010 All of those examples do the trick Thanks again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.