Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,400 changes: 5 additions & 2,395 deletions packages/editor/src/components/AdjustmentLayer/AdjustmentEditor.tsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Adjustment } from '@varve/scene';
import { renderChannelCases } from './legacy/renderChannelCases';
import { renderEffectCases } from './legacy/renderEffectCases';
import { renderLuminanceCases } from './legacy/renderLuminanceCases';
import { renderTonalCases } from './legacy/renderTonalCases';
import { useLegacyEditorContext } from './legacy/useLegacyEditorContext';
import type { AdjustmentEditorProps } from './types';

export function LegacyAdjustmentEditor(props: AdjustmentEditorProps) {
const context = useLegacyEditorContext(props);

const luminance = renderLuminanceCases(context);
if (luminance !== undefined) return luminance;

const tonal = renderTonalCases(context);
if (tonal !== undefined) return tonal;

const channel = renderChannelCases(context);
if (channel !== undefined) return channel;

const effects = renderEffectCases(context);
if (effects !== undefined) return effects;

const { adjustment } = props;
return (
<div className="adj-editor__slider-row">
<span className="adj-editor__label" style={{ color: 'var(--color-text-muted)' }}>
No editor for {(adjustment as Adjustment).kind}
</span>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { Color } from '@varve/engine';
import type { Adjustment } from '@varve/scene';
import { ColorPicker } from '@varve/ui/components/ColorPicker';
import { RangeValueControl } from '../../Inspector/controls/RangeValueControl';
import { BooleanRow } from './controls';
import { colorToManaged, managedToColor } from './managedColor';
import type { AdjustmentEditorProps } from './types';

export function BlackAndWhiteEditor({
adjustment,
onChange,
onEditStart,
onEditEnd,
}: AdjustmentEditorProps) {
const adj = adjustment as import('@varve/scene').BlackAndWhiteAdjustment;
const channels: { key: keyof import('@varve/scene').BlackAndWhiteAdjustment; label: string }[] = [
{ key: 'reds', label: 'Reds' },
{ key: 'yellows', label: 'Yellows' },
{ key: 'greens', label: 'Greens' },
{ key: 'cyans', label: 'Cyans' },
{ key: 'blues', label: 'Blues' },
{ key: 'magentas', label: 'Magentas' },
];

return (
<div className="adj-editor__group">
{channels.map((ch) => (
<div key={ch.key} className="adj-editor__slider-row">
<div className="adj-editor__slider-label">
<span>{ch.label}</span>
<span>{adj[ch.key] as number}</span>
</div>
<RangeValueControl
label={ch.label}
min={-200}
max={300}
value={adj[ch.key] as number}
onChange={(value) => onChange({ [ch.key]: value } as unknown as Partial<Adjustment>)}
/>
</div>
))}
<div className="adj-editor__slider-row">
<div className="adj-editor__slider-label">
<span>Brightness</span>
<span>{adj.brightness}</span>
</div>
<RangeValueControl
label="Brightness"
min={-100}
max={100}
value={adj.brightness}
onChange={(value) => onChange({ brightness: value } as unknown as Partial<Adjustment>)}
/>
</div>
<BooleanRow
label="Preserve Luminosity"
checked={adj.preserveLuminosity}
onChange={(value) =>
onChange({ preserveLuminosity: value } as unknown as Partial<Adjustment>)
}
/>
<BooleanRow
label="Tint"
checked={adj.tintColor !== undefined}
onChange={(value) =>
onChange({
tintColor: value ? ([190, 170, 140, 255] as Color) : undefined,
} as unknown as Partial<Adjustment>)
}
/>
{adj.tintColor && (
<ColorPicker
value={colorToManaged(adj.tintColor)}
onChange={(color) =>
onChange({ tintColor: managedToColor(color) } as unknown as Partial<Adjustment>)
}
onInteractionStart={onEditStart}
onInteractionEnd={onEditEnd}
/>
)}
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { mixerRows } from '@varve/engine';
import type { Adjustment } from '@varve/scene';
import { Button, Select } from '@varve/ui';
import { NumberField } from '../../Inspector/controls/NumberField';
import { BooleanRow } from './controls';
import type { AdjustmentEditorProps } from './types';

export function ChannelMixerEditor({ adjustment, onChange }: AdjustmentEditorProps) {
const adj = adjustment as import('@varve/scene').ChannelMixerAdjustment;
const rows = mixerRows(adj),
active = adj.monochrome ? 'red' : adj.outputChannel,
row = rows[active];
const patchRow = (key: keyof typeof row, value: number) => {
const updated = { ...row, [key]: value };
onChange({ ...updated, rows: { ...rows, [active]: updated } } as Partial<Adjustment>);
};
return (
<div className="curve-editor">
<Select
label="Output channel"
value={active}
disabled={adj.monochrome}
options={[
{ value: 'red', label: 'Red' },
{ value: 'green', label: 'Green' },
{ value: 'blue', label: 'Blue' },
]}
onChange={(v) => {
const channel = v as 'red' | 'green' | 'blue';
onChange({ outputChannel: channel, ...rows[channel], rows } as Partial<Adjustment>);
}}
/>
<BooleanRow
label="Monochrome"
checked={adj.monochrome}
onChange={(monochrome) => onChange({ monochrome, rows } as Partial<Adjustment>)}
/>
<NumberField
label="Red percent"
displayLabel="Red %"
value={row.redPercent}
onChange={(v) => patchRow('redPercent', v)}
step={1}
/>
<NumberField
label="Green percent"
displayLabel="Green %"
value={row.greenPercent}
onChange={(v) => patchRow('greenPercent', v)}
step={1}
/>
<NumberField
label="Blue percent"
displayLabel="Blue %"
value={row.bluePercent}
onChange={(v) => patchRow('bluePercent', v)}
step={1}
/>
<NumberField
label="Constant (code values)"
displayLabel="Offset"
value={row.constant}
onChange={(v) => patchRow('constant', v)}
step={1}
/>
<span className="curve-editor__hint">
Every output reads the original RGB. Coefficients are not normalized. Monochrome uses the
Red row.
</span>
<Button
size="sm"
variant="ghost"
onClick={() =>
onChange({
rows: undefined,
outputChannel: 'red',
redPercent: 100,
greenPercent: 0,
bluePercent: 0,
constant: 0,
monochrome: false,
} as Partial<Adjustment>)
}
>
Reset mixer
</Button>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Switch } from '@varve/ui';

export function BooleanRow({
label,
checked,
onChange,
ariaLabel,
}: {
label: string;
checked: boolean;
onChange: (value: boolean) => void;
ariaLabel?: string;
}) {
return (
<div className="adj-editor__row">
<Switch
className="adj-editor__checkbox-row"
label={label}
checked={checked}
onChange={(event) => onChange(event.target.checked)}
aria-label={ariaLabel ?? label}
/>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { type CurvePoint, compileCurve, curveChannels } from '@varve/engine';
import type { Adjustment } from '@varve/scene';
import { Button, Select } from '@varve/ui';
import { CurveEditor } from '../../Inspector/controls/CurveEditor';
import { SourceChannels } from '../SourceChannels';
import type { AdjustmentEditorProps } from './types';

// CurvesAdjustment.points ({input, output}, 0-255, document/render space) and
// CurveEditor's CurvePoint ({x, y}, 0-1, plot space) are the same tonal curve
// in two different unit systems — a straight linear scale, not a lossy or
// structural conversion. Order is preserved (not re-sorted) so a drag that
// moves one point doesn't reshuffle the others; CurveEditor already sorts by
// x internally for rendering/hit-testing.
export function curvesPointsToCurvePoints(
points: import('@varve/scene').CurvesPoint[],
): CurvePoint[] {
return points.map((p) => ({
...(p.id ? { id: p.id } : {}),
x: p.input / 255,
y: p.output / 255,
}));
}
export function curvePointsToCurvesPoints(
points: CurvePoint[],
): import('@varve/scene').CurvesPoint[] {
return points.map((p) => ({
...(p.id ? { id: p.id } : {}),
input: Math.max(0, Math.min(1, p.x)) * 255,
output: Math.max(0, Math.min(1, p.y)) * 255,
}));
}
export function CurvesEditor({
adjustment,
onChange,
onEditStart,
onEditEnd,
sourceHistogram,
histogramSourceLabel,
histogramLoading,
sourceImageData,
}: AdjustmentEditorProps) {
const adj = adjustment as import('@varve/scene').CurvesAdjustment;
const channels = curveChannels(adj);
const changeChannel = (channel: 'rgb' | 'red' | 'green' | 'blue') =>
onChange({
channel,
points: channels[channel] ?? [],
channelPoints: channels,
} as Partial<Adjustment>);
const changePoints = (points: CurvePoint[]) => {
const authored = curvePointsToCurvesPoints(points);
onChange({
points: authored,
channelPoints: { ...channels, [adj.channel]: authored },
} as Partial<Adjustment>);
};

return (
<div>
{(histogramSourceLabel || histogramLoading) && (
<div className="histogram-widget__source" role="note">
{histogramLoading ? 'Loading histogram…' : `Histogram: ${histogramSourceLabel}`}
</div>
)}
<Select
label="Curve interpolation"
value={String(adj.algorithmVersion ?? 1)}
options={[
{ value: '1', label: 'Legacy Catmull-Rom' },
{ value: '2', label: 'Shape preserving' },
]}
onChange={(v) => onChange({ algorithmVersion: Number(v) as 1 | 2 } as Partial<Adjustment>)}
/>
<Button
size="sm"
variant="ghost"
onClick={() =>
onChange({
points: [],
channelPoints: { rgb: [], red: [], green: [], blue: [] },
} as Partial<Adjustment>)
}
>
Reset all channels
</Button>
<CurveEditor
key={`${adj.id}-${adj.channel}`}
value={curvesPointsToCurvePoints(channels[adj.channel] ?? [])}
onChange={changePoints}
algorithm={adj.algorithmVersion === 2 ? 'pchip' : 'legacy'}
channel={adj.channel as 'rgb' | 'red' | 'green' | 'blue'}
onChannelChange={changeChannel}
histogram={sourceHistogram ?? undefined}
onDragStart={onEditStart}
onDragEnd={onEditEnd}
/>
<p className="tonal-editor__hint">
Sample upstream input to add a point on the current transfer. RGB uses the arithmetic mean
of the three encoded components.
</p>
<SourceChannels
source={sourceImageData}
label={histogramSourceLabel}
onSample={(x, y) => {
if (!sourceImageData || (channels[adj.channel]?.length ?? 0) >= 256) return;
const at = (y * sourceImageData.width + x) * 4;
if (sourceImageData.data[at + 3] === 0) return;
const c = { red: 0, green: 1, blue: 2 };
let input =
adj.channel === 'rgb'
? (sourceImageData.data[at]! +
sourceImageData.data[at + 1]! +
sourceImageData.data[at + 2]!) /
3
: sourceImageData.data[at + c[adj.channel]]!;
if (adj.channel !== 'rgb') {
input =
compileCurve(
curvesPointsToCurvePoints(channels.rgb ?? []),
adj.algorithmVersion === 2 ? 'pchip' : 'legacy',
)(input / 255) * 255;
}
const points = curvesPointsToCurvePoints(channels[adj.channel] ?? []);
const output = compileCurve(
points,
adj.algorithmVersion === 2 ? 'pchip' : 'legacy',
)(input / 255);
changePoints([...points, { id: crypto.randomUUID(), x: input / 255, y: output }]);
}}
/>
</div>
);
}
Loading
Loading