Skip to content

Algorithm to find the best foreground color based on the specified background color.

License

Notifications You must be signed in to change notification settings

brlebtag/BestFg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

BestFg

Algorithm to find the best foreground color based on the specified background color.

Extract RGB components from string:

function extractRGB(str) {
    return [parseInt(str.substr(1, 2), 16), parseInt(str.substr(3 ,2), 16), parseInt(str.substr(5, 2), 16)];
}

Get best foreground color:


function getBestFg(str){
    var rgb = extractRGB(str);
    var r = rgb[0], b  = rgb[1], g = rgb[2];
    
    var yiq = ((r*299)+(g*587)+(b*114))/1000;
    
    return (yiq >= 128) ? 'black' : 'white';
}

About

Algorithm to find the best foreground color based on the specified background color.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages