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.
How it works¶
- Build the matrix for the angle.
- 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
- \(\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¶
- Build the matrix for the angle.
- 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¶
- 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/