Skip to content

Instantly share code, notes, and snippets.

@pablorecio
Created June 24, 2013 15:38
Show Gist options
  • Save pablorecio/5850956 to your computer and use it in GitHub Desktop.
Save pablorecio/5850956 to your computer and use it in GitHub Desktop.
Simple jQuery module for fixing contrast according to W3 formula: http://www.w3.org/TR/AERT#color-contrast
if(jQuery) (function($) {
$.extend($.fn, {
fixContrast: function() {
$(this).each(function(element){
var backgroundcolor = $(this).css('background-color');
var rgb = backgroundcolor.replace("rgb(", "").replace(")", "").split(", ");
var ratio = Math.round((
(parseInt(rgb[0]) * 299) +
(parseInt(rgb[1]) * 587) +
(parseInt(rgb[2]) * 114)
) /1000);
if(ratio > 125) {
$(this).css('color', 'white');
}else{
$(this).css('color', 'black');
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment