Skip to content

Saturation adjustment

Fact Value
Also called Saturate, Desaturate, Color saturation
Status Implemented and tested
Family Enhancement, illumination and color
Engine Browser
Type Linear, color, color matrix
Output Image
Operator shown as color-transform (exact)
Support radius 0
Code web/src/core/filters/enhancement/color-adjust.ts, saturationFilter
Operator code web/src/core/filters/enhancement/color-adjust.ts, saturateMatrix

Overview

Saturation adjustment makes colors stronger or weaker. Each color moves away from or toward its own gray by a factor, while the gray value stays. It uses the matrix of the CSS saturate() function.

Three panels: a color chart with hue changing from left to right and brightness falling from top to bottom, above a gray ramp, the chart at 50 percent saturation with pale colors, and at 200 percent with intense colors. Three panels: a color chart with hue changing from left to right and brightness falling from top to bottom, above a gray ramp, the chart at 50 percent saturation with pale colors, and at 200 percent with intense colors.
Saturation at 50 and 200 percent.

How it works

  1. Find the gray of the pixel, Y = 0.213 R + 0.715 G + 0.072 B.
  2. Measure how far each channel is from Y.
  3. Multiply that distance by the factor and add Y back.

Mathematical foundation

\[ \begin{gathered} \begin{bmatrix} R' \\ G' \\ B' \end{bmatrix} = \begin{bmatrix} 0.213 + 0.787 s & 0.715 - 0.715 s & 0.072 - 0.072 s \\ 0.213 - 0.213 s & 0.715 + 0.285 s & 0.072 - 0.072 s \\ 0.213 - 0.213 s & 0.715 - 0.715 s & 0.072 + 0.928 s \end{bmatrix} \begin{bmatrix} R \\ G \\ B \end{bmatrix}, \qquad s = \frac{ 150 }{100} \end{gathered} \]

The formula shows the default parameter values.

where

  • \(R, G, B\): The encoded channel values from 0 to 255
  • \(s\): The saturation factor
  • \(Y\): The gray value with the weights of the spec

This is the matrix of the CSS saturate() function in the W3C Filter Effects spec. It equals C' = Y + s (C - Y) for each channel C, with the gray Y = 0.213 R + 0.715 G + 0.072 B. So s = 0 gives gray, s = 1 keeps the image, and s above 1 pushes each color away from its gray. The gray Y itself does not change. The matrix acts on the encoded sRGB values from 0 to 255, and the results are clipped to 0 to 255.

Written per channel, the matrix is C' = Y + s (C - Y). The gray Y of the output equals the gray of the input, since the weights of Y add up to 1. So saturation changes color strength without changing the gray value, until channels clip.

Kernel or operator

The Inspector shows the 3 by 3 matrix for the current settings. Each row gives the weights of one output channel.

Algorithm

  1. Build the matrix for the factor s.
  2. Multiply each pixel's color by it.

Parameters

Parameter Default Values Meaning
Saturation saturation 150 0 to 300 % The saturation factor s, in percent. 100 keeps the image.

Implementation

saturationFilter in web/src/core/filters/enhancement/color-adjust.ts builds its matrix with saturateMatrix. It is a pixel filter: the runner calls it once per pixel with the red, green and blue values, and rounds and clips the results at the end.

Worked example

Input Settings Output Pixel
(200, 100, 50) 0 % (117.7, 117.7, 117.7) (118, 118, 118)
(200, 100, 50) 150 % (241.2, 91.2, 16.1) (241, 91, 16)
(200, 100, 50) 300 % (364.6, 64.6, -85.4) (255, 65, 0)

The gray of the input is Y = 0.213 R + 0.715 G + 0.072 B = 117.70. At 0 percent every channel becomes Y. At 150 percent the gray of the output is still 117.70, and each channel moves 1.5 times as far from it.

Complexity

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

N is the number of pixels. Each pixel takes a fixed number of operations.

Artifacts and limitations

Limitations

  • Already vivid colors clip quickly at high factors. Vibrance protects them.

Artifacts

  • Clipped, flat areas in strong colors at high factors.
  • Skin turns orange when boosted a lot.

Tests

The reference computes the matrix of the W3C spec with NumPy on a small RGB test image with saturated, gray, black and white pixels. Each case compares all three channels.

References