Files
luminary/src/components/Histogram.tsx
T
bhursey cd52a2f82f Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw).
Local database, uploaded photos, build output and dependencies are gitignored.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-23 15:19:25 +00:00

90 lines
2.7 KiB
TypeScript

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<HTMLCanvasElement>(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 (
<div className="histo-box">
<canvas className="histogram" ref={ref} />
<button
className={`clip-tri shadows ${clipping === 'shadows' ? 'on' : ''}`}
title="Show shadow clipping (Shift+J)"
aria-pressed={clipping === 'shadows'}
onClick={() => onClipping?.(clipping === 'shadows' ? null : 'shadows')}
/>
<button
className={`clip-tri highlights ${clipping === 'highlights' ? 'on' : ''}`}
title="Show highlight clipping (J)"
aria-pressed={clipping === 'highlights'}
onClick={() => onClipping?.(clipping === 'highlights' ? null : 'highlights')}
/>
</div>
);
}