I have the numbers 1000 to 9999. I would like to have a function that gets an unique color in rgb for each number. The colors needs to be the same for each number every time the method is launched. Also the colors need to be very different when adding a number. So number 1 can be dark red, 2 light green, etc.
Hopefully some one knows how to do this! :)
This is what I got so far, but the colors are almost identical, thats not what I want!
for ($i = 1000; $i < 2099; $i++) {
$rgb = getRGB(dechex($i));
echo '<div style="width: 800px; height: 30px; margin-bottom: 10px; background-color: ' . rgb2html($rgb['red'], $rgb['green'], $rgb['blue']) . ';"></div>';
}
function getRGB($psHexColorString) {
$aColors = array();
if ($psHexColorString{0} == '#') {
$psHexColorString = substr($psHexColorString, 1);
}
$aColors['red'] = @hexdec($psHexColorString{0} . $psHexColorString{1});
$aColors['green'] = @hexdec($psHexColorString{2} . $psHexColorString{3});
$aColors['blue'] = @hexdec($psHexColorString{4} . $psHexColorString{5});
return $aColors;
}
function rgb2html($r, $g = -1, $b = -1) {
if (is_array($r) && sizeof($r) == 3)
list($r, $g, $b) = $r;
$r = intval($r);
$g = intval($g);
$b = intval($b);
$r = dechex($r < 0 ? 0 : ($r > 255 ? 255 : $r));
$g = dechex($g < 0 ? 0 : ($g > 255 ? 255 : $g));
$b = dechex($b < 0 ? 0 : ($b > 255 ? 255 : $b));
$color = (strlen($r) < 2 ? '0' : '') . $r;
$color .= (strlen($g) < 2 ? '0' : '') . $g;
$color .= (strlen($b) < 2 ? '0' : '') . $b;
return '#' . $color;
}
You can use the php function "dechex" to convert integer to Hexa RGB :
http://php.net/manual/en/function.dechex.php
In this page there is this function :
function toColor($n) {
return("#".substr("000000".dechex($n),-6));
}
Not tested but can be help.
EDIT after comment : you can add some pseudo-randomiser in the function:
function toColor($n) {
$n = crc32($n);
$n &= 0xffffffff;
return("#".substr("000000".dechex($n),-6));
}