import { useEffect, useRef } from 'react'; import type { Histogram as HistogramData } from '../gl/renderer'; export default function Histogram({ data, version, clipping, onClipping, }: { data: HistogramData | null; version: number; clipping?: 'highlights' | 'shadows' | null; onClipping?: (next: 'highlights' | 'shadows' | null) => void; }) { const ref = useRef(null); useEffect(() => { const canvas = ref.current; if (!canvas) return; const dpr = Math.min(window.devicePixelRatio || 1, 2); const rect = canvas.getBoundingClientRect(); const w = Math.max(1, Math.round(rect.width * dpr)); const h = Math.max(1, Math.round(rect.height * dpr)); if (canvas.width !== w) canvas.width = w; if (canvas.height !== h) canvas.height = h; const ctx = canvas.getContext('2d'); if (!ctx) return; ctx.clearRect(0, 0, w, h); ctx.fillStyle = '#121214'; ctx.fillRect(0, 0, w, h); // grid ctx.strokeStyle = 'rgba(255,255,255,0.06)'; ctx.lineWidth = 1; for (let i = 1; i < 4; i++) { const x = (i / 4) * w; ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, h); ctx.stroke(); } if (!data) return; let peak = 0; for (let i = 1; i < 255; i++) { peak = Math.max(peak, data.r[i], data.g[i], data.b[i]); } if (peak <= 0) return; const scale = (v: number) => Math.pow(v / peak, 0.42); const drawChannel = (bins: Uint32Array, color: string) => { ctx.beginPath(); ctx.moveTo(0, h); for (let i = 0; i < 256; i++) { const x = (i / 255) * w; const y = h - scale(bins[i]) * (h - 2); ctx.lineTo(x, y); } ctx.lineTo(w, h); ctx.closePath(); ctx.fillStyle = color; ctx.fill(); }; ctx.globalCompositeOperation = 'lighter'; drawChannel(data.r, 'rgba(220,64,64,0.55)'); drawChannel(data.g, 'rgba(64,200,96,0.55)'); drawChannel(data.b, 'rgba(72,120,240,0.55)'); ctx.globalCompositeOperation = 'source-over'; }, [data, version]); return (
); }