@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
4 lines • 2.77 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import{r as t,m as r,q as e,L as n,t as o,w as a}from"./common-7RW_NFNf.js";const s=(r,o,t)=>(299*r+587*o+114*t)/1e3>=128?-1:1,i=(o,t,n,a=1)=>{const u=[Math.max(Math.min(Math.round(t[0]+a*Math.max(1,t[0]/100)),255),0),Math.max(Math.min(Math.round(t[1]+a*Math.max(1,t[1]/100)),255),0),Math.max(Math.min(Math.round(t[2]+a*Math.max(1,t[2]/100)),255),0)],l=e(r.hex(`rgba(${o.join(",")},1)`),r.hex(`rgba(${u.join(",")},1)`)),f=u[0]+u[1]+u[2];return 0===f||765===f||l>n?{background:o,foreground:u,contrast:l}:i(o,u,n,a)},l=new Map,u=(r,o,t,n=1)=>{if(l.has(r))return l.get(r);const e=i(r,o,t,n);return l.set(r,e),e},f=(o,n=7)=>{let e=[0,0,0,1],a=[255,255,255,1];"string"==typeof o?(e=t(o),a=e):"object"==typeof o&&null!==o&&"string"==typeof o.background&&"string"==typeof o.foreground&&(e=t(o.background),a="string"==typeof o.foreground?t(o.foreground):e);const l=s(e[0],e[1],e[2]),i=u([e[0],e[1],e[2]],[a[0],a[1],a[2]],n,l);return a=[...i.foreground,1],{background:r.hex(`rgba(${e.join(",")})`),foreground:r.hex(`rgba(${a.join(",")})`),contrast:i.contrast}},c=/^#((\d|[a-f]){8}|(\d|[a-f]){6}|(\d|[a-f]){3,4})$/i;function g(r){return c.test(r)}function p(r){if(r.startsWith("{"))try{const o=JSON.parse(r);if(d(o))return{type:"ColorPair",value:o}}catch(r){return{type:null,value:null}}return{type:null,value:null}}function y(r){if(r)if("string"==typeof r){if(g(r))return{type:"string",valid:!0,value:r};{const o=p(r);if(o.value)return{type:o.type,valid:!0,value:o.value}}}else{const o=r;if(d(o))return{type:"ColorPair",valid:!0,value:o}}return{type:null,valid:!1,value:""}}function d(r){return!("object"!=typeof r||!r||"string"!=typeof r.backgroundColor||!("string"==typeof r.foregroundColor||r.foregroundColor&&"string"==typeof r.foregroundColor.primary&&"string"==typeof r.foregroundColor.secondary&&"string"==typeof r.foregroundColor.neutral))}function h(r){const o=y(r);switch(o.type){case null:return!1;case"string":case"ColorPair":return o.valid}}const v=(r,o,t)=>{a(r,"_color",h,new Set(["rgb in hex","ColorPair"]),o,t)},b=r=>{var t;let e;const a=y(r);switch(a.type){case"string":e=f(a.value);break;case"ColorPair":{const r=a.value;let o="";"string"==typeof r.foregroundColor?o=r.foregroundColor:(null===(t=r.foregroundColor)||void 0===t?void 0:t.primary)&&(o=r.foregroundColor.primary),o&&"string"==typeof o||(o="#fff"),e=f({background:r.backgroundColor,foreground:o});break}case null:n.warn(`_color was empty or invalid (${JSON.stringify(r)})`),e=f({background:"#000",foreground:"#000"})}return e.contrast<7&&o(`[KolBadge] The contrast of ${e.contrast} (≥7, AAA) is to low, between the color pair ${e.background} and ${e.foreground}.`),{backgroundColor:e.background,foregroundColor:e.foreground}};export{b as h,v};