Skip to content

Channel mixer

Fact Value
Also called RGB mixer, Color matrix
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, channelMixerFilter
Operator code web/src/core/filters/enhancement/color-adjust.ts, channelMixerFilter

Overview

The channel mixer builds each output channel as a weighted sum of the input red, green and blue. It is the most general linear color change: it can swap channels, blend them, or correct a cast. The default changes nothing.

Two panels: a color chart with hue changing from left to right and brightness falling from top to bottom, above a gray ramp, and the chart with red and blue swapped. Two panels: a color chart with hue changing from left to right and brightness falling from top to bottom, above a gray ramp, and the chart with red and blue swapped.
The channel mixer with red and blue swapped.

How it works

  1. Build the 3 by 3 matrix from the nine weights in percent.
  2. Multiply the color of each pixel by it.
  3. Clip the results to 0 to 255.

Mathematical foundation

\[ \begin{gathered} \begin{bmatrix} R' \\ G' \\ B' \end{bmatrix} = \frac{1}{100} \begin{bmatrix} 100 & 0 & 0 \\ 0 & 100 & 0 \\ 0 & 0 & 100 \end{bmatrix} \begin{bmatrix} R \\ G \\ B \end{bmatrix} \end{gathered} \]

The formula shows the default parameter values.

where

  • \(R, G, B\): The input channel values from 0 to 255
  • \(R', G', B'\): The output channel values

Each output channel is a weighted sum of the three input channels, with weights in percent. The default is the identity, 100 on the diagonal and 0 elsewhere, which changes nothing. A row that adds up to 100 keeps that channel's level for grays. The matrix acts on the encoded sRGB values from 0 to 255, and the results are clipped to 0 to 255.

Any linear change of RGB colors is a 3 by 3 matrix. Black-and-white conversion, sepia, saturation and hue rotation are special cases of it. A row that adds up to 100 percent keeps grays at the same level in that channel.

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 3 by 3 matrix from the nine weights.
  2. Multiply each pixel's color by it.

Parameters

Parameter Default Values Meaning
Red from red redFromRed 100 -200 to 200 % How much of the input red goes into the output red, in percent.
Red from green redFromGreen 0 -200 to 200 % How much of the input green goes into the output red, in percent.
Red from blue redFromBlue 0 -200 to 200 % How much of the input blue goes into the output red, in percent.
Green from red greenFromRed 0 -200 to 200 % How much of the input red goes into the output green, in percent.
Green from green greenFromGreen 100 -200 to 200 % How much of the input green goes into the output green, in percent.
Green from blue greenFromBlue 0 -200 to 200 % How much of the input blue goes into the output green, in percent.
Blue from red blueFromRed 0 -200 to 200 % How much of the input red goes into the output blue, in percent.
Blue from green blueFromGreen 0 -200 to 200 % How much of the input green goes into the output blue, in percent.
Blue from blue blueFromBlue 100 -200 to 200 % How much of the input blue goes into the output blue, in percent.

Implementation

channelMixerFilter in web/src/core/filters/enhancement/color-adjust.ts. 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) red and blue swapped (50, 100, 200) (50, 100, 200)
(200, 100, 50) red from green 50, red from red 50 (150, 100, 50) (150, 100, 50)

Each output channel is a weighted sum of the inputs, with weights in percent.

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

  • Nine numbers are hard to tune by hand.
  • Rows far from a sum of 100 change the brightness of grays.

Artifacts

  • Clipping when weights add up to much more than 100.
  • Noise from a weak channel shows when it gets a large weight.

Tests

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

References