Created
December 11, 2017 20:16
-
-
Save sandro-pasquali/bbedbe1adacdb3a31d4946cde050f6ba to your computer and use it in GitHub Desktop.
get proper font color for background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getAccessibleColor(rgb) { | |
let [ r, g, b ] = rgb; | |
let colors = [r / 255, g / 255, b / 255]; | |
let c = colors.map((col) => { | |
if (col <= 0.03928) { | |
return col / 12.92; | |
} | |
return Math.pow((col + 0.055) / 1.055, 2.4); | |
}); | |
let L = (0.2126 * c[0]) + (0.7152 * c[1]) + (0.0722 * c[2]); | |
return (L > 0.179) | |
? [ 0, 0, 0 ] | |
: [ 255, 255, 255 ]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment