Skip to content

Commit 794930a

Browse files
committed
fix(studio): align professional grading contracts
1 parent c1fcf75 commit 794930a

10 files changed

Lines changed: 229 additions & 117 deletions

‎packages/studio/src/components/editor/colorGradingFrameAnalysis.test.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,20 @@ describe("colorGradingFrameAnalysis", () => {
6565
expect(sampled?.saturation.max).toBeCloseTo(0.2);
6666
});
6767

68-
it("matches the shader's half-softness edge convention", () => {
69-
const key = secondaryKey({
68+
it("matches the runtime softness on saturation and luma feather edges", () => {
69+
const saturationKey = secondaryKey({
7070
hue: { center: 0, range: 180, softness: 0 },
7171
saturation: { min: 0.5, max: 1, softness: 0.4 },
7272
luma: { min: 0, max: 1, softness: 0 },
7373
});
74+
const lumaKey = secondaryKey({
75+
hue: { center: 0, range: 180, softness: 0 },
76+
saturation: { min: 0, max: 1, softness: 0 },
77+
luma: { min: 0.5, max: 1, softness: 0.4 },
78+
});
7479

75-
const belowOuterEdge = colorGradingSecondaryMask(77, 77, 77, key);
76-
const middleOfFeather = colorGradingSecondaryMask(112, 77, 77, key);
77-
expect(belowOuterEdge).toBe(0);
78-
expect(middleOfFeather).toBeGreaterThan(0);
79-
expect(middleOfFeather).toBeLessThan(1);
80+
expect(colorGradingSecondaryMask(255, 204, 204, saturationKey)).toBeCloseTo(0.15625, 5);
81+
expect(colorGradingSecondaryMask(51, 51, 51, lumaKey)).toBeCloseTo(0.15625, 5);
8082
});
8183

8284
it("builds an alpha-preserving black-and-white selection matte", () => {

‎packages/studio/src/components/editor/colorGradingFrameAnalysis.ts‎

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { NormalizedHfColorGradingSecondary } from "@hyperframes/core/color-grading";
1+
import {
2+
calculateHfColorGradingSecondaryMask,
3+
type NormalizedHfColorGradingSecondary,
4+
} from "@hyperframes/core/color-grading";
25
import { clampNumber } from "../../utils/studioHelpers";
36

47
const BYTE_MAX = 255;
@@ -53,53 +56,18 @@ function rgbToHsv(red: number, green: number, blue: number) {
5356
};
5457
}
5558

56-
function softRangeMask(value: number, min: number, max: number, softness: number): number {
57-
if (value < min) {
58-
if (softness <= 0 || value <= min - softness) return 0;
59-
const t = (value - (min - softness)) / softness;
60-
return t * t * (3 - 2 * t);
61-
}
62-
if (value > max) {
63-
if (softness <= 0 || value >= max + softness) return 0;
64-
const t = (value - max) / softness;
65-
return 1 - t * t * (3 - 2 * t);
66-
}
67-
return 1;
68-
}
69-
7059
export function colorGradingSecondaryMask(
7160
red: number,
7261
green: number,
7362
blue: number,
7463
key: NormalizedHfColorGradingSecondary["key"],
7564
): number {
7665
const hsv = rgbToHsv(red / BYTE_MAX, green / BYTE_MAX, blue / BYTE_MAX);
77-
const hueDistance = Math.abs(((((hsv.hue - key.hue.center + 540) % 360) + 360) % 360) - 180);
78-
const hueMask =
79-
key.hue.range >= 179.999
80-
? 1
81-
: hsv.saturation < 0.001
82-
? 0
83-
: softRangeMask(
84-
hueDistance,
85-
0,
86-
key.hue.range,
87-
Math.min(key.hue.softness, 180 - key.hue.range),
88-
);
89-
return (
90-
hueMask *
91-
softRangeMask(
92-
hsv.saturation,
93-
key.saturation.min,
94-
key.saturation.max,
95-
key.saturation.softness * 0.5,
96-
) *
97-
softRangeMask(
98-
rec709Luma(red / BYTE_MAX, green / BYTE_MAX, blue / BYTE_MAX),
99-
key.luma.min,
100-
key.luma.max,
101-
key.luma.softness * 0.5,
102-
)
66+
return calculateHfColorGradingSecondaryMask(
67+
hsv.hue,
68+
hsv.saturation,
69+
rec709Luma(red / BYTE_MAX, green / BYTE_MAX, blue / BYTE_MAX),
70+
key,
10371
);
10472
}
10573

‎packages/studio/src/components/editor/propertyPanelColorCurves.test.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,28 @@ describe("ColorCurves", () => {
131131
expect(onCommit).toHaveBeenCalledOnce();
132132
act(() => root.unmount());
133133
});
134+
135+
it("does not restore a deleted point when an overlapping key gesture settles", () => {
136+
const { host, root, onCommit } = renderCurves();
137+
const graph = activate(host, "master");
138+
act(() => {
139+
graph.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
140+
});
141+
expect(onCommit.mock.calls.at(-1)?.[0]?.curves.master).toHaveLength(3);
142+
onCommit.mockClear();
143+
144+
act(() => {
145+
graph.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }));
146+
});
147+
act(() => {
148+
graph.dispatchEvent(new KeyboardEvent("keydown", { key: "Delete", bubbles: true }));
149+
});
150+
act(() => {
151+
graph.dispatchEvent(new KeyboardEvent("keyup", { key: "ArrowDown", bubbles: true }));
152+
});
153+
154+
expect(onCommit).toHaveBeenCalledOnce();
155+
expect(onCommit.mock.calls[0]?.[0]?.curves.master).toHaveLength(2);
156+
act(() => root.unmount());
157+
});
134158
});

‎packages/studio/src/components/editor/propertyPanelColorCurves.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function ColorCurves({
5656
const deleteSelected = () => {
5757
if (selectedIndex === null) return;
5858
if (tab.kind === "rgb" && (selectedIndex === 0 || selectedIndex === points.length - 1)) return;
59+
transaction.cancel();
5960
const nextPoints =
6061
tab.kind === "hue" && points.length <= 3
6162
? []

‎packages/studio/src/components/editor/propertyPanelColorSecondary.test.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ describe("PropertyPanelColorSecondary", () => {
117117
act(() => root.unmount());
118118
});
119119

120+
it("preserves legal fractional hue centers at the wrap boundary", () => {
121+
const grading = normalizeHfColorGrading({
122+
secondaries: [
123+
{
124+
key: { hue: { center: 359.7, range: 20 } },
125+
correction: {},
126+
},
127+
],
128+
});
129+
if (!grading?.secondaries) throw new Error("Expected normalized secondaries");
130+
const { host, root } = renderSecondary({ secondaries: grading.secondaries });
131+
const hue = host.querySelector<HTMLElement>('[role="slider"][aria-label="Hue"]');
132+
133+
expect(Number(hue?.getAttribute("aria-valuenow"))).toBeCloseTo(359.7);
134+
expect(hue?.getAttribute("aria-valuemax")).toBe("359.99");
135+
act(() => root.unmount());
136+
});
137+
120138
it("shows a useful error when frame capture is unavailable", async () => {
121139
const { host, root, onCommit } = renderSecondary({
122140
captureFrame: vi.fn().mockRejectedValue(new Error("capture unavailable")),

‎packages/studio/src/components/editor/propertyPanelColorSecondary.tsx‎

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useRef, useState } from "react";
2-
import { COLOR_GRADING_MAX_SECONDARIES } from "@hyperframes/parsers/color-grading-contract";
32
import {
3+
getHfColorGradingCapabilities,
44
normalizeHfColorGrading,
55
type NormalizedHfColorGradingSecondary,
66
} from "@hyperframes/core/color-grading";
@@ -16,6 +16,25 @@ import {
1616

1717
type Secondaries = readonly NormalizedHfColorGradingSecondary[];
1818

19+
const SECONDARY_CAPABILITIES = getHfColorGradingCapabilities().secondaries;
20+
const PERCENT_SCALE = 100;
21+
const RANGE_GAP = 0.01;
22+
const HUE_CENTER_MAX = SECONDARY_CAPABILITIES.hue.center.maxExclusive - RANGE_GAP;
23+
24+
function wrapHueCenter(value: number): number {
25+
const { min, maxExclusive } = SECONDARY_CAPABILITIES.hue.center;
26+
const span = maxExclusive - min;
27+
return ((((value - min) % span) + span) % span) + min;
28+
}
29+
30+
const CORRECTION_CONTROLS = [
31+
["hueShift", "Hue shift", 1, "°"],
32+
["saturation", "Saturation", PERCENT_SCALE, "%"],
33+
["luma", "Luma", PERCENT_SCALE, "%"],
34+
["temperature", "Warmth", PERCENT_SCALE, "%"],
35+
["tint", "Tint", PERCENT_SCALE, "%"],
36+
] as const;
37+
1938
function defaultSecondary(): NormalizedHfColorGradingSecondary {
2039
const secondary = normalizeHfColorGrading({
2140
secondaries: [{ key: {}, correction: {} }],
@@ -84,7 +103,7 @@ export function PropertyPanelColorSecondary({
84103
onCommit(secondaries.map((secondary, index) => (index === activeIndex ? next : secondary)));
85104
};
86105
const addSecondary = () => {
87-
if (secondaries.length >= COLOR_GRADING_MAX_SECONDARIES) return;
106+
if (secondaries.length >= SECONDARY_CAPABILITIES.max) return;
88107
onCommit([...secondaries, DEFAULT_SECONDARY]);
89108
setSelectedIndex(secondaries.length);
90109
};
@@ -128,7 +147,7 @@ export function PropertyPanelColorSecondary({
128147
type="button"
129148
aria-label="Add secondary color selection"
130149
title="Add secondary color selection"
131-
disabled={secondaries.length >= COLOR_GRADING_MAX_SECONDARIES}
150+
disabled={secondaries.length >= SECONDARY_CAPABILITIES.max}
132151
onClick={addSecondary}
133152
className="text-panel-text-3 hover:text-panel-text-1 disabled:opacity-35"
134153
>
@@ -261,22 +280,26 @@ export function PropertyPanelColorSecondary({
261280
<FlatSlider
262281
label="Hue"
263282
value={selected.key.hue.center}
264-
min={0}
265-
max={359}
283+
min={SECONDARY_CAPABILITIES.hue.center.min}
284+
max={HUE_CENTER_MAX}
285+
step={0.1}
266286
tier="explicitCustom"
267287
displayValue={`${Math.round(selected.key.hue.center)}°`}
268288
onCommit={(center) =>
269289
replaceSelected({
270290
...selected,
271-
key: { ...selected.key, hue: { ...selected.key.hue, center } },
291+
key: {
292+
...selected.key,
293+
hue: { ...selected.key.hue, center: wrapHueCenter(center) },
294+
},
272295
})
273296
}
274297
/>
275298
<FlatSlider
276299
label="Hue range"
277300
value={selected.key.hue.range}
278-
min={0}
279-
max={180}
301+
min={SECONDARY_CAPABILITIES.hue.range.min}
302+
max={SECONDARY_CAPABILITIES.hue.range.max}
280303
tier="explicitCustom"
281304
displayValue={`${Math.round(selected.key.hue.range)}°`}
282305
onCommit={(range) =>
@@ -287,7 +310,10 @@ export function PropertyPanelColorSecondary({
287310
hue: {
288311
...selected.key.hue,
289312
range,
290-
softness: Math.min(selected.key.hue.softness, 180 - range),
313+
softness: Math.min(
314+
selected.key.hue.softness,
315+
SECONDARY_CAPABILITIES.hue.rangePlusSoftnessMax - range,
316+
),
291317
},
292318
},
293319
})
@@ -296,8 +322,11 @@ export function PropertyPanelColorSecondary({
296322
<FlatSlider
297323
label="Hue softness"
298324
value={selected.key.hue.softness}
299-
min={0}
300-
max={180 - selected.key.hue.range}
325+
min={SECONDARY_CAPABILITIES.hue.softness.min}
326+
max={Math.min(
327+
SECONDARY_CAPABILITIES.hue.softness.max,
328+
SECONDARY_CAPABILITIES.hue.rangePlusSoftnessMax - selected.key.hue.range,
329+
)}
301330
tier="explicitCustom"
302331
displayValue={`${Math.round(selected.key.hue.softness)}°`}
303332
onCommit={(softness) =>
@@ -310,13 +339,14 @@ export function PropertyPanelColorSecondary({
310339
{(["saturation", "luma"] as const).flatMap((key) => {
311340
const label = key === "saturation" ? "Saturation" : "Luma";
312341
const range = selected.key[key];
342+
const capability = SECONDARY_CAPABILITIES[key];
313343
return [
314344
<FlatSlider
315345
key={`${key}-min`}
316346
label={`${label} min`}
317-
value={range.min * 100}
318-
min={0}
319-
max={100}
347+
value={range.min * PERCENT_SCALE}
348+
min={capability.min.min * PERCENT_SCALE}
349+
max={capability.min.max * PERCENT_SCALE}
320350
tier="explicitCustom"
321351
displayValue={percent(range.min)}
322352
onCommit={(value) =>
@@ -326,7 +356,10 @@ export function PropertyPanelColorSecondary({
326356
...selected.key,
327357
[key]: {
328358
...range,
329-
min: Math.max(0, Math.min(value / 100, range.max - 0.01)),
359+
min: Math.max(
360+
capability.min.min,
361+
Math.min(value / PERCENT_SCALE, range.max - RANGE_GAP),
362+
),
330363
},
331364
},
332365
})
@@ -335,9 +368,9 @@ export function PropertyPanelColorSecondary({
335368
<FlatSlider
336369
key={`${key}-max`}
337370
label={`${label} max`}
338-
value={range.max * 100}
339-
min={0}
340-
max={100}
371+
value={range.max * PERCENT_SCALE}
372+
min={capability.max.min * PERCENT_SCALE}
373+
max={capability.max.max * PERCENT_SCALE}
341374
tier="explicitCustom"
342375
displayValue={percent(range.max)}
343376
onCommit={(value) =>
@@ -347,7 +380,10 @@ export function PropertyPanelColorSecondary({
347380
...selected.key,
348381
[key]: {
349382
...range,
350-
max: Math.min(1, Math.max(value / 100, range.min + 0.01)),
383+
max: Math.min(
384+
capability.max.max,
385+
Math.max(value / PERCENT_SCALE, range.min + RANGE_GAP),
386+
),
351387
},
352388
},
353389
})
@@ -356,15 +392,18 @@ export function PropertyPanelColorSecondary({
356392
<FlatSlider
357393
key={`${key}-softness`}
358394
label={`${label} softness`}
359-
value={range.softness * 100}
360-
min={0}
361-
max={50}
395+
value={range.softness * PERCENT_SCALE}
396+
min={capability.softness.min * PERCENT_SCALE}
397+
max={capability.softness.max * PERCENT_SCALE}
362398
tier="explicitCustom"
363399
displayValue={percent(range.softness)}
364400
onCommit={(value) =>
365401
replaceSelected({
366402
...selected,
367-
key: { ...selected.key, [key]: { ...range, softness: value / 100 } },
403+
key: {
404+
...selected.key,
405+
[key]: { ...range, softness: value / PERCENT_SCALE },
406+
},
368407
})
369408
}
370409
/>,
@@ -374,29 +413,19 @@ export function PropertyPanelColorSecondary({
374413
<div className="pt-1 text-[9px] font-semibold uppercase tracking-[0.12em] text-panel-text-5">
375414
Correction
376415
</div>
377-
{(
378-
[
379-
["hueShift", "Hue shift", -180, 180, "degree"],
380-
["saturation", "Saturation", -100, 100, "percent"],
381-
["luma", "Luma", -100, 100, "percent"],
382-
["temperature", "Warmth", -100, 100, "percent"],
383-
["tint", "Tint", -100, 100, "percent"],
384-
] as const
385-
).map(([key, label, min, max, format]) => {
386-
const scale = format === "degree" ? 1 : 100;
416+
{CORRECTION_CONTROLS.map(([key, label, scale, suffix]) => {
417+
const limit = SECONDARY_CAPABILITIES.correction[key];
387418
const value = selected.correction[key] * scale;
388419
return (
389420
<FlatSlider
390421
key={key}
391422
label={label}
392423
value={value}
393-
min={min}
394-
max={max}
424+
min={limit.min * scale}
425+
max={limit.max * scale}
395426
centerTick
396427
tier={Math.abs(value) > 0.0001 ? "explicitCustom" : "default"}
397-
displayValue={
398-
format === "degree" ? `${Math.round(value)}°` : `${Math.round(value)}%`
399-
}
428+
displayValue={`${Math.round(value)}${suffix}`}
400429
onCommit={(next) =>
401430
replaceSelected({
402431
...selected,

0 commit comments

Comments
 (0)