Skip to content

Instantly share code, notes, and snippets.

@sandro-pasquali
Created December 11, 2017 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandro-pasquali/bbedbe1adacdb3a31d4946cde050f6ba to your computer and use it in GitHub Desktop.
Save sandro-pasquali/bbedbe1adacdb3a31d4946cde050f6ba to your computer and use it in GitHub Desktop.
get proper font color for background
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