-
-
Save richardbutler/6882966 to your computer and use it in GitHub Desktop.
Quick fork of jquery.brightness.js, but without the jQuery dependency.
This file contains 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 brightness(el) { | |
var bgColor, rgba, y, parent; | |
bgColor = el.style.backgroundColor; | |
parent = el.parentNode; | |
function reportParent() { | |
return parent ? brightness(parent) : undefined; | |
} | |
if ((bgColor !== null) && bgColor.length) { | |
rgba = bgColor.match(/^rgb(?:a)?\(([0-9]{1,3}),\s([0-9]{1,3}),\s([0-9]{1,3})(?:,\s)?([0-9]{1,3})?\)$/); | |
if (rgba !== null) { | |
if (rgba[4] === '0') { | |
return reportParent(); | |
} else { | |
y = 2.99 * rgba[1] + 5.87 * rgba[2] + 1.14 * rgba[3]; | |
if (y >= 1275) { | |
return 'light'; | |
} else { | |
return 'dark'; | |
} | |
} | |
} | |
} else { | |
return reportParent(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment