Skip to content

Instantly share code, notes, and snippets.

@richardbutler
Forked from kozo002/jquery.brightness.js
Last active December 24, 2015 23:49
Show Gist options
  • Save richardbutler/6882966 to your computer and use it in GitHub Desktop.
Save richardbutler/6882966 to your computer and use it in GitHub Desktop.
Quick fork of jquery.brightness.js, but without the jQuery dependency.
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