Sepia¶
| Fact | Value |
|---|---|
| Also called | Sepia tone, Sepia toning |
| 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, sepiaFilter |
| Operator code | web/src/core/filters/enhancement/color-adjust.ts, sepiaMatrix |
Overview¶
Sepia gives the image the warm brown tone of old photographs. It mixes the colors with the fixed matrix of the CSS sepia() function. The amount blends between the image and the full sepia result.
How it works¶
- Build the matrix for the amount.
- Multiply the color of each pixel by it.
- Clip the results to 0 to 255.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(R, G, B\): The encoded channel values from 0 to 255
- \(k\): One minus the amount, from 0 to 1
This is the matrix of the CSS sepia() function in the W3C Filter Effects spec. With k = 0 it is the full sepia matrix. Its rows add up to more than 1 for red and green and less than 1 for blue, so white turns cream and gray turns brown. With k = 1 it is the identity. The matrix acts on the encoded sRGB values from 0 to 255, and the results are clipped to 0 to 255.
Each output channel is a weighted sum of the inputs. The full sepia matrix maps gray (v, v, v) to about (1.35 v, 1.20 v, 0.94 v), a warm brown. Interpolating between the identity and that matrix gives a partial tone.
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¶
- Build the matrix for the amount.
- Multiply each pixel's color by it.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Amount amount |
100 | 0 to 100 % | How far to move toward the full sepia matrix. |
Implementation¶
sepiaFilter in web/src/core/filters/enhancement/color-adjust.ts builds its matrix with sepiaMatrix. 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 |
|---|---|---|---|
| (255, 255, 255) | 100 | (344.5, 306.8, 238.9) | (255, 255, 239) |
| (128, 128, 128) | 100 | (172.9, 154, 119.9) | (173, 154, 120) |
| (128, 128, 128) | 50 | (150.5, 141, 124) | (150, 141, 124) |
At full amount, white leaves the range in red and green and clips to cream. Mid-gray turns brown, since the red row of the matrix adds up to 1.351 and the blue row to 0.937.
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
- Bright areas clip, since the red and green rows add up to more than 1.
Artifacts
- Flat, clipped highlights at full amount.
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¶
- W3C. Filter Effects Module Level 1, section 13.1, the filter functions. W3C Working Draft, 18 December 2018. https://www.w3.org/TR/filter-effects-1/