Posted By

headloose on 08/16/11


Tagged

color contrast accessilibity


Versions (?)

jQ contrast


 / Published in: jQuery
 

URL: headloose.com

a jQuery function to set a class depending on whether it is high or low contrast

  1. $.fn.contrast = function (cl1, cl2) {
  2. var m,
  3. yiq;
  4. return this.each(function () {
  5. m = $(this).css('background-color').match(/^\D+(\d+)\D+(\d+)\D+(\d+)\D+(?:([\d\.]+)\D+)?$/);
  6. if(c.transparent !== true) {
  7. yiq = ((m[1]*299)+(m[2]*587)+(m[3]*114))/1000;
  8. $(this).addClass(((yiq >= 128) ? cl1 : cl2));
  9. } else {
  10. $(this).addClass(cl1);
  11. }
  12. });
  13. };

Report this snippet  

You need to login to post a comment.