Random page color

By alabama on Sep 25, 2011

Screenshots

I was bored and learned a little bit of php today (never done php/html/etc before) and thought this was a cool idea.
Every time you refresh the page it'll be a different color.

I do realize I should probably center it with css, but I'll update that in a few.

It'll also display the hex value of the color.

View a live example here:

http://mslcoding.com/dev/color.php

<html>
<?php
 $color=rand(1,100000);
$x="#".$color;
$hex= $x;
?>
<body bgcolor="<?php echo $x; ?>" </body>

<br><br><br><br>
<div align="center">
 <font size="10"><?php echo $hex ?> Press f5</>
</div>
</html>

Comments

Sign in to comment.
alabama   -  Sep 30, 2011

huh

 Respond  
Jonesy44   -  Sep 30, 2011

Sorry I forgot to reply, the code above is a function. Just call it with randHEX()

Eg:

echo randHEX();

 Respond  
Korvin   -  Sep 30, 2011

He did.
Get a random number from 0 to 4294967295 (The maximum number that can be represented in hexidecimal representations) then convert it to hex using the dechex() function.

http://php.net/manual/en/function.dechex.php

 Respond  
alabama   -  Sep 26, 2011

Could you post what the full code would look like? I really just started php like yesterday.

 Respond  
Jonesy44   -  Sep 26, 2011

Look just above.. :/

 Respond  
alabama   -  Sep 25, 2011

jaytea..How can I get it to generated hex colors with letters? not just numbers

example of what i want: am82ne or w/e
example of what it does: 32992

 Respond  
Jonesy44   -  Sep 25, 2011
function randHEX() {
  return dechex(rand(0,4294967295));
}

<body bgcolor="<?php echo randHEX() ?>"

 Respond  
jaytea   -  Sep 25, 2011

not reaaaally :P 1 to 100000 is #010000 to #0186A0.

what you're aiming for is probably $rgb($rand(0, 170), $rand(0, 170), $rand(0, 170)) where aa is 170 in hex. this does not represent a single contiguous range, and so a single rand() wouldn't suffice.

 Respond  
Jonesy44   -  Sep 25, 2011

You're using a number selection only. Where as #dedede which is a valid hex colour will never show because it's not numbers.. Kind of hard to explain how hexadecimal works...

Sorry if you already know and this is condecending.. :p Denary is 0-9, Hex is 0-15 per unit. When you get past 9, it goes from A onwards. i.e denary 10 = hex A.

 Respond  
alabama   -  Sep 25, 2011

100000 tops it off at #aaaaa :) it was meant to be like that.

 Respond  
alabama   -  Sep 25, 2011

it was at that number, but I didn't want any white colors.

 Respond  
jaytea   -  Sep 25, 2011

you're using rand(1,100000) to generate a random integer RGB value whereas the range of possible values is much larger: 0 to 2^24-1 = 16777215.

thus if you want to select from the range of all possible RGB values, you should use rand(0, 16777215)

edit: is #N a valid colour if N is expressed in base10?

 Respond  
alabama   -  Sep 25, 2011

what u mean?

 Respond  
Jonesy44   -  Sep 25, 2011

Might want to use $r1, $r2, $r3 random between 0 and 255. Then convert the RGB value to hex. At the moment, you're only get 10/16ths of the entire hex range using denary values.

 Respond  
alabama   -  Sep 25, 2011

no clue, it's unnecessary yes but I just included it to distinguish it more

 Respond  
FelicianoX   -  Sep 25, 2011

Why $hex if you already have $x

 Respond  
alabama   -  Sep 25, 2011

lol yeah i was reading a basic tutorial on php and decided to try create something unique

 Respond  
blackvenomm666   -  Sep 25, 2011

thats pretty neat. i haven't learned php yet myself although i do know a little html. but thats a neat idea:D

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.