I stumbled over this blogpost by Brian Suda and quickly applied the "Dopplr’s coloring algorithm" to some examples, and that this works is not in question. What is, is why?

The process is as follows:

$hex_color = sub-string(md5($original_string), 0, 6)

Input some string (e.g. city names as with dopplr's original usage), make a md5-hash of this, and use the first 6 digits from the hash. Prepend this with a "#" and a string is transformed into a color fit for use in CSS-documents etc.

How is it that the six first digits of a md5-hash always matches a color in the hexadecimal representation?

有帮助吗?

解决方案

MD5 always outputs hex. Colors can be represented as a 6-character hex string in css. So, the MD5 hash is just producing a semi-random hex string.

And any hex string (that is 6 characters long and begins with a #) is valid to define a color.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top