How to transform a hash into a color

4

How to transform a hash into a random color .. Notice that I used the hash expression just to say that a cluster of letters / numbers can be transformed into color like this:

d0be2dc421be4fcd0172e5afceea3970e2f3d940

is non-nonsense, nonsense but whenever this hash is used, it will give a specific color and if it is changed the final 0 by 1 will show another color .. or even if any part of the hash it will show different colors.

>

Can you understand ?, the color format I would like to be RGB but it can be in the style #FFFFFF I do not distinguish, I thought of something like this: Take three parts of the hash one at the beginning another in the middle another at the end and leave but I do not know how to generate those colors after getting these parts I have come to help ..

    
asked by anonymous 09.08.2015 / 17:54

2 answers

4

Responding directly to the question how to transform a hash into a color will be:

function hashToColor($hash) {
  $code = dechex(crc32($hash));
  $code = substr($code, 0, 6);
  return $code;
}

Obviously the example can be optimized but this is the solution I give you. Simple and straightforward for easy understanding.

However, you should consider the use of CRC32 soon of origin having obviously in mind the possibility of collisions etc.

    
09.08.2015 / 20:14
0

Since there is no relation between its hash and the color it will represent, this process is totally random.

You did not specify how this hash is being generated so I'll just show you how you can generate the value in hexadecimal color

echo sprintf("#%06x",rand(0,16777215));

This code will generate values like: #7bdc39 .

Now you can associate each new hash with this command.

    
09.08.2015 / 19:46