UNPKG

accessible-colors

Version:

Utility functions for working with colors in adherence to WCAG 2.1 guidelines.

8 lines 3.3 kB
/** * @license * author: Christopher Hawke (https://github.com/cdhawke) * accessible-colors.js v1.0.9 * Released under the MIT license. */ "use strict";var b=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var T=(r,t)=>{for(var n in t)b(r,n,{get:t[n],enumerable:!0})},V=(r,t,n,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let l of G(t))!v.call(r,l)&&l!==n&&b(r,l,{get:()=>t[l],enumerable:!(e=B(t,l))||e.enumerable});return r};var j=r=>V(b({},"__esModule",{value:!0}),r);var J={};T(J,{getContrast:()=>w,getLuminance:()=>A,getRandomAAAColor:()=>O,getRandomAAColor:()=>I,isAAAContrast:()=>H,isAAContrast:()=>M,isContrasting:()=>C,randomColor:()=>x,suggestAAAColorVariant:()=>E,suggestAAColorVariant:()=>z});module.exports=j(J);var c=r=>{let t=$(r);return D(t)},m=r=>{let t=p(r);return y(t)},D=r=>{let{r:t,g:n,b:e}=r;return`#${(t<<16|n<<8|e).toString(16).padStart(6,"0")}`},p=r=>{let t=parseInt(r.replace(/^#/,""),16),n=t>>16&255,e=t>>8&255,l=t&255;return{r:n,g:e,b:l}},$=({h:r,s:t,l:n})=>{let e,l,o;if(t==0)e=l=o=n;else{let s=(a,f,g)=>(g<0&&(g+=1),g>1&&(g-=1),g<.16666666666666666?a+(f-a)*6*g:g<.5?f:g<.6666666666666666?a+(f-a)*(.6666666666666666-g)*6:a),i=n<.5?n*(1+t):n+t-n*t,u=2*n-i;e=s(u,i,r+1/3),l=s(u,i,r),o=s(u,i,r-1/3)}return{r:Math.round(e*255),g:Math.round(l*255),b:Math.round(o*255)}},y=({r,g:t,b:n})=>{r/=255,t/=255,n/=255;let e=Math.max(r,t,n),l=Math.min(r,t,n),o,s,i=(e+l)/2;if(e==l)o=s=0;else{let u=e-l;switch(s=i>.5?u/(2-e-l):u/(e+l),e){case r:o=(t-n)/u+(t<n?6:0);break;case t:o=(n-r)/u+2;break;case n:o=(r-t)/u+4;break}o/=6}return{h:o,s,l:i}},L=(r,t,n,e,l)=>{let{l:o,...s}=r,i=n==="lighten"?1:o,u=n==="lighten"?o:0,a=c({...s,l:u}),f=c({...s,l:i}),g=c(t);if(!e(n==="lighten"?f:a,g,l))return null;let S=null,R=null;for(;a!==S||f!==R;){S=a,R=f;let h=(u+i)/2,k=c({...s,l:h});n==="lighten"&&(e(k,g,l)?(i=h,f=c({...s,l:h})):(u=h,a=c({...s,l:h}))),n==="darken"&&(e(k,g,l)?(u=h,a=c({...s,l:h})):(i=h,f=c({...s,l:h})))}return m(n==="lighten"?f:a)},d=(r,t,n,e)=>{let l=m(r),o=m(t);if(!o||!l)return null;if(n(r,t,e))return r;let s=L(l,o,"darken",n,e),i=L(l,o,"lighten",n,e);if(s!==null&&i!==null){let u=Math.abs(l.l-s.l),a=Math.abs(l.l-i.l);return c(u<a?s:i)}return s===null&&i!==null?c(i):i===null&&s!==null?c(s):null};var A=r=>{if(!r)return null;let t=p(r);if(!t)return null;let[n,e,l]=Object.values(t).map(o=>{let s=o/255;return s<=.03928?s/12.92:Math.pow((s+.055)/1.055,2.4)});return .2126*n+.7152*e+.0722*l},w=(r,t,n=3)=>{if(r===null||t===null)return null;let e=A(r),l=A(t);if(e===null||l===null)return null;let[o,s]=[e,l].sort((i,u)=>u-i);return Math.round((o+.05)/(s+.05)*10**n)/10**n},C=(r,t,n)=>{let e=w(r,t);return e?e>=n:null},M=(r,t,n=!1)=>C(r,t,n?3:4.5),H=(r,t,n=!1)=>C(r,t,n?4.5:7),x=()=>`#${Math.floor(Math.random()*16777215).toString(16).padStart(6,"0")}`,I=(r,t=!1)=>{let n=x(),e=0;for(;!M(r,n,t);){if(e++>1e3)return null;n=x()}return n},O=(r,t=!1)=>{let n=x(),e=0;for(;!H(r,n,t);){if(e++>1e3)return null;n=x()}return n},z=(r,t,n)=>d(r,t,M,n),E=(r,t,n)=>d(r,t,H,n);0&&(module.exports={getContrast,getLuminance,getRandomAAAColor,getRandomAAColor,isAAAContrast,isAAContrast,isContrasting,randomColor,suggestAAAColorVariant,suggestAAColorVariant}); //# sourceMappingURL=index.js.map