import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { buildSampler, clamp01 } from '../lib/curves'; import type { CurvePoint } from '../types'; export type ParametricKey = 'highlights' | 'lights' | 'darks' | 'blacks'; interface Props { points: CurvePoint[]; onChange: (points: CurvePoint[]) => void; color: string; /** Region tone shaper values, shown as draggable dots on the curve. */ parametric?: Record; onParametric?: (key: ParametricKey, value: number) => void; } /** Lightroom-scale zones: the four regions share the diagonal evenly. */ const ZONES: { key: ParametricKey; label: string; x: number }[] = [ { key: 'highlights', label: 'Highlights', x: 0.875 }, { key: 'lights', label: 'Lights', x: 0.625 }, { key: 'darks', label: 'Darks', x: 0.375 }, { key: 'blacks', label: 'Blacks', x: 0.125 }, ]; /** Full slider travel moves a zone dot a quarter of the graph. */ const ZONE_TRAVEL = 0.18; export default function CurveEditor({ points, onChange, color, parametric, onParametric }: Props) { const svgRef = useRef(null); const pointsRef = useRef(points); const [drag, setDrag] = useState(null); const [paramDrag, setParamDrag] = useState(null); const [hover, setHover] = useState(null); useEffect(() => { pointsRef.current = points; }, [points]); const sampler = useMemo(() => buildSampler(points), [points]); const path = useMemo(() => { const segs: string[] = []; for (let i = 0; i <= 80; i++) { const x = i / 80; const y = sampler(x); segs.push(`${i === 0 ? 'M' : 'L'}${(x * 100).toFixed(2)},${((1 - y) * 100).toFixed(2)}`); } return segs.join(' '); }, [sampler]); const toNorm = useCallback((clientX: number, clientY: number) => { const svg = svgRef.current; if (!svg) return { x: 0, y: 0 }; const rect = svg.getBoundingClientRect(); return { x: clamp01((clientX - rect.left) / rect.width), y: clamp01(1 - (clientY - rect.top) / rect.height), }; }, []); const handleDown = (e: React.PointerEvent) => { e.preventDefault(); const paramAttr = (e.target as Element).getAttribute('data-param'); if (paramAttr && parametric && onParametric) { setParamDrag(paramAttr as ParametricKey); e.currentTarget.setPointerCapture(e.pointerId); return; } const idxAttr = (e.target as Element).getAttribute('data-idx'); let idx: number; if (idxAttr !== null) { idx = Number(idxAttr); } else { const p = toNorm(e.clientX, e.clientY); const next = [...points, { x: p.x, y: p.y }].sort((a, b) => a.x - b.x); idx = next.findIndex((pt) => pt.x === p.x && pt.y === p.y); onChange(next); } setDrag(idx); e.currentTarget.setPointerCapture(e.pointerId); }; const handleMove = (e: React.PointerEvent) => { if (paramDrag && parametric && onParametric) { const zone = ZONES.find((item) => item.key === paramDrag); if (!zone) return; const p = toNorm(e.clientX, e.clientY); const base = sampler(zone.x); const value = ((p.y - base) / ZONE_TRAVEL) * 100; onParametric(paramDrag, Math.round(Math.max(-100, Math.min(100, value)))); return; } if (drag === null) return; const pts = pointsRef.current; const idx = drag; if (!pts[idx]) return; const p = toNorm(e.clientX, e.clientY); const lo = idx > 0 ? pts[idx - 1].x + 0.01 : 0; const hi = idx < pts.length - 1 ? pts[idx + 1].x - 0.01 : 1; const next = pts.map((pt, i) => (i === idx ? { x: Math.min(hi, Math.max(lo, p.x)), y: p.y } : pt)); onChange(next); }; const handleUp = () => { setDrag(null); setParamDrag(null); }; const removePoint = (idx: number) => { const pts = pointsRef.current; if (pts.length <= 2) return; onChange(pts.filter((_, i) => i !== idx)); setDrag(null); }; return (
{ const idxAttr = (e.target as Element).getAttribute('data-idx'); if (idxAttr !== null) removePoint(Number(idxAttr)); }} > {[25, 50, 75].map((v) => ( ))} {parametric && onParametric && ZONES.map((zone) => { const value = parametric[zone.key] ?? 0; const y = clamp01(sampler(zone.x) + (value / 100) * ZONE_TRAVEL); return ( {`${zone.label}: ${value > 0 ? '+' : ''}${value}`} ); })} {points.map((p, i) => ( setHover(i)} onPointerLeave={() => setHover((h) => (h === i ? null : h))} /> ))}
Click to add a point · drag to shape · drag the four gold dots for highlights, lights, darks and blacks
); }