Created
June 24, 2013 15:38
-
-
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
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
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