Skip to content

Hue rotation

Fact Value
Also called Hue shift, Hue-rotate
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, hueRotateFilter
Operator code web/src/core/filters/enhancement/color-adjust.ts, hueRotateMatrix

Overview

Hue rotation turns all colors around the color wheel by one angle. It uses the matrix of the CSS hue-rotate() function, which turns the color difference around the gray axis. Grays stay gray, and the gray value of each color changes little.

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 turned by 90 degrees, and turned by 180 degrees with colors near their opposites. 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 turned by 90 degrees, and turned by 180 degrees with colors near their opposites.
Hue rotation by 90 and by 180 degrees.

How it works

  1. Build the matrix for the angle.
  2. Multiply the color of each pixel by it.
  3. Clip the results to 0 to 255.

Mathematical foundation

\[ \begin{gathered} M(\theta) = \begin{bmatrix} 0.213 & 0.715 & 0.072 \\ 0.213 & 0.715 & 0.072 \\ 0.213 & 0.715 & 0.072 \end{bmatrix} + \cos\theta \begin{bmatrix} 0.787 & -0.715 & -0.072 \\ -0.213 & 0.285 & -0.072 \\ -0.213 & -0.715 & 0.928 \end{bmatrix} + \sin\theta \begin{bmatrix} -0.213 & -0.715 & 0.928 \\ 0.143 & 0.140 & -0.283 \\ -0.787 & 0.715 & 0.072 \end{bmatrix}, \qquad \theta = 90^{\circ} \end{gathered} \]

The formula shows the default parameter values.

where

  • \(R, G, B\): The encoded channel values from 0 to 255
  • \(\theta\): The rotation angle

This is the matrix of the CSS hue-rotate() function in the W3C Filter Effects spec. The first part keeps the gray value, the other two turn the color difference around the gray axis. Grays stay gray. The rotation happens in a luma and chroma plane, not in HSV, so the gray value of each color changes little, but HSV hue does not move by exactly theta. The HSV adjustment filter turns the HSV hue exactly. The matrix acts on the encoded sRGB values from 0 to 255, and the results are clipped to 0 to 255.

The matrix has three parts. The first keeps the gray Y = 0.213 R + 0.715 G + 0.072 B. The other two turn the remaining color difference by cos and sin of the angle. Every column of the two turning parts adds up to 0 when weighted by the gray weights, so the gray value stays, up to the rounding of the constants.

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 angle.
  2. Multiply each pixel's color by it.

Parameters

Parameter Default Values Meaning
Angle angle 90 -180 to 180 deg The angle theta to turn the hues by.

Implementation

hueRotateFilter in web/src/core/filters/enhancement/color-adjust.ts builds its matrix with hueRotateMatrix. 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, 0, 0) 120 degrees (-93.1, 113.1, -92.3) (0, 113, 0)
(128, 128, 128) 120 degrees (128, 128, 128) (128, 128, 128)
(200, 100, 50) 180 degrees (35.4, 135.4, 185.4) (35, 135, 185)

Pure red turned by 120 degrees moves toward green, but the result is not pure green, and some channels leave the range. Gray stays gray.

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

  • Some colors leave the 0 to 255 range and clip.
  • The hue does not turn by exactly the angle in HSV terms.

Artifacts

  • Clipped, flat patches in strong colors.

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