@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
4 lines • 3.35 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
;var common=require("./common-BHv-S8J5.js");const getContrastYIQ=(o,r,t)=>(299*o+587*r+114*t)/1e3>=128?-1:1,calcColorContrast=(o,r,t,n=1)=>{const a=[Math.max(Math.min(Math.round(r[0]+n*Math.max(1,r[0]/100)),255),0),Math.max(Math.min(Math.round(r[1]+n*Math.max(1,r[1]/100)),255),0),Math.max(Math.min(Math.round(r[2]+n*Math.max(1,r[2]/100)),255),0)],e=common.s(common.rgba$1.hex(`rgba(${o.join(",")},1)`),common.rgba$1.hex(`rgba(${a.join(",")},1)`)),l=a[0]+a[1]+a[2];return 0===l||765===l||e>t?{background:o,foreground:a,contrast:e}:calcColorContrast(o,a,t,n)},cache=new Map,getColorContrast=(o,r,t,n=1)=>{if(cache.has(o))return cache.get(o);const a=calcColorContrast(o,r,t,n);return cache.set(o,a),a},createContrastColorPair=(o,r=7)=>{let t=[0,0,0,1],n=[255,255,255,1];"string"==typeof o?(t=common.rgba(o),n=t):"object"==typeof o&&null!==o&&"string"==typeof o.background&&"string"==typeof o.foreground&&(t=common.rgba(o.background),n="string"==typeof o.foreground?common.rgba(o.foreground):t);const a=getContrastYIQ(t[0],t[1],t[2]),e=getColorContrast([t[0],t[1],t[2]],[n[0],n[1],n[2]],r,a);return n=[...e.foreground,1],{background:common.rgba$1.hex(`rgba(${t.join(",")})`),foreground:common.rgba$1.hex(`rgba(${n.join(",")})`),contrast:e.contrast}},HEX_REGEX=/^#((\d|[a-f]){8}|(\d|[a-f]){6}|(\d|[a-f]){3,4})$/i;function isHexString(o){return HEX_REGEX.test(o)}function isColorObjectString(o){if(o.startsWith("{"))try{const r=JSON.parse(o);if(isValidColorPair(r))return{type:"ColorPair",value:r}}catch(o){return{type:null,value:null}}return{type:null,value:null}}function typeOfColor(o){if(o)if("string"==typeof o){if(isHexString(o))return{type:"string",valid:!0,value:o};{const r=isColorObjectString(o);if(r.value)return{type:r.type,valid:!0,value:r.value}}}else{const r=o;if(isValidColorPair(r))return{type:"ColorPair",valid:!0,value:r}}return{type:null,valid:!1,value:""}}function isValidColorPair(o){return!("object"!=typeof o||!o||"string"!=typeof o.backgroundColor||!("string"==typeof o.foregroundColor||o.foregroundColor&&"string"==typeof o.foregroundColor.primary&&"string"==typeof o.foregroundColor.secondary&&"string"==typeof o.foregroundColor.neutral))}function validatorFunction(o){const r=typeOfColor(o);switch(r.type){case null:return!1;case"string":case"ColorPair":return r.valid}}const validateColor=(o,r,t)=>{common.watchValidator(o,"_color",validatorFunction,new Set(["rgb in hex","ColorPair"]),r,t)},handleColorChange=o=>{var r;let t;const n=typeOfColor(o);switch(n.type){case"string":t=createContrastColorPair(n.value);break;case"ColorPair":{const o=n.value;let a="";"string"==typeof o.foregroundColor?a=o.foregroundColor:(null===(r=o.foregroundColor)||void 0===r?void 0:r.primary)&&(a=o.foregroundColor.primary),a&&"string"==typeof a||(a="#fff"),t=createContrastColorPair({background:o.backgroundColor,foreground:a});break}case null:common.Log.warn(`_color was empty or invalid (${JSON.stringify(o)})`),t=createContrastColorPair({background:"#000",foreground:"#000"})}return t.contrast<7&&common.a11yHint(`[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}};exports.handleColorChange=handleColorChange,exports.validateColor=validateColor;