LasseRafn / ui-avatars

MIT License
1.05k stars 138 forks source link

Show Random Background when background=random is passed #30

Closed HRankit closed 3 years ago

HRankit commented 5 years ago

This was a feature which was missing from it. Showing the same old static colors or managing the colors while sending from the url can be a bit lot of work for some lazy developers like me. So this pull request should be able to solve that problem. Avoid passing color as the color or text-color is automatically set to black and white. This solves issue #28

violuke commented 4 years ago

Rather than a totally random colour each time (or as an additional option), how about using something like Random Color and the prng option to get the same "random" colour each time. We use this lib to generate the colours we send over in the URL with prng being the user's ID in our database. It would be helpful for users to have to the same colour each time.

batyshkaLenin commented 4 years ago

It`s awesome PR, you my hero! I hope soon it will be available.

HRankit commented 4 years ago

Rather than a totally random colour each time (or as an additional option), how about using something like Random Color and the prng option to get the same "random" colour each time. We use this lib to generate the colours we send over in the URL with prng being the user's ID in our database. It would be helpful for users to have to the same colour each time.

@violuke I didn't know about Random Color library. Thank you for letting me know. A doubt though, using the library, how do you guys set the text-color to black or white? As you cannot predict which color is gonna be sent by Random Color. Shouldn't text-color be in contrast?? How do you guys manage that?

HRankit commented 4 years ago

It`s awesome PR, you my hero! I hope soon it will be available.

@batyshkaLenin I highly doubt it would be available. It's been sitting here since May 3, 2019 with no response.

violuke commented 4 years ago

@HRankit we use the 'luminosity' => 'light' option and then always submit the text colour as black. It's a bit of a cop-out vs generating a contrasting colour, but it results in some good looking avatars :boom:

Screenshot 2020-04-22 at 4 03 08 pm
HRankit commented 4 years ago

@violuke Thank you. 'luminosity' => 'light' should do the trick. I'll try it on my project too.

LasseRafn commented 4 years ago

Could we somehow use the limunosity thing?

I’ll make a minor change to this PR, just to avoid reading from JSON, but otherwise looks good! 👍🏻

Apologies, it went past me in my inbox

kamaroolkarim commented 3 years ago

any updates on this PR?

LasseRafn commented 3 years ago

Let me quickly test it locally and merge 😄 sorry

LasseRafn commented 3 years ago

Sorry I added through another commit — copied your code with minor adjustment (I worry loading from json will break the server at ~30M requests/day, PHP is rather slow at JSON)

kamaroolkarim commented 3 years ago

cool. thanks @LasseRafn

LasseRafn commented 2 years ago

Not working now!!

https://ui-avatars.com/api/?background=random

always generate a same color

Ah yes, it's due to cache. It will generate a new color for unique names.

If random background for same user is necessary for you, you could append a random string or ID as a query parameter to bust the cache.

Eg: /api?name=test&background=random&cache-buster=12345abcdef