October 18, 2008

Color chooser with PHP

I made this color chooser in PHP which is a rather simple code. It displays a table of known colors with RGB values ranging from 0 to 255. The table cell onclick property is used to redirect back to the same page by passing the color value through GET.

Here is the code:


<table cellspacing="0" cellpadding="0">

<?php
$inc = 255/5;
for ($r=0; $r<=255; $r+=$inc)
{
echo "<tr>";
for ($g=0; $g<=255; $g+=$inc)
{
for ($b=0; $b<=255; $b+=$inc)
{
$cstr = sprintf ("%02X%02X%02X", $r, $g, $b);
echo "<td width="5px" height="5px" bgcolor="#$cstr"
onclick="window.location='hello.php?g_cstr=$cstr'">n";
echo "</td>n";
}
}
$cstr = sprintf ("%02X%02X%02X", $r, $r, $r);
echo "<td width="5px" height="5px" bgcolor="#$cstr"
onclick="window.location='hello.php?g_cstr=$cstr'">n";
echo "</td>n";
echo "</tr>";
}
?>
</table>

The color you picked was:
<table> <tr>
<?php
$sel_col = $_GET['g_cstr'];
echo "<td width="25px" bgcolor="$sel_col"></td>n";
echo "<td>$sel_col</td>";
?>
</tr> </table>



The output will be something like this:


Peace ..V,