Skip to main content

Color

Struct Color 

Source
#[repr(C)]
pub struct Color { pub r: f32, pub g: f32, pub b: f32, pub a: f32, }
Expand description

An RGBA colour. Components lie in [0.0, 1.0].

Colours are expressed in sRGB, the way a colour picker states them. The conversion to linear (Color::to_linear) happens at the last moment, at the GPU boundary, because the render surface is sRGB (see the colour-management milestone).

Fields§

§r: f32

The red channel, sRGB, in [0.0, 1.0].

§g: f32

The green channel, sRGB, in [0.0, 1.0].

§b: f32

The blue channel, sRGB, in [0.0, 1.0].

§a: f32

The alpha channel: 0.0 fully transparent, 1.0 fully opaque.

Implementations§

Source§

impl Color

Source

pub const BLACK: Self

Opaque black.

Source

pub const WHITE: Self

Opaque white.

Source

pub const TRANSPARENT: Self

Fully transparent black — the alpha is 0.0, so the RGB channels never show.

Source

pub const fn rgb(r: f32, g: f32, b: f32) -> Self

Builds an opaque colour.

Source

pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self

Builds a colour with an alpha channel.

Source

pub const fn rgb8(r: u8, g: u8, b: u8) -> Self

Builds an opaque colour from 8-bit components (0..=255).

Source

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

Builds a colour from 8-bit components (0..=255).

Source

pub fn try_hex(s: &str) -> Option<Color>

Parses a CSS hex code (sRGB values), with or without a leading #.

Accepted formats: #RGB, #RGBA, #RRGGBB, #RRGGBBAA, in either case. Returns None if the string is invalid. See Color::hex for the convenient variant, which panics on invalid input.

Source

pub fn hex(s: &str) -> Color

Builds a colour from a CSS hex code (see Color::try_hex).

Panics if the string is invalid — convenient for known literals such as Color::hex("#3B82F6"). For dynamic input, prefer try_hex.

Source

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

The [r, g, b, a] array form, ready for the GPU.

Source

pub fn fade(self, opacity: f32) -> Color

Multiplies the opacity (alpha channel) by opacity, clamped to 0.0..=1.0.

Source

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

Replaces the alpha channel (clamped to 0.0..=1.0), leaving RGB alone.

Source

pub const fn from_argb_u32(argb: u32) -> Color

Builds a colour from an 0xAARRGGBB integer (alpha first).

Source

pub fn compute_luminance(self) -> f32

Relative luminance (WCAG), computed on the linearised channels — the basis of any contrast calculation. Ignores alpha. Result in [0, 1].

Source

pub fn to_linear(self) -> Color

The linear version of this colour (Color values are sRGB). Apply this before handing anything to the GPU: an sRGB target re-encodes linear back to sRGB, so sending linear values reproduces the colour you asked for. Alpha is left unchanged.

Source

pub fn to_srgb(self) -> Color

The inverse of Color::to_linear: linear → sRGB.

Source

pub fn lerp(self, other: Color, t: f32) -> Color

Linear interpolation towards other (t clamped to 0.0..=1.0).

Source

pub fn surface_tint(self, tint: Color, elevation: f32) -> Color

The surface tint for a raised surface: self with tint laid over it at the opacity Material 3 gives that elevation.

A raised surface in Material 3 is not lit, it is tinted. A shadow says a thing is above the page; the tint says how far, and it is what keeps a raised card legible against a dark background where a shadow shows nothing at all.

The opacities are the specification’s own table, interpolated between its levels and clamped outside them (see surface_tint_opacity).

The blend is a plain channel mix, not the framework’s usual warning about translucency: the result here is an opaque colour computed once and handed to the renderer to paint, so nothing is composited and there is no linear-space step to get wrong. Laying the tint on as a translucent layer instead would go through compositing and come out darker — the same trap as every other token with an alpha.

A fully transparent tint leaves the surface alone, as the reference’s does.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Self

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

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

impl From<MaterialColor> for Color

Source§

fn from(ramp: MaterialColor) -> Color

A ramp used where a single colour is expected is its primary step.

Source§

impl Lerp for Color

Source§

fn lerp(self, other: Self, t: f32) -> Self

Interpolates from self (at t=0) to other (at t=1).
Source§

impl PartialEq for Color

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl Pod for Color

Source§

impl StructuralPartialEq for Color

Source§

impl Zeroable for Color

Source§

fn zeroed() -> Self

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> AnyBitPattern for T
where T: Pod,

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<T> CheckedBitPattern for T
where T: AnyBitPattern,

Source§

type Bits = T

Self must have the same layout as the specified Bits except for the possible invalid bit patterns being checked during is_valid_bit_pattern.
Source§

fn is_valid_bit_pattern(_bits: &T) -> bool

If this function returns true, then it must be valid to reinterpret bits as &Self.
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> 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> NoUninit for T
where T: Pod,

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.