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