Skip to content

Instantly share code, notes, and snippets.

@pid
Forked from kozo002/jquery.brightness.js
Created October 8, 2013 13:25
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 pid/6884593 to your computer and use it in GitHub Desktop.
Save pid/6884593 to your computer and use it in GitHub Desktop.
jQuery.fn.brightness = function() {
var bg_color, rgba, y;
bg_color = this.css('background-color');
if ((bg_color != null) && bg_color.length) {
rgba = bg_color.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') {
if (this.parent().length) return this.parent().brightness();
} else {
y = 2.99 * rgba[1] + 5.87 * rgba[2] + 1.14 * rgba[3];
if (y >= 1275) {
return 'light';
} else {
return 'dark';
}
}
}
} else {
if (this.parent().length) return this.parent().brightness();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment