import{r as e}from"./rolldown-runtime-DIIn1E2Q.js";import{A as t,D as n,G as r,Lt as i,O as a,Ot as o,R as s,S as c,Y as l,b as u,et as d,ft as f,j as p,k as m,nt as h,ut as g}from"./vendor-vue-core-IZuWV8u3.js";function createMonotoneInterpolator(e){if(e.length===0)return()=>0;if(e.length===1)return()=>e[0][1];let t=[...e].sort((e,t)=>e[0]-t[0]),n=t.length,r=t.map(e=>e[0]),i=t.map(e=>e[1]),a=[],o=[];for(let e=0;e<n-1;e++){let t=r[e+1]-r[e];a.push(t===0?0:(i[e+1]-i[e])/t)}o.push(a[0]??0);for(let e=1;e<n-1;e++)a[e-1]*a[e]<=0?o.push(0):o.push((a[e-1]+a[e])/2);o.push(a[n-2]??0);for(let e=0;e<n-1;e++)if(a[e]===0)o[e]=0,o[e+1]=0;else{let t=o[e]/a[e],n=o[e+1]/a[e],r=t*t+n*n;if(r>9){let i=3/Math.sqrt(r);o[e]=i*t*a[e],o[e+1]=i*n*a[e]}}return e=>{if(e<=r[0])return i[0];if(e>=r[n-1])return i[n-1];let t=0,a=n-1;for(;t<a-1;){let n=t+a>>1;r[n]<=e?t=n:a=n}let s=r[a]-r[t];if(s===0)return i[t];let c=(e-r[t])/s,l=c*c,u=l*c,d=2*u-3*l+1,f=u-2*l+c,p=-2*u+3*l,m=u-l;return d*i[t]+f*s*o[t]+p*i[a]+m*s*o[a]}}function histogramToPath(e){if(!e.length)return``;let t=Array.from(e).sort((e,t)=>e-t)[253];if(t===0)return``;let n=1/t,r=[`M0,1`];for(let t=0;t<256;t++){let i=t/255,a=1-Math.min(1,e[t]*n);r.push(`L${i},${a}`)}return r.push(`L1,1 Z`),r.join(` `)}function useCurveEditor({svgRef:e,modelValue:t}){let r=o(-1),i=null,a=n(()=>{let e=t.value;if(e.length<2)return``;let n=createMonotoneInterpolator(e),r=e[0][0],i=e[e.length-1][0]-r,a=[];for(let e=0;e<=128;e++){let t=r+e/128*i,o=1-n(t);a.push(`${e===0?`M`:`L`}${t},${o}`)}return a.join(``)});function svgCoords(t){let n=e.value;if(!n)return[0,0];let r=n.getScreenCTM();if(!r)return[0,0];let i=new DOMPoint(t.clientX,t.clientY).matrixTransform(r.inverse());return[Math.max(0,Math.min(1,i.x)),Math.max(0,Math.min(1,1-i.y))]}function findNearestPoint(e,n){let r=-1,i=.0016;for(let a=0;a<t.value.length;a++){let o=t.value[a][0]-e,s=t.value[a][1]-n,c=o*o+s*s;c<i&&(i=c,r=a)}return r}function handleSvgPointerDown(e){if(e.button!==0)return;let[n,r]=svgCoords(e),i=findNearestPoint(n,r);if(i>=0){startDrag(i,e);return}if(e.ctrlKey)return;let a=[n,r],o=[...t.value,a];o.sort((e,t)=>e[0]-t[0]),t.value=o,startDrag(o.indexOf(a),e)}function startDrag(n,a){if(i?.(),a.button===2||a.button===0&&a.ctrlKey){if(t.value.length>2){let e=[...t.value];e.splice(n,1),t.value=e}return}r.value=n;let o=e.value;if(!o)return;o.setPointerCapture(a.pointerId);let onMove=e=>{if(r.value<0)return;let[n,i]=svgCoords(e),a=[n,i],o=[...t.value];o[r.value]=a,o.sort((e,t)=>e[0]-t[0]),t.value=o,r.value=o.indexOf(a)},endDrag=()=>{r.value<0||(r.value=-1,o.removeEventListener(`pointermove`,onMove),o.removeEventListener(`pointerup`,endDrag),o.removeEventListener(`lostpointercapture`,endDrag),i=null)};i=endDrag,o.addEventListener(`pointermove`,onMove),o.addEventListener(`pointerup`,endDrag),o.addEventListener(`lostpointercapture`,endDrag)}return l(()=>{i?.()}),{curvePath:a,handleSvgPointerDown,startDrag}}var _=[`y1`,`y2`],v=[`x1`,`x2`],y=[`d`,`fill`],b=[`d`,`stroke`],x=[`cx`,`cy`,`fill`,`onPointerdown`],S=s({__name:`CurveEditor`,props:r({curveColor:{default:`white`},histogram:{}},{modelValue:{required:!0},modelModifiers:{}}),emits:[`update:modelValue`],setup(e){let r=g(e,`modelValue`),o=f(`svgRef`),{curvePath:s,handleSvgPointerDown:l,startDrag:m}=useCurveEditor({svgRef:o,modelValue:r}),S=n(()=>e.histogram?histogramToPath(e.histogram):``);return(e,n)=>(d(),p(`svg`,{ref_key:`svgRef`,ref:o,viewBox:`-0.04 -0.04 1.08 1.08`,preserveAspectRatio:`xMidYMid meet`,class:`aspect-square w-full cursor-crosshair rounded-[5px] bg-node-component-surface`,onPointerdown:n[0]||=u((...e)=>i(l)&&i(l)(...e),[`stop`]),onContextmenu:n[1]||=u(()=>{},[`prevent`,`stop`])},[(d(),p(c,null,h([.25,.5,.75],e=>a(`line`,{key:`h`+e,x1:0,y1:e,x2:1,y2:e,stroke:`currentColor`,"stroke-opacity":`0.1`,"stroke-width":`0.003`},null,8,_)),64)),(d(),p(c,null,h([.25,.5,.75],e=>a(`line`,{key:`v`+e,x1:e,y1:0,x2:e,y2:1,stroke:`currentColor`,"stroke-opacity":`0.1`,"stroke-width":`0.003`},null,8,v)),64)),n[2]||=a(`line`,{x1:`0`,y1:`1`,x2:`1`,y2:`0`,stroke:`currentColor`,"stroke-opacity":`0.15`,"stroke-width":`0.003`},null,-1),S.value?(d(),p(`path`,{key:0,"data-testid":`histogram-path`,d:S.value,fill:e.curveColor,"fill-opacity":`0.15`,stroke:`none`},null,8,y)):t(``,!0),a(`path`,{"data-testid":`curve-path`,d:i(s),fill:`none`,stroke:e.curveColor,"stroke-width":`0.008`,"stroke-linecap":`round`},null,8,b),(d(!0),p(c,null,h(r.value,(t,n)=>(d(),p(`circle`,{key:n,cx:t[0],cy:1-t[1],r:`0.02`,fill:e.curveColor,stroke:`white`,"stroke-width":`0.004`,class:`cursor-grab`,onPointerdown:u(e=>i(m)(n,e),[`stop`])},null,40,x))),128))],544))}}),C=s({__name:`WidgetCurve`,props:{modelValue:{default:()=>[[0,0],[1,1]]},modelModifiers:{}},emits:[`update:modelValue`],setup(e){let t=g(e,`modelValue`);return(e,n)=>(d(),m(S,{modelValue:t.value,"onUpdate:modelValue":n[0]||=e=>t.value=e},null,8,[`modelValue`]))}});export{C as default};
//# sourceMappingURL=WidgetCurve-8mZYJflh.js.map