var S=Object.defineProperty;var I=(n,s,t)=>s in n?S(n,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[s]=t;var l=(n,s,t)=>I(n,typeof s!="symbol"?s+"":s,t);class w{constructor(s,t,o){l(this,"r",0);l(this,"g",0);l(this,"b",0);this.set(s,t,o)}toString(){return`rgb(${Math.round(this.r)}, ${Math.round(this.g)}, ${Math.round(this.b)})`}set(s,t,o){this.r=this.clamp(s),this.g=this.clamp(t),this.b=this.clamp(o)}hueRotate(s=0){s=s/180*Math.PI;const t=Math.sin(s),o=Math.cos(s);this.multiply([.213+o*.787-t*.213,.715-o*.715-t*.715,.072-o*.072+t*.928,.213-o*.213+t*.143,.715+o*.285+t*.14,.072-o*.072-t*.283,.213-o*.213-t*.787,.715-o*.715+t*.715,.072+o*.928+t*.072])}grayscale(s=1){this.multiply([.2126+.7874*(1-s),.7152-.7152*(1-s),.0722-.0722*(1-s),.2126-.2126*(1-s),.7152+.2848*(1-s),.0722-.0722*(1-s),.2126-.2126*(1-s),.7152-.7152*(1-s),.0722+.9278*(1-s)])}sepia(s=1){this.multiply([.393+.607*(1-s),.769-.769*(1-s),.189-.189*(1-s),.349-.349*(1-s),.686+.314*(1-s),.168-.168*(1-s),.272-.272*(1-s),.534-.534*(1-s),.131+.869*(1-s)])}saturate(s=1){this.multiply([.213+.787*s,.715-.715*s,.072-.072*s,.213-.213*s,.715+.285*s,.072-.072*s,.213-.213*s,.715-.715*s,.072+.928*s])}multiply(s){const t=this.clamp(this.r*s[0]+this.g*s[1]+this.b*s[2]),o=this.clamp(this.r*s[3]+this.g*s[4]+this.b*s[5]),h=this.clamp(this.r*s[6]+this.g*s[7]+this.b*s[8]);this.r=t,this.g=o,this.b=h}brightness(s=1){this.linear(s)}contrast(s=1){this.linear(s,-(.5*s)+.5)}linear(s=1,t=0){this.r=this.clamp(this.r*s+t*255),this.g=this.clamp(this.g*s+t*255),this.b=this.clamp(this.b*s+t*255)}invert(s=1){this.r=this.clamp((s+this.r/255*(1-2*s))*255),this.g=this.clamp((s+this.g/255*(1-2*s))*255),this.b=this.clamp((s+this.b/255*(1-2*s))*255)}hsl(){const s=this.r/255,t=this.g/255,o=this.b/255,h=Math.max(s,t,o),i=Math.min(s,t,o);let c=0,e=0;const g=(h+i)/2;if(h!==i){const a=h-i;switch(e=g>.5?a/(2-h-i):a/(h+i),h){case s:c=(t-o)/a+(t<o?6:0);break;case t:c=(o-s)/a+2;break;case o:c=(s-t)/a+4;break}c/=6}return{h:c*100,s:e*100,l:g*100}}clamp(s){return Math.max(0,Math.min(255,s))}}class L{constructor(s){l(this,"target");l(this,"targetHSL");l(this,"reusedColor");this.target=s,this.targetHSL=s.hsl(),this.reusedColor=new w(0,0,0)}solve(){const s=this.solveNarrow(this.solveWide());return{values:s.values,loss:s.loss,filter:this.css(s.values)}}solveWide(){const o=[60,180,18e3,600,1.2,1.2];let h={values:[],loss:1/0};for(let i=0;h.loss>25&&i<3;i++){const c=[50,20,3750,50,100,100],e=this.spsa(5,o,15,c,1e3);e.loss<h.loss&&(h=e)}return h}solveNarrow(s){const t=s.loss,o=2,h=t+1,i=[.25*h,.25*h,h,.25*h,.2*h,.2*h];return this.spsa(t,i,o,s.values,500)}spsa(s,t,o,h,i){const e=.16666666666666666;let g=[],a=1/0;const b=new Array(6),m=new Array(6),M=new Array(6);for(let p=0;p<i;p++){const f=o/Math.pow(p+1,e);for(let r=0;r<6;r++)b[r]=Math.random()>.5?1:-1,m[r]=h[r]+f*b[r],M[r]=h[r]-f*b[r];const y=this.loss(m)-this.loss(M);for(let r=0;r<6;r++){const $=y/(2*f)*b[r],A=t[r]/Math.pow(s+p+1,1);h[r]=this.fix(h[r]-A*$,r)}const d=this.loss(h);d<a&&(g=h.slice(0),a=d)}return{values:g,loss:a}}fix(s,t){let o=100;return t===2?o=7500:(t===4||t===5)&&(o=200),t===3?s=(s%o+o)%o:s=Math.max(0,Math.min(o,s)),s}loss(s){const t=this.reusedColor;t.set(0,0,0),t.invert(s[0]/100),t.sepia(s[1]/100),t.saturate(s[2]/100),t.hueRotate(s[3]*3.6),t.brightness(s[4]/100),t.contrast(s[5]/100);const o=t.hsl();return Math.abs(t.r-this.target.r)+Math.abs(t.g-this.target.g)+Math.abs(t.b-this.target.b)+Math.abs(o.h-this.targetHSL.h)+Math.abs(o.s-this.targetHSL.s)+Math.abs(o.l-this.targetHSL.l)}css(s){const t=(o,h=1)=>Math.round(s[o]*h);return`filter: invert(${t(0)}%) sepia(${t(1)}%) saturate(${t(2)}%) hue-rotate(${t(3,3.6)}deg) brightness(${t(4)}%) contrast(${t(5)}%);`}}function k(n){const s=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;n=n.replace(s,(o,h,i,c)=>h+h+i+i+c+c);const t=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(n);return t?[parseInt(t[1],16),parseInt(t[2],16),parseInt(t[3],16)]:null}const H=n=>{const s=k(n);if(!s)throw new Error("Invalid hex color");const t=new w(s[0],s[1],s[2]);return new L(t).solve().filter};export{H as c};
//# sourceMappingURL=color-filter.utils-DukMf1cu.js.map
