pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
/* private fields */
}Expand description
A linear, straight-alpha color.
Straight rather than premultiplied at this level because it is what a caller writes; premultiplication happens on the way to the target.
The components are linear light in Self::space’s primaries. Everything
below the API works in one space, and Self::to_array is where a color
arrives in it – so a color stated in another one is converted exactly once,
where it is used, rather than at every place that reads it.
Fields§
§r: f32§g: f32§b: f32§a: f32Implementations§
Source§impl Color
impl Color
pub const TRANSPARENT: Color
pub const BLACK: Color
pub const WHITE: Color
Sourcepub fn linear(r: f32, g: f32, b: f32, a: f32) -> Color
pub fn linear(r: f32, g: f32, b: f32, a: f32) -> Color
A color whose components are linear light, encoded on the way in.
A convenience rather than a second storage: there is one representation here and it is encoded, so this applies the transfer function and keeps the result. Zero and one are fixed points, so the primaries and the grays at either end are the same number either way; a mid-tone is not.
Sourcepub const fn space(self) -> ColorSpace
pub const fn space(self) -> ColorSpace
Which primaries this color’s components are stated against.
Sourcepub fn with_space(self, space: ColorSpace) -> Color
pub fn with_space(self, space: ColorSpace) -> Color
The same color, its components restated against other primaries.
Exact in both directions – a change of primaries is a matrix, and it is invertible. What it is not is range-preserving: a color inside Display P3 and outside sRGB comes back with a component below zero or above one, which is the honest answer rather than a failure. Clamping there would be the substitution this renderer refuses, and it would be silent.
Sourcepub const fn srgb(r: f32, g: f32, b: f32, a: f32) -> Color
pub const fn srgb(r: f32, g: f32, b: f32, a: f32) -> Color
A color given in sRGB, kept as it was given.
The components stay in sRGB’s transfer function for the whole of their
life here, which is what upstream does: a DlColor is built from bytes
with no decode, skia_conversions::ToColor copies it into the engine’s
own Color unchanged, and no shader in impeller/compiler/shader_lib
applies a transfer function. Blending, filtering and coverage all act on
encoded numbers.
Alpha is not subject to the transfer function at all: it is a coverage fraction rather than a perceptual quantity.
Sourcepub const fn display_p3(r: f32, g: f32, b: f32, a: f32) -> Color
pub const fn display_p3(r: f32, g: f32, b: f32, a: f32) -> Color
A color given in Display P3, whose components go through the same
transfer function Self::srgb uses.
Display P3 shares sRGB’s transfer function and its white point and
differs in two of its three primaries, so this decodes exactly as an
sRGB color does and then carries the result against the wider primaries.
Nothing is clipped here or later: a saturated P3 red has no
representation inside the sRGB primaries’ triangle, and what
Self::to_array gives for one has a component below zero. That is the
color, stated in coordinates that cannot contain it.
Sourcepub fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color
pub fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color
A color from the eight-bit sRGB values a design tool produces.
Sourcepub fn with_alpha(self, alpha: f32) -> Color
pub fn with_alpha(self, alpha: f32) -> Color
Scale alpha, leaving the color itself alone.
pub fn is_opaque(self) -> bool
Sourcepub fn is_invisible(self) -> bool
pub fn is_invisible(self) -> bool
Whether this color would draw nothing.
Sourcepub fn to_array(self) -> [f32; 4]
pub fn to_array(self) -> [f32; 4]
The four components, linear and straight, in the order everything here takes them.
Public because a caller building a color filter needs a color in the form the filter takes one, and the alternative is a constructor per filter kind on this type. The components in the space everything below the API works in.
This is the one place a color leaves whatever space it was stated in, so a paint, a gradient stop and a vertex tint all reach the pipeline against the same primaries however they were written.