Skip to content

Instantly share code, notes, and snippets.

@Flyer53
Last active December 11, 2015 18:43
Show Gist options
  • Save Flyer53/28a6793a6d36ab2d1b02 to your computer and use it in GitHub Desktop.
Save Flyer53/28a6793a6d36ab2d1b02 to your computer and use it in GitHub Desktop.
Plugin for one-color https://github.com/One-com/one-color. Function calculates perceived brightness of a passed color.
function pB (algo) {
var algorithm = algo || 'rec2020';
var rgb = this.rgb();
if (algorithm === 'rec2020') {
// https://en.wikipedia.org/wiki/Rec._2020
return ( (rgb._red * 2627) + (rgb._green * 6780) + (rgb._blue * 593) ) / 10000;
} else if (algorithm === 'w3c') {
// http://www.w3.org/TR/AERT#color-contrast
return ( (rgb._red * 299) + (rgb._green * 587) + (rgb._blue * 114) ) / 1000;
} else if (algorithm === 'nbd') {
// http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
return Math.sqrt( (Math.pow(rgb._red, 2) * .241) + (Math.pow(rgb._green, 2) * .691) + (Math.pow(rgb._blue, 2) * .068) );
} else if (algorithm === 'rec709') {
// https://en.wikipedia.org/wiki/Relative_luminance ; https://en.wikipedia.org/wiki/Rec._709
return ( (rgb._red * 241) + (rgb._green * 691) + (rgb._blue * 68) ) / 1000;
} else {
// default rec2020
return ( (rgb._red * 2627) + (rgb._green * 6780) + (rgb._blue * 593) ) / 10000;
}
}
one.color.installMethod('perceivedBrightness', pB);
//ONECOLOR.installMethod('perceivedBrightness', pB);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment