pub struct ColorSpace {
pub primaries: Primaries,
pub transfer: Transfer,
pub matrix: Matrix,
pub range: Range,
}Expand description
Everything needed to turn a frame’s samples into light, in one place.
Fields§
§primaries: PrimariesThe chromaticities.
transfer: TransferThe transfer curve.
matrix: MatrixThe luma coefficients.
range: RangeFull or limited.
Implementations§
Source§impl ColorSpace
impl ColorSpace
Sourcepub fn guess_for(width: u32, height: u32) -> Self
pub fn guess_for(width: u32, height: u32) -> Self
What a frame of this size is, when the file does not say.
Files with no colour metadata are common, and guessing by resolution is what every player does: SD was made on 601, HD on 709, UHD increasingly on 2020. It is a convention rather than a rule, so anything the container does state overrides this.
Sourcepub fn yuv_to_rgb(self, bit_depth: u32) -> [[f32; 4]; 3]
pub fn yuv_to_rgb(self, bit_depth: u32) -> [[f32; 4]; 3]
The YUV→RGB matrix for these settings, as three rows of [y, u, v, offset].
Feed it samples normalised by the maximum code value — code / 255 at
8 bits, code / 1023 at 10 — and it returns display RGB in
0.0..=1.0. The offset column absorbs both the limited-range pedestal
and chroma’s neutral point, so a shader is one matrix * yuv + offset
with no branches and no per-pixel arithmetic of its own.
bit_depth is needed even though the samples arrive normalised, because
the neutral chroma code is 2^(n-1) against a maximum of 2^n - 1:
128/255 at 8 bits, 512/1023 at 10. Calling it exactly 0.5 is the
approximation most shaders make, and it tints neutral greys by a
fraction of a code value in the wrong direction. It costs nothing to be
right.