0c113e7a91
Camera profiles, proofing tables and the half-float sidecar all have to agree with the pixels the editor actually grades, so they land together with the preset filter menu that was unusable while thumbnails streamed in. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
191 lines
6.6 KiB
TypeScript
191 lines
6.6 KiB
TypeScript
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<ParametricKey, number>;
|
|
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<SVGSVGElement>(null);
|
|
const pointsRef = useRef(points);
|
|
const [drag, setDrag] = useState<number | null>(null);
|
|
const [paramDrag, setParamDrag] = useState<ParametricKey | null>(null);
|
|
const [hover, setHover] = useState<number | null>(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<SVGSVGElement>) => {
|
|
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<SVGSVGElement>) => {
|
|
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 (
|
|
<div className="curve-wrap">
|
|
<svg
|
|
ref={svgRef}
|
|
className="curve"
|
|
viewBox="0 0 100 100"
|
|
preserveAspectRatio="none"
|
|
onPointerDown={handleDown}
|
|
onPointerMove={handleMove}
|
|
onPointerUp={handleUp}
|
|
onPointerCancel={handleUp}
|
|
onDoubleClick={(e) => {
|
|
const idxAttr = (e.target as Element).getAttribute('data-idx');
|
|
if (idxAttr !== null) removePoint(Number(idxAttr));
|
|
}}
|
|
>
|
|
<rect x="0" y="0" width="100" height="100" className="curve-bg" />
|
|
{[25, 50, 75].map((v) => (
|
|
<g key={v}>
|
|
<line x1={v} y1="0" x2={v} y2="100" className="curve-grid" />
|
|
<line x1="0" y1={v} x2="100" y2={v} className="curve-grid" />
|
|
</g>
|
|
))}
|
|
<line x1="0" y1="100" x2="100" y2="0" className="curve-diag" />
|
|
{parametric &&
|
|
onParametric &&
|
|
ZONES.map((zone) => {
|
|
const value = parametric[zone.key] ?? 0;
|
|
const y = clamp01(sampler(zone.x) + (value / 100) * ZONE_TRAVEL);
|
|
return (
|
|
<g key={zone.key} style={{ cursor: 'ns-resize' }}>
|
|
<line
|
|
x1={zone.x * 100}
|
|
y1="0"
|
|
x2={zone.x * 100}
|
|
y2="100"
|
|
stroke="rgba(255,255,255,0.12)"
|
|
strokeDasharray="2 3"
|
|
vectorEffect="non-scaling-stroke"
|
|
/>
|
|
<circle
|
|
data-param={zone.key}
|
|
cx={zone.x * 100}
|
|
cy={(1 - y) * 100}
|
|
r={paramDrag === zone.key ? 3.2 : 2.3}
|
|
fill="#141416"
|
|
stroke="#f0c86a"
|
|
strokeWidth="1.4"
|
|
vectorEffect="non-scaling-stroke"
|
|
>
|
|
<title>{`${zone.label}: ${value > 0 ? '+' : ''}${value}`}</title>
|
|
</circle>
|
|
</g>
|
|
);
|
|
})}
|
|
<path d={path} fill="none" stroke={color} strokeWidth="1.6" vectorEffect="non-scaling-stroke" />
|
|
{points.map((p, i) => (
|
|
<circle
|
|
key={`${i}-${p.x.toFixed(3)}`}
|
|
data-idx={i}
|
|
cx={p.x * 100}
|
|
cy={(1 - p.y) * 100}
|
|
r={hover === i || drag === i ? 3 : 2.1}
|
|
className="curve-point"
|
|
fill={color}
|
|
onPointerEnter={() => setHover(i)}
|
|
onPointerLeave={() => setHover((h) => (h === i ? null : h))}
|
|
/>
|
|
))}
|
|
</svg>
|
|
<div className="curve-hint">
|
|
Click to add a point · drag to shape · drag the four gold dots for highlights, lights, darks and blacks
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|