Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP int to unique rgb color [closed]

Tags:

php

int

colors

rgb

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;
}
like image 899
user5706 Avatar asked Jun 12 '12 08:06

user5706


People also ask

What is argb color code?

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.

What is the difference between RGBA and RGB?

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.

Which of the following colors is represented by RGB 0 255 255)?

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).

How many colors in RGB()?

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.


2 Answers

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));
}
like image 118
Akarun Avatar answered Sep 21 '22 23:09

Akarun


if you just want to random pick 9000 colors, i think you can do it like this:

$color = substr(md5($number),6);
like image 27
Marcus Avatar answered Sep 19 '22 23:09

Marcus