How to turn a hash into a color

Asked

Viewed 161 times

4

How to turn a hash into a random color.. Notice that I used the expression "hash" only to say that a cluster of letters/numbers can be transformed into colors, like this:

d0be2dc421be4fcd0172e5afceea3970e2f3d940

is meaningless, meaningless but whenever this hash is used it will give a specific color and if the end is changed 0 for 1 will show another color.. or even if any part of the hash is changed it will show different colors.

Note: I think there are infinite ways to mount a hash but I’m not sure if there are infinite colors so it’s okay if different hashs give an equal color (even if it’s not ideal)

Can you understand? , the color format I would like it to be RGB but can be in style #FFFFFF I don’t make a distinction, I thought of something like this: Take three parts of the hash one at the beginning another in the middle other at the end and work from it to pick up colors, but as I don’t know how to generate these colors after picking up these parts I come to ask help..

  • You can do in array and mount a reading function as the hash passed... If hash x returns color x but returns color y

  • You’ll store these hashs and colors somewhere?

2 answers

4


By directly answering the question the way to turn 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 direct for easy understanding.

However, the use of CRC32 logo of origin obviously taking into account the possibility of collisions etc.

0

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

You didn’t specify like this hash this being generated then I will show only how you can generate the value for colors in the format hexadecimal

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

This code will generate values such as: #7bdc39.

Now you can associate each new hash with this command.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.