Black-and-white conversion¶
| Fact | Value |
|---|---|
| Also called | Black and white mix, Monochrome mixer, Custom grayscale |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Linear, color, weighted channel sum |
| Output | Image |
| Operator shown as | color-transform (exact) |
| Support radius | 0 |
| Code | web/src/core/filters/enhancement/color-adjust.ts, bwMixFilter |
| Operator code | web/src/core/filters/enhancement/color-adjust.ts, bwMixFilter |
Overview¶
Black-and-white conversion turns the image gray with weights for red, green and blue that you choose. It works like a colored filter in front of black-and-white film: a high red weight makes red things light and blue things dark.
How it works¶
- Multiply red, green and blue by their weights.
- Add the three and divide by 100.
- Write the result to all three channels.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(R, G, B\): The encoded channel values from 0 to 255
- \(w_R, w_G, w_B\): The weights in percent
The gray value is a weighted sum of the three channels, with weights in percent. The defaults 30, 59 and 11 are close to the BT.601 luma weights. Weights that add up to 100 keep white white and gray gray. A larger sum brightens the image, a smaller one darkens it. Negative weights darken the areas where that channel is strong.
The conversion is a projection of the color onto one direction in RGB space. Colors whose weighted sums match turn into the same gray, so any weights lose some differences between colors. The weights choose which differences survive.
Kernel or operator¶
The Inspector shows the three weights as fractions of 1.
Algorithm¶
- Multiply each channel by its weight and add the three.
- Divide by 100 and write the result to all three channels.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Red red |
30 | -200 to 300 % | The weight of the red channel, in percent. |
Green green |
59 | -200 to 300 % | The weight of the green channel, in percent. |
Blue blue |
11 | -200 to 300 % | The weight of the blue channel, in percent. |
Implementation¶
bwMixFilter 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) | 30, 59, 11 | (124.5, 124.5, 124.5) | (125, 125, 125) |
| (200, 100, 50) | 70, 30, 0 | (170, 170, 170) | (170, 170, 170) |
| (60, 90, 200) | 70, 30, 0 | (69, 69, 69) | (69, 69, 69) |
With the red-filter mix, the orange color gets lighter and the blue one much darker, as a blue sky does behind a red filter on black-and-white film.
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
- Colors with the same weighted sum turn into the same gray.
- Sums far from 100 clip highlights or crush shadows.
Artifacts
- Noise in a channel with a large weight becomes visible.
Tests¶
The reference computes the weighted sum with NumPy on a small RGB test image with saturated, gray, black and white pixels. Each case compares all three channels.
References¶
- GIMP documentation, the Channel Mixer filter. https://docs.gimp.org/2.10/en/gimp-filter-channel-mixer.html