Git Product home page Git Product logo

Comments (8)

LasseRafn avatar LasseRafn commented on May 31, 2024 2

Thanks for adding this! 👍 I’ll put some work into this

from ui-avatars.

3D-I avatar 3D-I commented on May 31, 2024 2

Another interesting function related and very similar. That's JS though.

https://github.com/tcat-tamu/sda.site.web/blob/342fde2e3d840e317b9e0931594c36e165a7c3ba/apps/site/src/app/vwise/components/panel/panel.directive.js#L177

from ui-avatars.

HsRealDev avatar HsRealDev commented on May 31, 2024 1

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.

3D-I avatar 3D-I commented on May 31, 2024 1

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.

LasseRafn avatar LasseRafn commented on May 31, 2024 1

Thanks you so much for this, @HsRealDev! I will test this and maybe make some adjustments (haven't tested it yet)

from ui-avatars.

HsRealDev avatar HsRealDev commented on May 31, 2024

@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.

HsRealDev avatar HsRealDev commented on May 31, 2024

@LasseRafn You are welcome! Good to hear I could be some help back since your API saved me some hours :-)

from ui-avatars.

3D-I avatar 3D-I commented on May 31, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.