Photoshot development · 02
Calibrating brightness and contrast: from a GitHub reference to ordered response sampling
A numerical failure, its source-level correction, and the boundary between historical chart measurements and a new 27-point replay.
Source scope: initial eead3ca / later v0.16.0 · 19f45ed. This record does not audit every feature of the current production editor.
Abstract
The feature was a non-destructive brightness/contrast adjustment integrated with masks, opacity and project persistence. Konva’s Contrast filter provided an inspected reference, while Photoshot used its own expressions and channel lookup tables. A September 2026 comparison exposed an intermediate approximation whose mean absolute RGB-channel error was 11.505 against Photoshop 27.3.1. Sampling measured brightness and contrast responses in that order reduced the recorded mean error to 0.241; reversing the order gave 1.150. We reproduce 27 archived gray outputs with the unchanged public engine and expose the underlying data. The full historical chart was not remeasured for this article.
1. Feature contract: a persistent adjustment layer
The objective was to change brightness and contrast through an editable adjustment, retain the original pixels, and preserve the chosen values when the project is reopened. The adjustment must participate in the existing mask, opacity and layer-order machinery. A working slider is therefore necessary but insufficient: it may update a number while the resulting pixel response is still wrong.
For an 8-bit channel, the engine can prepare a lookup from each input value 0–255 to an output response. The table is calculated when the adjustment is compiled and reused by pixel processing. This was already present in the initial implementation; it was not introduced by the later calibration fix. The substantive change was the transfer function and its order.1
2. Initial code and the observed numerical failure
The September 18 record shows Konva’s src/filters/Contrast.ts being opened before the initial adjustment engine was written. Photoshot’s first expressions brighten toward white or darken toward black, then apply contrast around normalized midpoint 0.5. The old-revision path remains in the public source so projects using that calculation retain their behavior. It is not identical to Konva’s formula.
On September 25, an intermediate normal-mode approximation was tested against Photoshop 27.3.1 with brightness +30 and contrast +20. Mean absolute RGB-channel difference was 11.505 and maximum difference was 25. For gray input 0, that build returned 25 instead of 0; for input 128 it returned 168 instead of 157. These are actual recorded failures. They belong to the tested intermediate build, not a retrospective measurement of the September 18 legacy expression.2
19f45ed if (t === 'brightness' && a.revision !== 2) {
x = v.brightness < 0 ? x * (1 + v.brightness / 100) : x + ((1 - x) * v.brightness) / 100;
const contrast = v.contrast < 0 ? 1 + v.contrast / 100 : 1 / (1 - v.contrast / 101);
x = (x - 0.5) * contrast + 0.5;
}3. What the GitHub reference contributed
Konva normalizes each channel, moves its midpoint to zero, scales by a contrast factor, shifts back and clamps the result. Reading an actual pixel loop made its input range, channel handling and alpha boundary explicit. The short excerpt below shows the factor and part of the red-channel operation; it is a reference excerpt, not Photoshot production code.3
The record supports source consultation, not installation or verbatim transplantation of the Konva filter. Photoshot used separately written expressions, then revised them using measured response data. The distinction matters: a public implementation can be useful while still targeting a different response curve. The solution to the measured mismatch came from calibration and application-specific integration, not from claiming the reference was Photoshop’s implementation.
const adjust = Math.pow((this.contrast() + 100) / 100, 2);
// …
red -= 0.5;
red *= adjust;
red += 0.5;| Source | Use category | Contribution | Decision |
|---|---|---|---|
| Konva Contrast.ts | Inspected GitHub source | Midpoint-based contrast pixel loop | Consulted; not installed verbatim |
| Initial Photoshot engine | Application-specific expressions | Brightness then contrast; 256-entry LUTs | Old revision retained |
| Photoshop calibration | Measured response data | Single-control gray-ramp outputs | Input data, not Adobe source code |
| Photoshot revision 2 | Application-specific correction | Response interpolation and ordered composition | Integrated with existing layer machinery |
Actual chronology: reference lookup and initial implementation on September 18, comparison and calibration on September 25. The reference was not first discovered after that numerical failure.
4. Correction: response data, interpolation and operation order
Calibration sampled a synthetic gray ramp in Photoshop normal mode: brightness from −150 to +150 and contrast from −50 to +100 in steps of 10, varying one control while holding the other at zero. The resulting response tables are retained in tone-response.ts. Intermediate control values and channel positions are interpolated through sampleResponse; the compiled RGB tables contain floating-point responses.
In revision 2 normal mode, the input passes through brightness first and contrast second. The order is part of the implementation: C(B(x)) is generally different from B(C(x)). The response calls are inside the existing 256-entry-per-channel compilation path; masks and opacity remain compositor responsibilities. Old revisions keep their prior branch, and the separate Legacy option must not be confused with the normal-mode measured response.14
19f45ed x = sampleResponse(toneResponse.brightness, v.brightness, -150, x);
x = sampleResponse(toneResponse.contrast, v.contrast, -50, x);19f45ed const tables = ['r', 'g', 'b'].map((channel) =>
Float64Array.from({ length: 256 }, (_, i) => {
let x = i / 255;5. Methods and results: archive versus new replay
The historical chart was 256 × 32 pixels: eight gray-ramp rows followed by mixed RGB rows. The comparison excluded alpha and evaluated 24,576 channel samples. In the archived order experiment, B → C gave mean error 0.2412109375 and maximum 1; C → B gave 1.1497395833 and maximum 4. The follow-up patched-engine run reported mean 0.241, maximum 1 and 75.88% exact channel matches. Those rounded figures describe the same stated test condition.
For this article we re-imported the unchanged public engine and checked its response tables against the archived calibration JSON. They match exactly. We also compiled +30/+20, +17/+13 and −65/−23, then evaluated the nine archived gray inputs for each setting: all 27 current outputs match the archived Photoshot values. The retained Photoshop values are comparison points, not newly generated reference exports. The original full-chart PNGs are unavailable in the reviewed folder, so their full-chart errors were not recalculated today.25
| Implementation / setting | Mean |Δ| | Max |Δ| | Origin |
|---|---|---|---|
| Intermediate approximation, +30/+20 | 11.505 | 25 | Historical |
| Response B → C, +30/+20 | 0.241211 | 1 | Historical |
| Response C → B, +30/+20 | 1.149740 | 4 | Historical |
| Additional setting +17/+13 | 0.215 | 1 | Historical |
| Additional setting −65/−23 | 0.229 | 1 | Historical |
Columns: implementation / mean absolute channel error / maximum / measurement origin. Newly rerun: calibration tables equal archived data; 27/27 gray outputs equal archived Photoshot outputs. Those 27 samples do not re-estimate full-chart means.
6. Result interpretation and remaining limits
The documented correction fixes a concrete numerical mismatch on the archived test chart and reproduces the retained gray outputs. The mean-error reduction is approximately 97.9% for that comparison, not a visual-quality percentage or a universal compatibility score. No confidence interval is supplied because this is a deterministic chart comparison, not independent repeated sampling. No rendering-speed benefit was measured.
A later portrait illustration uses brightness +18 and contrast +12. Its appearance must not be used as evidence for the +30/+20 chart statistics. Reproducing all pixels against a newly generated Photoshop export, checking further color profiles and running browser persistence/export checks remain distinct tasks. Part 3 extends the same adjustment architecture to levels and histogram controls.
Code, data and reproducibility
Download the retained measurement summary, new replay data and exact source listings. The full-chart source PNGs were not available for a new Photoshop comparison. The supplied replay harness checks the current source against archived values.