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;
}
RGB Value. Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255. For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0.
RGB is a three-channel format containing data for Red, Green, and Blue. RGBA is a four-channel format containing data for Red, Green, Blue, and an Alpha value. The CSS function rgb() has wide browser support. The CSS function rgba() may have limited support in the older browser.
To display black, set all color parameters to 0, like this: rgb(0, 0, 0). To display white, set all color parameters to 255, like this: rgb(255, 255, 255).
Each color channel is expressed from 0 (least saturated) to 255 (most saturated). This means that 16,777,216 different colors can be represented in the RGB color space.
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));
}
if you just want to random pick 9000 colors, i think you can do it like this:
$color = substr(md5($number),6);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With