UNPKG

@public-ui/components

Version:

Contains all web components that belong to KoliBri - The accessible HTML-Standard.

4 lines 3.2 kB
/*! * KoliBri - The accessible HTML-Standard */ import{A as t,B as r,C as n,D as e,E as o,L as a,q as s,w as i}from"./common-Dd0zJQbD.js";function u(o){var a;Array.isArray(o)&&o.raw&&(o=String.raw(...arguments));var e=t(o);if(!e.space)return[];const u="h"===e.space[0]?r.min:n.min,s="h"===e.space[0]?r.max:n.max;return(a=Array(3))[0]=Math.min(Math.max(e.values[0],u[0]),s[0]),a[1]=Math.min(Math.max(e.values[1],u[1]),s[1]),a[2]=Math.min(Math.max(e.values[2],u[2]),s[2]),"h"===e.space[0]&&(a=r.rgb(a)),a.push(Math.min(Math.max(e.alpha,0),1)),a}const f=(r,o,t)=>(299*r+587*o+114*t)/1e3>=128?-1:1,l=(r,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)],s=o(e.hex(`rgba(${r.join(",")},1)`),e.hex(`rgba(${u.join(",")},1)`)),i=u[0]+u[1]+u[2];return 0===i||765===i||s>n?{background:r,foreground:u,contrast:s}:l(r,u,n,a)},c=new Map,g=(r,o,t,n=1)=>{if(c.has(r))return c.get(r);const a=l(r,o,t,n);return c.set(r,a),a},h=(r,o=7)=>{let t=[0,0,0,1],n=[255,255,255,1];"string"==typeof r?(t=u(r),n=t):"object"==typeof r&&null!==r&&"string"==typeof r.background&&"string"==typeof r.foreground&&(t=u(r.background),n="string"==typeof r.foreground?u(r.foreground):t);const a=f(t[0],t[1],t[2]),s=g([t[0],t[1],t[2]],[n[0],n[1],n[2]],o,a);return n=[...s.foreground,1],{background:e.hex(`rgba(${t.join(",")})`),foreground:e.hex(`rgba(${n.join(",")})`),contrast:s.contrast}},p=/^#((\d|[a-f]){8}|(\d|[a-f]){6}|(\d|[a-f]){3,4})$/i;function y(r){return p.test(r)}function d(r){if(r.startsWith("{"))try{const o=JSON.parse(r);if(v(o))return{type:"ColorPair",value:o}}catch(r){return{type:null,value:null}}return{type:null,value:null}}function M(r){if(r)if("string"==typeof r){if(y(r))return{type:"string",valid:!0,value:r};{const o=d(r);if(o.value)return{type:o.type,valid:!0,value:o.value}}}else{const o=r;if(v(o))return{type:"ColorPair",valid:!0,value:o}}return{type:null,valid:!1,value:""}}function v(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 b(r){const o=M(r);switch(o.type){case null:return!1;case"string":case"ColorPair":return o.valid}}const $=(r,o,t)=>{i(r,"_color",b,new Set(["rgb in hex","ColorPair"]),o,t)},w=r=>{var o;let t;const n=M(r);switch(n.type){case"string":t=h(n.value);break;case"ColorPair":{const r=n.value;let a="";"string"==typeof r.foregroundColor?a=r.foregroundColor:(null===(o=r.foregroundColor)||void 0===o?void 0:o.primary)&&(a=r.foregroundColor.primary),a&&"string"==typeof a||(a="#fff"),t=h({background:r.backgroundColor,foreground:a});break}case null:a.warn(`_color was empty or invalid (${JSON.stringify(r)})`),t=h({background:"#000",foreground:"#000"})}return t.contrast<7&&s(`[KolBadge] The contrast of ${t.contrast} (≥7, AAA) is to low, between the color pair ${t.background} and ${t.foreground}.`),{backgroundColor:t.background,foregroundColor:t.foreground}};export{w as h,u as r,$ as v};