ColorMatrix
struct · ferroui-base · v0.5.0
struct ColorMatrix
A colour transform of the form out = in * M: in is the row
[R, G, B, A, 1] of a colour with straight (not premultiplied) alpha,
each channel from 0 to 1, and M has five rows of four columns.
Row i holds what input channel i (red, green, blue, alpha) adds to
each output channel (red, green, blue, alpha); the fifth row is the
offset of each output channel. This is the layout of the Matrix5x4 of
WinUI (M11 .. M54).
Members
rowsIDENTITYnewfrom_matrix5x4saturationluminance_to_alphatintinvertgrayscalesepialerpscale_and_offsetis_identitythenapplyapply_premultipliedapply_to_rgba8apply_to_premultiplied_rgba8to_row_major_4x5
rows
rows: [[f32; 4]; 5]
The rows: red, green, blue and alpha in, then the offsets.
IDENTITY
const IDENTITY: ColorMatrix
The matrix that changes nothing.
new
const fn new(rows: [[f32; 4]; 5]) -> ColorMatrix
A matrix from its rows.
from_matrix5x4
fn from_matrix5x4(m: [f32; 20]) -> ColorMatrix
A matrix from the twenty members of WinUI's Matrix5x4, in the order
M11, M12, M13, M14, M21, ..., M54.
saturation
fn saturation(saturation: f32) -> ColorMatrix
The saturation matrix of Win2D's SaturationEffect (the
saturate colour matrix of SVG): 0 is grey, 1 changes nothing,
values above 1 saturate more.
luminance_to_alpha
fn luminance_to_alpha() -> ColorMatrix
Win2D's LuminanceToAlphaEffect: the luminance of the colour becomes
its alpha, the colour black.
tint
fn tint(tint: [f32; 4]) -> ColorMatrix
Win2D's TintEffect: each channel multiplied by the channel of
tint (red, green, blue, alpha from 0 to 1).
invert
fn invert() -> ColorMatrix
Win2D's InvertEffect: each colour channel c becomes 1 - c; the
alpha is kept.
grayscale
fn grayscale() -> ColorMatrix
Win2D's GrayscaleEffect: each colour channel becomes
0.299 R + 0.587 G + 0.114 B.
sepia
fn sepia(intensity: f32) -> ColorMatrix
Win2D's SepiaEffect: the identity at intensity 0, the sepia
matrix (red 0.393 R + 0.769 G + 0.189 B, green `0.349 R + 0.686 G
- 0.168 B
, blue0.272 R + 0.534 G + 0.131 B`) at 1, and the interpolation of the two in between; the intensity is clamped to 0..1, the alpha kept.
lerp
fn lerp(self: &Self, to: &ColorMatrix, t: f32) -> ColorMatrix
The matrix self + (to - self) * t, member by member.
scale_and_offset
fn scale_and_offset(scale: [f32; 4], offset: [f32; 4]) -> ColorMatrix
The matrix that scales each channel and adds an offset:
out = in * scale + offset, channel by channel.
is_identity
fn is_identity(self: &Self) -> bool
Whether the matrix changes nothing.
then
fn then(self: &Self, next: &ColorMatrix) -> ColorMatrix
The matrix that applies self, then next.
apply
fn apply(self: &Self, color: [f32; 4]) -> [f32; 4]
Applies the matrix to a colour with straight alpha, each channel from 0 to 1. The result is clamped to 0..1.
apply_premultiplied
fn apply_premultiplied(self: &Self, color: [f32; 4], mode: ColorMatrixAlphaMode) -> [f32; 4]
Applies the matrix to a premultiplied colour, each channel from 0 to
1, in mode; the result is premultiplied, clamped to 0..1, its colour
channels no larger than its alpha.
apply_to_rgba8
fn apply_to_rgba8(self: &Self, pixels: &mut [u8], mode: ColorMatrixAlphaMode)
Applies the matrix in mode to premultiplied RGBA pixels of eight
bits a channel, in place.
apply_to_premultiplied_rgba8
fn apply_to_premultiplied_rgba8(self: &Self, pixels: &mut [u8])
Applies the matrix to premultiplied RGBA pixels of eight bits a channel, in place: each pixel is unpremultiplied, transformed and premultiplied again (the straight alpha mode).
to_row_major_4x5
fn to_row_major_4x5(self: &Self) -> [f32; 20]
The matrix in the row-major 4 x 5 form of Skia and SVG: a row per output channel, its last column the offset.
Trait implementations
SendSyncFreezeUnsafeUnpinUnpinUnwindSafeRefUnwindSafePropertyValueAnyValueClassDefaultsBorrow<T>BorrowMut<T>CloneToUninitInto<U>From<T>TryInto<U>TryFrom<U>AnyToOwnedErasedDestructorIntoEitherCloneCopyDebugStructuralPartialEqPartialEqDefault
Availability
Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.