Skip to content

Instantly share code, notes, and snippets.

@ravanscafi
Last active April 23, 2024 15:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravanscafi/8e1760bfda26c5dc1657 to your computer and use it in GitHub Desktop.
Save ravanscafi/8e1760bfda26c5dc1657 to your computer and use it in GitHub Desktop.
Random Color from Input's MD5 Hash
<?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