UNPKG

@public-ui/components

Version:

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

4 lines 3.81 kB
/*! * KoliBri - The accessible HTML-Standard */ "use strict";var common=require("./common-1pTy2kta.js");function rgba(o){var r;Array.isArray(o)&&o.raw&&(o=String.raw(...arguments));var t=common.parse(o);if(!t.space)return[];const a="h"===t.space[0]?common.hsl.min:common.rgb.min,n="h"===t.space[0]?common.hsl.max:common.rgb.max;return(r=Array(3))[0]=Math.min(Math.max(t.values[0],a[0]),n[0]),r[1]=Math.min(Math.max(t.values[1],a[1]),n[1]),r[2]=Math.min(Math.max(t.values[2],a[2]),n[2]),"h"===t.space[0]&&(r=common.hsl.rgb(r)),r.push(Math.min(Math.max(t.alpha,0),1)),r}const getContrastYIQ=(o,r,t)=>(299*o+587*r+114*t)/1e3>=128?-1:1,calcColorContrast=(o,r,t,a=1)=>{const n=[Math.max(Math.min(Math.round(r[0]+a*Math.max(1,r[0]/100)),255),0),Math.max(Math.min(Math.round(r[1]+a*Math.max(1,r[1]/100)),255),0),Math.max(Math.min(Math.round(r[2]+a*Math.max(1,r[2]/100)),255),0)],e=common.s(common.rgba.hex(`rgba(${o.join(",")},1)`),common.rgba.hex(`rgba(${n.join(",")},1)`)),l=n[0]+n[1]+n[2];return 0===l||765===l||e>t?{background:o,foreground:n,contrast:e}:calcColorContrast(o,n,t,a)},cache=new Map,getColorContrast=(o,r,t,a=1)=>{if(cache.has(o))return cache.get(o);const n=calcColorContrast(o,r,t,a);return cache.set(o,n),n},createContrastColorPair=(o,r=7)=>{let t=[0,0,0,1],a=[255,255,255,1];"string"==typeof o?(t=rgba(o),a=t):"object"==typeof o&&null!==o&&"string"==typeof o.background&&"string"==typeof o.foreground&&(t=rgba(o.background),a="string"==typeof o.foreground?rgba(o.foreground):t);const n=getContrastYIQ(t[0],t[1],t[2]),e=getColorContrast([t[0],t[1],t[2]],[a[0],a[1],a[2]],r,n);return a=[...e.foreground,1],{background:common.rgba.hex(`rgba(${t.join(",")})`),foreground:common.rgba.hex(`rgba(${a.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 a=typeOfColor(o);switch(a.type){case"string":t=createContrastColorPair(a.value);break;case"ColorPair":{const o=a.value;let n="";"string"==typeof o.foregroundColor?n=o.foregroundColor:(null===(r=o.foregroundColor)||void 0===r?void 0:r.primary)&&(n=o.foregroundColor.primary),n&&"string"==typeof n||(n="#fff"),t=createContrastColorPair({background:o.backgroundColor,foreground:n});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.rgba=rgba,exports.validateColor=validateColor;