import*as Common from"../common/common.js";import*as Root from"../root/root.js";import*as UI from"../ui/ui.js";export class ContrastOverlay{constructor(t,o){this._contrastInfo=t,this._visible=!1,this._contrastRatioSVG=UI.UIUtils.createSVGChild(o,"svg","spectrum-contrast-container fill"),this._contrastRatioLines=new Map,Root.Runtime.experiments.isEnabled("APCA")?this._contrastRatioLines.set("APCA",UI.UIUtils.createSVGChild(this._contrastRatioSVG,"path","spectrum-contrast-line")):(this._contrastRatioLines.set("aa",UI.UIUtils.createSVGChild(this._contrastRatioSVG,"path","spectrum-contrast-line")),this._contrastRatioLines.set("aaa",UI.UIUtils.createSVGChild(this._contrastRatioSVG,"path","spectrum-contrast-line"))),this._width=0,this._height=0,this._contrastRatioLineBuilder=new ContrastRatioLineBuilder(this._contrastInfo),this._contrastRatioLinesThrottler=new Common.Throttler.Throttler(0),this._drawContrastRatioLinesBound=this._drawContrastRatioLines.bind(this),this._contrastInfo.addEventListener("ContrastInfoUpdated",this._update.bind(this))}_update(){this._visible&&!this._contrastInfo.isNull()&&(Root.Runtime.experiments.isEnabled("APCA")&&null===this._contrastInfo.contrastRatioAPCA()||this._contrastInfo.contrastRatio()&&this._contrastRatioLinesThrottler.schedule(this._drawContrastRatioLinesBound))}setDimensions(t,o){this._width=t,this._height=o,this._update()}setVisible(t){this._visible=t,this._contrastRatioSVG.classList.toggle("hidden",!t),this._update()}async _drawContrastRatioLines(){for(const[t,o]of this._contrastRatioLines){const s=this._contrastRatioLineBuilder.drawContrastRatioLine(this._width,this._height,t);s?o.setAttribute("d",s):o.removeAttribute("d")}}}export class ContrastRatioLineBuilder{constructor(t){this._contrastInfo=t}drawContrastRatioLine(t,o,s){const i=Root.Runtime.experiments.isEnabled("APCA"),n=i?this._contrastInfo.contrastRatioAPCAThreshold():this._contrastInfo.contrastRatioThreshold(s);if(!t||!o||null===n)return null;const r=.02,a=this._contrastInfo.color(),e=this._contrastInfo.bgColor();if(!a||!e)return null;const l=a.rgba(),h=a.hsva(),c=e.rgba(),C=Common.ColorUtils.luminance(c);let m=Common.ColorUtils.blendColors(l,c);const d=Common.ColorUtils.luminance(m)>C,u=i?Common.ColorUtils.desiredLuminanceAPCA(C,n,d):Common.Color.Color.desiredLuminance(C,n,d);if(i&&Math.abs(Math.round(Common.ColorUtils.contrastRatioByLuminanceAPCA(u,C)))<n)return null;let _=h[2],R=0;const p=[h[0],0,0,h[3]];let f=[];const b=[];Common.Color.Color.hsva2rgba(p,b),m=Common.ColorUtils.blendColors(b,c);let A,L=t=>Common.ColorUtils.luminance(Common.ColorUtils.blendColors(Common.Color.Color.fromHSVA(t).rgba(),c));for(Root.Runtime.experiments.isEnabled("APCA")&&(L=t=>Common.ColorUtils.luminanceAPCA(Common.ColorUtils.blendColors(Common.Color.Color.fromHSVA(t).rgba(),c))),A=0;A<1.02;A+=r){A=Math.min(1,A),p[1]=A,p[2]=_+R*r;const s=Common.Color.Color.approachColorValue(p,c,2,u,L);if(null===s)break;R=0===A?0:(s-_)/r,_=s,f.push(f.length?"L":"M"),f.push((A*t).toFixed(2)),f.push(((1-s)*o).toFixed(2))}return A<1.02&&(A-=r,p[2]=1,A=Common.Color.Color.approachColorValue(p,c,1,u,L),null!==A&&(f=f.concat(["L",(A*t).toFixed(2),"-0.1"]))),0===f.length?null:f.join(" ")}}