Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate random color #32

Open
smoqadam opened this issue Jan 19, 2019 · 4 comments
Open

generate random color #32

smoqadam opened this issue Jan 19, 2019 · 4 comments

Comments

@smoqadam
Copy link

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

@LasseRafn
Copy link
Owner

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

sorry for the late response

@HRankit
Copy link

HRankit commented May 3, 2019

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

@ghost
Copy link

ghost commented Jun 14, 2019

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

@misteregis
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants