php-initial-avatar-generator icon indicating copy to clipboard operation
php-initial-avatar-generator copied to clipboard

generate random color

Open smoqadam opened this issue 6 years ago • 4 comments

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

smoqadam avatar Jan 19 '19 07:01 smoqadam

Hi, thank you for this! I'll add this into it 👍 would you care to do a PR?

sorry for the late response

LasseRafn avatar Apr 18 '19 20:04 LasseRafn

Hey @LasseRafn You can use the colors from https://gist.github.com/HRankit/5fc47a4dc534a4a769992e05b86ab43d . It has most of material design colors.

HRankit avatar May 03 '19 09:05 HRankit

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;
    }

ghost avatar Jun 14 '19 09:06 ghost

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:

color2hex.txt

misteregis avatar Sep 08 '19 04:09 misteregis