Jump to content

Random colours only coming up with shades of green, brown, orange and red


Recommended Posts

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

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

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 :-)

@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

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'>";

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.