Last active
April 23, 2024 15:47
-
-
Save ravanscafi/8e1760bfda26c5dc1657 to your computer and use it in GitHub Desktop.
Random Color from Input's MD5 Hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generate a consistent* random color for a text input. | |
* Consistent because it's based on MD5 hash for the input. | |
* Since both MD5 and CSS colors uses hexa, a substring of the MD5 can be used. | |
* Clever, huh? | |
* | |
* PS: you can get any substring of length 3 (for less colors) or 6 (for more colors) from your hash. Just don't | |
* randomize it, or you will loose consistency. | |
* | |
* @param string $input | |
* @return string | |
*/ | |
function cssColorForString($input) | |
{ | |
return '#' . substr(md5($input), 0, 6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment