Skip to content

White balance

Fact Value
Also called Neutral point white balance, Gray point, Click white balance
Status Implemented and tested
Family Enhancement, illumination and color
Engine Browser
Type Linear, color, neutral point channel gains
Output Image
Operator shown as color-transform (exact)
Support radius 0
Code web/src/core/filters/enhancement/white-balance.ts, whiteBalanceFilter
Operator code web/src/core/filters/enhancement/white-balance.ts, neutralGains

Overview

White balance removes a color cast with one color you pick. The picked color should be neutral, such as a gray card, a white wall or a sheet of paper. The filter finds the gains that turn it gray and applies them to the whole image. Set the probe on the neutral object with the Probe tool, then press Use probe pixel.

Two panels: the color chart with a strong orange cast, and the same chart after white balance on a gray of the ramp, with neutral grays and natural colors. Two panels: the color chart with a strong orange cast, and the same chart after white balance on a gray of the ramp, with neutral grays and natural colors.
The chart with a tungsten cast, made by adapting it from D65 to illuminant A, and after white balance on a gray of the ramp.

How it works

  1. Pick the color of something that should be neutral.
  2. Decode it to linear light and compute its luminance.
  3. Compute for each channel the gain that brings it to that luminance.
  4. Multiply every pixel by the gains, in linear light.

Mathematical foundation

\[ g_C = \frac{Y_n}{n_C}, \quad Y_n = 0.2126\, n_R + 0.7152\, n_G + 0.0722\, n_B, \quad C' = \begin{cases} \operatorname{srgb}\left(g_C \operatorname{lin}(C)\right) & \text{linear light} \\ g_C\, C & \text{otherwise} \end{cases} \]

The formula shows the default parameter values.

where

  • \(n_C\): Channel C of the neutral color
  • \(Y_n\): The luminance of the neutral color
  • \(g_C\): The gain of channel C

n is the picked neutral color and Y_n its luminance, with the BT.709 weights. The gain of each channel turns the neutral into the gray (Y_n, Y_n, Y_n), so it keeps its brightness and loses its color. All other colors get the same gains, which is the von Kries idea of scaling each channel on its own. With Linear light on, the default, the gains act on linear light values: decoded from sRGB, scaled, and encoded again. A change in the color of the light scales linear values, so this is the physical model. With it off, the gains scale the encoded values. A channel of the neutral at 0 gets gain 1.

A change in the color of the light multiplies each channel of a surface by about the same factor everywhere in the image. Undoing it needs one gain per channel, a diagonal matrix. That is the von Kries model of adaptation. One neutral surface is enough to find the gains, since its true color is known to be gray.

Kernel or operator

The Inspector shows the three gains for the current neutral color, and whether they act on linear or encoded values.

Algorithm

  1. Decode the neutral color to linear light, if Linear light is on.
  2. Compute its luminance and the three gains.
  3. Scale each channel by its gain, in linear light or on the encoded values.
  4. Encode back to sRGB and clip to 0 to 255.

Parameters

Parameter Default Values Meaning
Neutral color neutral #d2c8b4 color The color of something in the image that should be neutral gray or white. Take it from the image with the probe pixel.
Linear light linearLight on On or off Scale the linear light values instead of the encoded sRGB values.

Implementation

whiteBalanceFilter in web/src/core/filters/enhancement/white-balance.ts computes the gains with neutralGains. The color parameter has its own control: a hex field, the system color picker, and a button that takes the stored color of the image at the probe pixel.

Worked example

Quantity Value
Picked neutral, encoded (239, 167, 93), #efa75d
Gains in linear light (0.542, 1.211, 4.274)
The same pixel after the filter (182, 182, 182)

The neutral is the gray ramp at x = 128 in the warm chart of the figure. Its red is high and its blue low, so the gains lower red and raise blue. After the filter the pixel is gray, up to rounding.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(1)\)

N is the number of pixels. The gains or the matrix are computed once per run.

Artifacts and limitations

Limitations

  • Needs a truly neutral object. Picking a colored object gives a new cast.
  • One set of gains for the whole image, so mixed lighting stays mixed.

Artifacts

  • Clipped highlights in channels whose gain is above 1.

Tests

The reference computes the same steps with NumPy on a small RGB test image with saturated, gray, black and white pixels, and compares all three channels. The cases cover a warm and a cool neutral, a gray neutral that changes nothing, and both settings of Linear light.

References