Skip to content

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.

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 sepia amount 50 with faded warm colors, and at amount 100 in browns and creams. 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 sepia amount 50 with faded warm colors, and at amount 100 in browns and creams.
Sepia at amounts 50 and 100.

How it works

  1. Build the matrix for the amount.
  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} = \begin{bmatrix} 0.393 + 0.607 k & 0.769 - 0.769 k & 0.189 - 0.189 k \\ 0.349 - 0.349 k & 0.686 + 0.314 k & 0.168 - 0.168 k \\ 0.272 - 0.272 k & 0.534 - 0.534 k & 0.131 + 0.869 k \end{bmatrix} \begin{bmatrix} R \\ G \\ B \end{bmatrix}, \qquad k = 1 - \frac{ 100 }{100} \end{gathered} \]

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

  1. Build the matrix for the amount.
  2. 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