Comments (8)
Thanks for adding this! 👍 I’ll put some work into this
from ui-avatars.
Another interesting function related and very similar. That's JS though.
from ui-avatars.
Not sure if it's any help, but here is a piece of code what I'm currently using to create/calculate background and font colors based on the hash of name (so that they still stay constant throughout the application):
$backgrounColor = substr(md5($userId)), 0, 6);
$fontColor = 'fff';
$colors = [
'red' => base_convert(substr($backgrounColor, 0, 2), 16, 10),
'green' => base_convert(substr($backgrounColor, 2, 2), 16, 10),
'blue' => base_convert(substr($backgrounColor, 4, 2), 16, 10)
];
// Custom - more light - calculation for fontcolor.
// This was found somewhere from interwebz but cannot find the url.
// So cannot give the thanks to right person. :(
if(($colors['red'] * 0.299 + $colors['green'] * 0.587 + $colors['blue'] * 0.114) > 186) {
$fontColor = '000';
}
// Contrast calculation based on the W3C Recommendations - https://www.w3.org/TR/WCAG20/
// foreach ($colors as $colorCode => $colorValue) {
// $c = $colorValue / 255;
// if($c <= 0.03928) {
// $colors[$colorCode] = $c / 12.92;
// } else {
// $colors[$colorCode] = (($c+0.055)/1.055) ^ 2.4;
// }
// }
//
// $luminance = 0.2126 * $colors['red'] +
// 0.7152 * $colors['green'] +
// 0.0722 * $colors['blue'];
//
// if($luminance > 0.179) {
// $fontColor = '000';
// }
from ui-avatars.
I am really interested in using this kind of approach and maybe also a randomness of sort, may I use/modify the present code giving credits where those are due? Thx.
Could the snippet make the use of more colors related to letters (say 23 or so on)?
Didn't accurately read/tested it..
from ui-avatars.
Thanks you so much for this, @HsRealDev! I will test this and maybe make some adjustments (haven't tested it yet)
from ui-avatars.
@3D-I - yes, of course you can use it and modify as much as you want. I'm using that to call Lasse's awesome api in my proof of concept projects and I calculate the color always based on the user's ID (in my case MongoId). This way the the avatar's will look like "random" but still be always the same for the user throughout the app.
from ui-avatars.
@LasseRafn You are welcome! Good to hear I could be some help back since your API saved me some hours :-)
from ui-avatars.
Any news on this? I am bound to rewrite my phpBB extension from scratch and I will be more than happy to have this function working. Thanks.
from ui-avatars.
Related Issues (20)
- Add support for gradient backgrounds HOT 2
- No usage tracking HOT 2
- First load error HOT 1
- Custom font name when requesting .svg
- Fatal error: Uncaught Error: Class 'Utils\Input' not found HOT 1
- ui-avatars.com is down again HOT 6
- Gravatar strips the spaces in the name from default URL HOT 3
- receiving duplicate cors headers
- The deployed website does not match the code on GitHub
- Duplicate response headers returned
- ui-avatars.com is down - return Error 502
- Directory URLs not working (incl Gravatar fallbacks) HOT 7
- Dockerfile fails to start HOT 1
- "Édward Doe" returns "ED" when it should return "ÉD"
- Error in avatar - User with special char "&" HOT 2
- feat: Unique (background)color based on text HOT 2
- Monospace Font? HOT 1
- Use Own Avatars with the API
- Can't generate avatar with three initials (including middle initial) HOT 2
- Error when value of param `name` contains `<` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ui-avatars.