Jump to content

Creating an HTML hex color chart/table?


random1

Recommended Posts

How can you create a HTML Hex Color Chart using PHP to generate it?

 

Such as: http://www.immigration-usa.com/html_colors.html

 

So far I have the code:

 

<?php

function random_hex_color()
{
return sprintf("%02X%04X%02X", mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
}

echo('<table>' . "\n");

for($i = 0; $i < 500; $i++)
{
$hex = random_hex_color();
echo('<tr>' . "\n");
echo('<td bgcolor="#' . $hex . ';">#' . $hex . '</td>');
echo('</tr>');
}

echo("\n" . '</table>');

?>

 

but is not random enough. I'd like it so that is shades or each color ; red, yellow, blue, green, black.

Link to comment
Share on other sites

Its not random because you are using 4 zero filled spaces for the Green component in the sprintf format  , effectively fixing it as 00.

 

Change random_hex_color function to this

 

function random_hex_color()
{
   return sprintf("%02X%02X%02X", mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
}

Link to comment
Share on other sites

I ended up with:

 

<?php

function random_hex_color($max_r = 255, $max_g = 255, $max_b = 255)
{
// ensure that values are in the range between 0 and 255
if ($max_r > 255) { $max_r = 255; }
if ($max_g > 255) { $max_g = 255; }
if ($max_b > 255) { $max_b = 255; }
if ($max_r < 0) { $max_r = 0; }
if ($max_g < 0) { $max_g = 0; }
if ($max_b < 0) { $max_b = 0; }

// generate and return the random color
return dechex(rand(0, $max_r)) . dechex(rand(0, $max_g)) . dechex(rand(0, $max_b));
}

echo('<table>' . "\n");

for($i = 0; $i < 500; $i++)
{
$hex = random_hex_color();
echo('<tr height="40px">' . "\n");
echo('<td width="40px" bgcolor="#' . $hex . ';">    </td>');
echo('<td width="120px"> #' . $hex . '</td>');
echo('</tr>');
}

echo("\n" . '</table>');

?>

Link to comment
Share on other sites

You new method does produce random colors but there is still a bias in color selection . There are some possible colors which will never be produced and the bias is towards colors with higher red components and lower blue .

 

This is because you are dropping leading 0's  in your RGB color settings then concatenating them together.

 

So for instance  the 3 different  colours rgb(0F,FF,0F) rgb(0F,0F,FF) & rgb(FF,0F,0F) get returned as FFFF which is interpreted as rgb(FF,FF,00) . If you want your random colors to be uniformly distributed across all possible colors then just use the fix I gave you earlier.

Link to comment
Share on other sites

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.