Skip to content

Instantly share code, notes, and snippets.

@jacquesbh
Created June 25, 2012 19:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacquesbh/2990850 to your computer and use it in GitHub Desktop.
Save jacquesbh/2990850 to your computer and use it in GitHub Desktop.
Hexa colors in PHP loop
<?php
define('PAD', 1);
define('WIDTH', 1);
define('HEIGHT', 1);
ob_start();
echo '<table cellspacing="0" cellpadding="0" border="0" style="margin: 0; padding: 0;">';
for ($cell = 0, $col = 1, $line = 1, $nbLines = (255/PAD), $nbCells = $nbLines * $nbLines; $cell < $nbCells; $cell+=PAD) {
if ($cell % $nbLines == 0) {
echo "\n",'<tr>';
$col = 1;
$line++;
} else {
$col++;
}
$red = str_pad(dechex($nbLines - $line), 2, '0', STR_PAD_LEFT);
$green = str_pad(dechex($col), 2, '0', STR_PAD_LEFT);
$blue = str_pad(dechex($line), 2, '0', STR_PAD_LEFT);
$color = '#' . $red . $green . $blue;
echo '<td style="width:'.WIDTH.'px;height:'.HEIGHT.'px;background-color:'.$color.';"></td>';
if (($cell+1) % $nbLines == 0) {
echo '</tr>';
}
}
echo '</table>';
ob_end_flush();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment