php-initial-avatar-generator
php-initial-avatar-generator copied to clipboard
generate random color
Hi Lasse, thank you for this pacakge.
Lately, I used this package for my project. I needed to generate a random color for each user. here is my commit if you find it useful let me know to send you a PR.
Thanks
Hi, thank you for this! I'll add this into it 👍 would you care to do a PR?
sorry for the late response
Hey @LasseRafn You can use the colors from https://gist.github.com/HRankit/5fc47a4dc534a4a769992e05b86ab43d . It has most of material design colors.
maybe this:
function textToColor($str)
{
if (empty($str)) {
return false;
}
$color = '#';
$hash = 0;
for ($i = 0; $i < mb_strlen($str); $i++) {
$char = mb_substr($str, $i, 1, 'UTF-8');
$hash = hexdec(bin2hex($char)) + (($hash << 5) - $hash);
}
for ($i = 0; $i < 3; $i++) {
$char_hash = ($hash >> $i * 2) & 0xFF;
$char_hash_hex = dechex($char_hash);
$col = '00' . $char_hash_hex;
$color .= mb_substr($col, -2);
}
return $color;
}
Hi @LasseRafn, I don't know if you already have any function in your script to load the color by name but anyway I created a function, see below: