Skip to main content

Color

Struct Color 

Source
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: f32

Implementations§

Source§

impl Color

Source

pub const TRANSPARENT: Color

Source

pub const BLACK: Color

Source

pub const WHITE: Color

Source

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.

Source

pub const fn space(self) -> ColorSpace

Which primaries this color’s components are stated against.

Source

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.

Source

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.

Source

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.

Source

pub fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color

A color from the eight-bit sRGB values a design tool produces.

Source

pub fn to_srgb(self) -> [f32; 4]

Back to sRGB, for reporting or for a caller that needs it.

Source

pub fn with_alpha(self, alpha: f32) -> Color

Scale alpha, leaving the color itself alone.

Source

pub fn is_opaque(self) -> bool

Source

pub fn is_invisible(self) -> bool

Whether this color would draw nothing.

Source

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.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Color

Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnsafeUnpin for Color

§

impl UnwindSafe for Color

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>