UNPKG

contrast-ratio-checker

Version:

A package based on WCAG 2.0 to easily check contrast ratio.

1 lines 4.07 kB
!function(t,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define([],o):"object"==typeof exports?exports.ContrastRatioChecker=o():t.ContrastRatioChecker=o()}(this,(()=>(()=>{"use strict";var t={};return{590:function(t,o){var e=this&&this.__assign||function(){return e=Object.assign||function(t){for(var o,e=1,r=arguments.length;e<r;e++)for(var a in o=arguments[e])Object.prototype.hasOwnProperty.call(o,a)&&(t[a]=o[a]);return t},e.apply(this,arguments)};Object.defineProperty(o,"__esModule",{value:!0}),o.ContrastRatioChecker=void 0;var r=function(){function t(){this.PX_TO_PT_RATIO=3/4}return t.prototype.isValidHex=function(t){try{return/^#(([0-9a-f]{3})|([0-9a-f]{6}))$/gi.test(t)}catch(t){throw Error("isValidHex failed. ".concat(t))}},t.prototype.getFullHex=function(t){try{return 6===t.length?t:t.split("").map((function(t){return"".concat(t).concat(t)})).join("")}catch(t){throw Error("getFullHex failed. ".concat(t))}},t.prototype.separateStringPerCharGroup=function(t,o){try{var e=new RegExp(".{1,".concat(o,"}"),"g");return t.match(e)}catch(t){throw Error("separateStringPerCharGroup failed. ".concat(t))}},t.prototype.convertHexToRgb=function(t){try{if(!this.isValidHex(t))throw Error("Invalid hexadecimal string");var o=t.slice(1),e=this.getFullHex(o);return this.separateStringPerCharGroup(e,2).reduce((function(t,o,e){var r=parseInt(o,16);switch(e){case 0:return t.red=r,t;case 1:return t.green=r,t;case 2:return t.blue=r,t}}),{})}catch(t){throw Error("convertHexToRgb failed. ".concat(t))}},t.prototype.getRelativeLuminanceComponent=function(t){try{return t<=.03928?t/12.92:Math.pow((t+.055)/1.055,2.4)}catch(t){throw Error("getRelativeLuminanceComponent failed. ".concat(t))}},t.prototype.getLuminance=function(t){try{var o=t.red/255,e=t.green/255,r=t.blue/255;return.2126*this.getRelativeLuminanceComponent(o)+.7152*this.getRelativeLuminanceComponent(e)+.0722*this.getRelativeLuminanceComponent(r)}catch(t){throw Error("getLuminance failed. ".concat(t))}},t.prototype.getContrastRatioByLuminance=function(t,o){try{return(Math.max(t,o)+.05)/(Math.min(t,o)+.05)}catch(t){throw Error("getContrastRatioByLuminance failed. ".concat(t))}},t.prototype.getContrastRatioByRgb=function(t,o){try{var e=this.getLuminance(t),r=this.getLuminance(o);return this.getContrastRatioByLuminance(e,r)}catch(t){throw Error("getContrastRatioByRgb failed. ".concat(t))}},t.prototype.getContrastRatioByHex=function(t,o){try{var e=this.convertHexToRgb(t),r=this.convertHexToRgb(o);return this.getContrastRatioByRgb(e,r)}catch(t){throw Error("getContrastRatioByHex failed. ".concat(t))}},t.prototype.getWcagLargeTextValidation=function(t,o){try{var e=t*this.PX_TO_PT_RATIO;return!!(e>=18||e>=14&&o)}catch(t){throw Error("getWcagLargeTextValidation failed. ".concat(t))}},t.prototype.getCustomRatioValidation=function(t,o){void 0===o&&(o=null);try{return o?{CUSTOM:t>=o}:{}}catch(t){throw Error("getCustomRatioValidation failed. ".concat(t))}},t.prototype.getWcagRatioValidation=function(t,o,e){void 0===o&&(o=16),void 0===e&&(e=!1);try{var r=this.getWcagLargeTextValidation(o,e);return{WCAG_AA:t>=(r?3:4.5),WCAG_AAA:t>=(r?4.5:7)}}catch(t){throw Error("getWcagRatioValidation failed. ".concat(t))}},t.prototype.getRatioValidationByRgb=function(t,o,r){void 0===r&&(r=null);try{var a=this.getContrastRatioByRgb(t,o),n=this.getWcagRatioValidation(a,null==r?void 0:r.fontSizePx,null==r?void 0:r.bold),i=n.WCAG_AA,c=n.WCAG_AAA,u=this.getCustomRatioValidation(a,null==r?void 0:r.custom);return e({WCAG_AA:i,WCAG_AAA:c},u)}catch(t){throw Error("getRatioValidationByRgb failed. ".concat(t))}},t.prototype.getRatioValidationByHex=function(t,o,r){void 0===r&&(r=null);try{var a=this.getContrastRatioByHex(t,o),n=this.getWcagRatioValidation(a,null==r?void 0:r.fontSizePx,null==r?void 0:r.bold),i=n.WCAG_AA,c=n.WCAG_AAA,u=this.getCustomRatioValidation(a,null==r?void 0:r.custom);return e({WCAG_AA:i,WCAG_AAA:c},u)}catch(t){throw Error("getRatioValidationByHex failed. ".concat(t))}},t}();o.ContrastRatioChecker=r}}[590](0,t),t})()));