Skip to main content
BVDART Logo

The Mystery of Gamma Correction

⊘ Deep Dive• 5 min read
gammalinearizationcolor-mathrgb
A common mistake when calculating RGB distance is ignoring gamma. Why you must linearize RGB values before doing math, and what goes wrong when you don't.

The Hidden Curve

When you read an RGB value of (128, 128, 128) from an image file, you might assume it represents exactly half the brightness of (255, 255, 255). It doesn't. It represents roughly 21.5% of the physical light intensity — less than a quarter. This discrepancy is gamma encoding, and ignoring it is one of the most common mistakes in color computation.

Stored value is not light intensity

Equal code steps
Equal light steps
Piecewise sRGB decoding curve and selected channel101Encoded channel CLinear light
Curve view
C 0.50000 → 0.21404 linear((C + 0.055) / 1.055)^2.4Breakpoint C = 0.04045

Both ramps run from black to the selected C and end at the same displayed gray. One divides stored values equally; the other divides light equally, then encodes it for display. Decoding is a known transfer function, distinct from the Lab’s creative gamma adjustment.

The two tonal ramps change with the selected channel: equal code steps and equal light steps reach the same gray. The curve then zooms near black. Use any control to take over.

Inspect an encoded value and its exact sRGB decoding. The stored value 128/255 is about 21.6% linear intensity; the Lab’s creative gamma control is a separate operation.

Try it yourself
  1. Move Encoded channel C and compare the measured linear value with both ramps.
  2. Choose Near black and move through the C = 0.04045 breakpoint.

Watch: C = 0.5 decodes to about 0.214. Near black, sRGB uses a linear segment; equal stored-value steps and equal light steps produce different ramps.

Full guide Open in the Lab

Why Gamma Exists

Gamma encoding has historical roots in CRT monitors, whose phosphors had a non-linear response to voltage — roughly a power curve of γ ≈ 2.2. Image files are encoded with the inverse curve (γ ≈ 1/2.2 ≈ 0.4545) so that the CRT's non-linearity cancels out and the image appears correct.

But there's a deeper reason gamma persists in the LCD era: it matches human vision. We're far more sensitive to differences in dark tones than bright tones. Gamma encoding allocates more of the 0–255 range to darks, where we can see the finest distinctions, and compresses the brights, where we're less discerning. It's a form of perceptual compression.

The Math Problem

When you calculate the average of two colors — say, blending (255, 0, 0) (red) and (0, 0, 0) (black) — doing it directly in gamma-encoded sRGB gives you (128, 0, 0). But physically, the correct midpoint requires linearizing first, averaging in linear space, then re-encoding:

Linear red = 1.0, linear black = 0.0. Average = 0.5. Re-encoded to sRGB ≈ (188, 0, 0). The difference is dramatic — the gamma-naive blend is visibly darker than it should be.

Linearization in Practice

The sRGB standard defines a precise transfer function (slightly more complex than a simple power curve, with a linear segment near black). To linearize:

For each channel, normalize to [0, 1], then apply: if V ≤ 0.04045, then V_linear = V / 12.92; otherwise V_linear = ((V + 0.055) / 1.055) ^ 2.4. The inverse (linear to sRGB) reverses this.

Operations intended to model the addition or averaging of light should use linear-light values before encoding the result for display. Other operations have different goals: perceptual color distances use spaces such as CIELAB, while an encoded-space adjustment can be an intentional artistic choice. State which model a calculation uses.

Gamma and Dithering

Floyd–Steinberg diffuses the difference between a sample and its quantized result. Computing that error in linear light aims to preserve a local average of physical light; computing it in encoded sRGB preserves a different numerical average and produces a different tonal result. Compare the two goals explicitly rather than treating gamma adjustment as interchangeable with sRGB decoding.

The Takeaway

Gamma is not an obscure technical detail — it's the single most impactful correction you can make to any color algorithm. The difference between gamma-naive and gamma-correct processing is often more visible than the difference between different algorithms entirely.

The gamma-expansion study shows the exact sRGB decoding function and compares equally spaced stored values with equally spaced light. The Lab’s Gamma adjustment is a separate creative power-curve control. Lab operations do not all share one color model: the indexed quantizer, for example, assigns colors by Euclidean distance in encoded RGB.

Related Concepts

Related Articles

Explore Related Sections

More about the work: