Skip to content

Instantly share code, notes, and snippets.

@Erenor
Last active February 16, 2020 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Erenor/260209032cedc9eb3bf583c00f27d878 to your computer and use it in GitHub Desktop.
Save Erenor/260209032cedc9eb3bf583c00f27d878 to your computer and use it in GitHub Desktop.
[PHP] A simple function to get a 6-chars hexadecimal color starting from any string
/**
* Function takes a random string and returns an hex color in the 6-digits format
*
* @param string $input
*
* @return string
*/
function meow_str_to_hex_color($input) {
//characters that we do not want into an hex color
$replace = range('g', 'z');
//md5 of the string (random chars, but always the same for the same start string)
$str = md5($input);
//remove unwanted chars
$str = str_replace($replace, '', $str);
//check lenght
if (strlen($str) < 6) {
//uhhh too short! make it 6-chars long with some zeros
$str = str_pad ($str , 6, 0, STR_PAD_LEFT);
}
else {
//oh, too long: use first 6 chars
$str = substr($str, 0, 6);
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment