UNPKG

text-contrast

Version:

JavaScript Library for Setting Text Color According to Background-Color

2 lines 4.17 kB
!function(r,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("textContrast",[],t):"object"==typeof exports?exports.textContrast=t():r.textContrast=t()}("undefined"!=typeof self?self:this,function(){return function(r){var t={};function e(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return r[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=r,e.c=t,e.d=function(r,t,n){e.o(r,t)||Object.defineProperty(r,t,{enumerable:!0,get:n})},e.r=function(r){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})},e.t=function(r,t){if(1&t&&(r=e(r)),8&t)return r;if(4&t&&"object"==typeof r&&r&&r.__esModule)return r;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:r}),2&t&&"string"!=typeof r)for(var o in r)e.d(n,o,function(t){return r[t]}.bind(null,o));return n},e.n=function(r){var t=r&&r.__esModule?function(){return r.default}:function(){return r};return e.d(t,"a",t),t},e.o=function(r,t){return Object.prototype.hasOwnProperty.call(r,t)},e.p="",e(e.s=0)}([function(r,t,e){"use strict";function n(r,t){return function(r){if(Array.isArray(r))return r}(r)||function(r,t){var e=[],n=!0,o=!1,i=void 0;try{for(var u,a=r[Symbol.iterator]();!(n=(u=a.next()).done)&&(e.push(u.value),!t||e.length!==t);n=!0);}catch(r){o=!0,i=r}finally{try{n||null==a.return||a.return()}finally{if(o)throw i}}return e}(r,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function o(r,t,e){var n=function(r){return window.getComputedStyle(r)["background-color"]}(r);r.style.color=isLight(n)?e:t}function i(r){var t=Object.prototype.toString.call(r).match(/\[.* (.*)\]/)[1].toLowerCase();return"array"==t||"htmlcollection"==t||"nodelist"==t}function u(r){if(!i(r))throw new Error("RGB array not valid");r.forEach(function(r){if(t=r,isNaN(parseFloat(t))||!isFinite(t))throw new Error("RGB component ".concat(r," not valid"));var t});var t=n(r,3);return(299*t[0]+587*t[1]+114*t[2])/1e3}function a(r){var t,e,o;if("string"==typeof r){var u;switch(function(r){if(!r)return;return r=r.toString().trim().toUpperCase(),/^#?([0-9a-fA-F]{3}){1,2}$/.test(r)?"hex":/RGB/.test(r)?"rgb":void 0}(r)){case"hex":u=function(r){if("string"!=typeof r)throw new Error("Invalid hex code");var t=r.replace("#","").toUpperCase();if(t.length%3!=0)throw new Error("Invalid hex code");3===t.length&&(t=t.replace(/(.)(.)(.)/,"$1$1$2$2$3$3"));if(!/[0-9A-F]{6}/.test(t))throw new Error("Invalid hex code");var e=parseInt(t.substr(0,2),16),n=parseInt(t.substr(2,2),16),o=parseInt(t.substr(4,2),16);return[e,n,o]}(r);break;case"rgb":u=function(r){var t=r.match(/rgba?\((\d+),\s?(\d+),\s?(\d+)/);if(!t)return;var e=n(t,4),o=(e[0],e[1]),i=e[2],u=e[3];return[o,i,u].map(function(r){return parseInt(r)})}(r);break;default:throw new Error("Unknown color type - ".concat(r))}t=u[0],e=u[1],o=u[2]}else{if(!i(r))throw new Error("Unknown color type - ".concat(r));t=r[0],e=r[1],o=r[2]}return[t,e,o].forEach(function(r){if(!/^[0-9]{1,3}$/.test(r))throw new Error("Error converting color to RGB array")}),[t,e,o]}function c(r){return function(r){if(Array.isArray(r)){for(var t=0,e=new Array(r.length);t<r.length;t++)e[t]=r[t];return e}}(r)||function(r){if(Symbol.iterator in Object(r)||"[object Arguments]"===Object.prototype.toString.call(r))return Array.from(r)}(r)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}e.r(t),e.d(t,"textContrast",function(){return f});var f=function(){function r(r){var t=a(r);if(t)return u(t)>=128}return{fix:function r(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"white",n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"black";return i(t)?c(t).forEach(function(t){return r(t,e,n)}):"string"==typeof t?c(document.querySelectorAll(t)).forEach(function(t){return r(t,e,n)}):o(t,e,n)},isLight:r,isDark:function(t){var e=r(t);return void 0===e?void 0:!e},isLightOrDark:function(t){return r(t)?"light":"dark"}}}()}])}); //# sourceMappingURL=text-contrast.min.js.map