import { useEffect, useMemo, useRef, useState } from 'react'; import { ASPECTS, BW_FILTERS, HUE_BANDS, identityCurve, thumbTransform } from '../lib/edits'; import { computeAutoEdits } from '../lib/auto'; import { clamp } from '../lib/curves'; import type { CurveChannel, Edits, EventRecord, HistoryEntry, PhotoRecord, Preset } from '../types'; import CurveEditor from './CurveEditor'; import { getBitmap, thumbSrc } from '../lib/images'; import { grainSampleWindow, rotatedBounds, Renderer } from '../gl/renderer'; import { applyFilmVariant, FILM_VARIANTS, type FilmVariant } from '../lib/presets'; import { usePresetThumbs } from './PresetThumbs'; import Slider from './Slider'; export function Panel({ title, children, defaultOpen = true, right, }: { title: string; children: React.ReactNode; defaultOpen?: boolean; right?: React.ReactNode; }) { const [open, setOpen] = useState(defaultOpen); return (
{right}
{open &&
{children}
}
); } export interface PanelSetProps { edits: Edits; photo: PhotoRecord; set: (patch: Partial, label: string) => void; } const HUE_TRACK = 'linear-gradient(90deg,#ff4d4d,#ffd24d,#6dff6d,#4dfff0,#4d8bff,#c44dff,#ff4dd2,#ff4d4d)'; export function BasicPanel({ edits, photo, set }: PanelSetProps) { const [autoBusy, setAutoBusy] = useState(false); const auto = async () => { setAutoBusy(true); try { const partial = await computeAutoEdits(photo.thumb); set(partial, 'Auto tone'); } finally { setAutoBusy(false); } }; return (
White balance
set({ temp: v }, 'Temperature')} track="linear-gradient(90deg,#3f74d8,#a8b6c6,#f4b23f)" /> set({ tint: v }, 'Tint')} track="linear-gradient(90deg,#54b364,#a8b6c6,#c56ad2)" />
Tone
set({ exposure: v }, 'Exposure')} format={(v) => v.toFixed(2)} /> set({ contrast: v }, 'Contrast')} /> set({ highlights: v }, 'Highlights')} /> set({ shadows: v }, 'Shadows')} /> set({ whites: v }, 'Whites')} /> set({ blacks: v }, 'Blacks')} />
Presence
set({ texture: v }, 'Texture')} /> set({ clarity: v }, 'Clarity')} /> set({ dehaze: v }, 'Dehaze')} /> set({ vibrance: v }, 'Vibrance')} /> set({ saturation: v }, 'Saturation')} /> {edits.bw && ( <>
B&W filter
{BW_FILTERS.map((f) => ( ))}
{edits.bwFilter !== 'none' && ( set({ bwFilterAmount: v }, 'B&W filter strength')} /> )}
A filter darkens the colors it absorbs: red drops blue skies, green lifts foliage. The color mixer below tunes individual hues on top.
)}
); } const CURVE_COLORS: Record = { rgb: '#e9e9ec', red: '#e06a6a', green: '#6ecf7d', blue: '#6f9de8', }; export function CurvePanel({ edits, set }: PanelSetProps) { const [channel, setChannel] = useState('rgb'); const points = edits.curve[channel]; const update = (next: typeof points) => set({ curve: { ...edits.curve, [channel]: next } }, `Curve ${channel.toUpperCase()}`); return ( update(identityCurve())}> Reset } >
{(['rgb', 'red', 'green', 'blue'] as CurveChannel[]).map((c) => ( ))}
); } export function HslPanel({ edits, set }: PanelSetProps) { const [band, setBand] = useState(0); const active = HUE_BANDS[band]; const setBandValue = (kind: 'hue' | 'sat' | 'lum', value: number) => { const arr = [...edits.hsl[kind]]; arr[band] = value; const label = kind === 'hue' ? 'Hue' : kind === 'sat' ? 'Saturation' : 'Luminance'; set({ hsl: { ...edits.hsl, [kind]: arr } }, `${label} · ${active.name}`); }; return ( set( { hsl: { hue: HUE_BANDS.map(() => 0), sat: HUE_BANDS.map(() => 0), lum: HUE_BANDS.map(() => 0) } }, 'Reset color mixer', ) } > Reset } >
{HUE_BANDS.map((b, i) => (
{active.name}
setBandValue('hue', v)} track={HUE_TRACK} /> setBandValue('sat', v)} /> setBandValue('lum', v)} />
Shift, saturate or brighten one color range without touching the rest.
); } export function ColorGradingPanel({ edits, set }: PanelSetProps) { return (
Shadows
set({ grading: { ...edits.grading, shadows: { ...edits.grading.shadows, h: v } } }, 'Grading shadows hue')} track={HUE_TRACK} /> set({ grading: { ...edits.grading, shadows: { ...edits.grading.shadows, s: v } } }, 'Grading shadows')} />
Highlights
set({ grading: { ...edits.grading, highlights: { ...edits.grading.highlights, h: v } } }, 'Grading highlights hue') } track={HUE_TRACK} /> set({ grading: { ...edits.grading, highlights: { ...edits.grading.highlights, s: v } } }, 'Grading highlights')} /> set({ grading: { ...edits.grading, balance: v } }, 'Grading balance')} />
); } /** * 1:1 loupe of the grain. A small window of the real crop is rendered at actual * pixels, which is the only way to judge a resolution independent grain size * without exporting: this is what the file looks like at 100%. */ function GrainPatch({ photo, edits, freezeGrain, }: { photo: PhotoRecord; edits: Edits; freezeGrain?: boolean; }) { const ref = useRef(null); const renderer = useRef(null); const bitmap = useRef(null); const [ready, setReady] = useState(false); useEffect(() => { let alive = true; getBitmap(photo) .then((bmp) => { if (!alive) return; bitmap.current = bmp; setReady(true); }) .catch(() => undefined); return () => { alive = false; }; }, [photo.id]); useEffect( () => () => { renderer.current?.dispose(); renderer.current = null; }, [], ); useEffect(() => { const canvas = ref.current; const bmp = bitmap.current; if (!canvas || !bmp) return; const dpr = Math.min(window.devicePixelRatio || 1, 2); const frame = rotatedBounds(bmp.width, bmp.height, edits.crop.angle, edits.crop.quarter); // The patch takes the shape of the framed area, so it reads as a window onto // the crop rather than a generic swatch. const cropPxW = Math.max(1, frame.w * edits.crop.w); const cropPxH = Math.max(1, frame.h * edits.crop.h); const cssW = 148; const cssH = Math.round(Math.min(148, Math.max(64, cssW * (cropPxH / cropPxW)))); const W = Math.max(1, Math.round(cssW * dpr)); const H = Math.max(1, Math.round(cssH * dpr)); canvas.style.width = `${cssW}px`; canvas.style.height = `${cssH}px`; if (canvas.width !== W) canvas.width = W; if (canvas.height !== H) canvas.height = H; try { if (!renderer.current) renderer.current = new Renderer(canvas); const r = renderer.current; r.setImage(bmp); // One source pixel per device pixel, centred on whatever the crop frames: // drag the crop rectangle and the patch samples that part of the photo. const view = grainSampleWindow(frame, edits.crop, W, H); r.render(edits, { width: W, height: H, view, freezeGrain }); } catch { // Keep the last good frame rather than blanking the patch. } }, [photo.id, ready, edits, freezeGrain]); return (
Grain at 100% · inside the crop
); } export function EffectsPanel({ edits, photo, set, grainFreeze }: PanelSetProps & { grainFreeze?: boolean }) { return (
Vignette
set({ vignette: { ...edits.vignette, amount: v } }, 'Vignette amount')} /> set({ vignette: { ...edits.vignette, midpoint: v } }, 'Vignette midpoint')} /> set({ vignette: { ...edits.vignette, feather: v } }, 'Vignette feather')} />
Grain
set({ grain: { ...edits.grain, amount: v } }, 'Grain amount')} /> set({ grain: { ...edits.grain, size: v } }, 'Grain size')} /> set({ grain: { ...edits.grain, roughness: v } }, 'Grain roughness')} />
); } export function DetailPanel({ edits, set }: PanelSetProps) { return (
Sharpening
set({ sharpen: { ...edits.sharpen, amount: v } }, 'Sharpen amount')} /> set({ sharpen: { ...edits.sharpen, radius: v } }, 'Sharpen radius')} format={(v) => v.toFixed(1)} def={1} /> set({ sharpen: { ...edits.sharpen, detail: v } }, 'Sharpen detail')} />
Noise reduction
set({ noise: { ...edits.noise, luminance: v } }, 'Noise reduction')} /> set({ noise: { ...edits.noise, detail: v } }, 'Noise detail')} />
); } const BRAND_PREFIX = /^(Kodak|Fujifilm|Ilford|Agfa|CineStill|Lomography) /; export function PresetsPanel({ presets, photo, onApply, onSave, }: { presets: Preset[]; photo: PhotoRecord; onApply: (preset: Preset) => void; onSave: (name: string) => void; }) { const [variant, setVariant] = useState('base'); // Variants are derived on the fly, so every stock gets push/pull and split // grade versions without tripling the catalogue. const shown = useMemo(() => presets.map((p) => applyFilmVariant(p, variant)), [presets, variant]); const groups = new Map(); for (const preset of shown) { const arr = groups.get(preset.group) ?? []; arr.push(preset); groups.set(preset.group, arr); } const thumbs = usePresetThumbs(photo, shown); return (
Presets
{FILM_VARIANTS.map((v) => ( ))}
{[...groups.entries()].map(([group, items]) => (
{group}
{items.map((preset) => { const src = thumbs[preset.id]; return ( ); })}
))}
); } export function HistoryPanel({ history, onRestore }: { history: HistoryEntry[]; onRestore: (edits: Edits) => void }) { return (
History {history.length}
{[...history].reverse().map((entry, i) => ( ))}
); } export function CropPanel({ edits, set, onAspect, onRotate, onFlipPreview, mirrored, }: { edits: Edits; set: PanelSetProps['set']; frame: { w: number; h: number }; onAspect: (id: string) => void; onRotate: (dir: -1 | 1) => void; onFlipPreview?: () => void; mirrored?: boolean; }) { const crop = edits.crop; return (
{onFlipPreview && ( )}
{ASPECTS.map((a) => ( ))}
set({ crop: { ...crop, angle: v } }, 'Straighten')} format={(v) => `${v.toFixed(1)}°`} />
); } export function NavigatorPanel({ photo }: { photo: PhotoRecord }) { return (
Navigator Fit
{photo.name}
{photo.name}
{photo.raw ? (photo.decode === 'embedded' ? 'Raw · camera preview' : 'Raw · demosaiced') : 'JPEG/PNG'}{' '} · {photo.width}×{photo.height}
); } export function MetadataPanel({ photo, events }: { photo: PhotoRecord; events: EventRecord[] }) { const event = events.find((e) => e.id === photo.eventId); const rows: [string, string][] = [ ['File', photo.name], ['Dimensions', `${photo.width} × ${photo.height}`], ['Size', `${(photo.size / (1024 * 1024)).toFixed(2)} MB`], ['Camera', photo.meta.camera ?? '—'], ['Lens', photo.meta.lens ?? '—'], ['ISO', photo.meta.iso ? String(photo.meta.iso) : '—'], ['Aperture', photo.meta.aperture ? `f/${photo.meta.aperture}` : '—'], ['Shutter', photo.meta.shutter ?? '—'], ['Focal', photo.meta.focal ? `${photo.meta.focal} mm` : '—'], ['Captured', photo.takenAt ? new Date(photo.takenAt).toLocaleString() : '—'], ['Imported', new Date(photo.addedAt).toLocaleDateString()], ['Event', event?.name ?? 'Unsorted'], ]; return (
{rows.map(([key, value]) => (
{key}
{value}
))}
); }