#[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: f32The red channel, sRGB, in [0.0, 1.0].
g: f32The green channel, sRGB, in [0.0, 1.0].
b: f32The blue channel, sRGB, in [0.0, 1.0].
a: f32The alpha channel: 0.0 fully transparent, 1.0 fully opaque.
Implementations§
Source§impl Color
impl Color
Sourcepub const TRANSPARENT: Color
pub const TRANSPARENT: Color
Fully transparent black — the alpha is 0.0, so the RGB channels never show.
Sourcepub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color
Builds a colour with an alpha channel.
Sourcepub const fn rgb8(r: u8, g: u8, b: u8) -> Color
pub const fn rgb8(r: u8, g: u8, b: u8) -> Color
Builds an opaque colour from 8-bit components (0..=255).
Sourcepub const fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color
pub const fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color
Builds a colour from 8-bit components (0..=255).
Sourcepub fn try_hex(s: &str) -> Option<Color>
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.
Sourcepub fn hex(s: &str) -> Color
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.
Sourcepub fn fade(self, opacity: f32) -> Color
pub fn fade(self, opacity: f32) -> Color
Multiplies the opacity (alpha channel) by opacity, clamped to 0.0..=1.0.
Sourcepub fn with_alpha(self, alpha: f32) -> Color
pub fn with_alpha(self, alpha: f32) -> Color
Replaces the alpha channel (clamped to 0.0..=1.0), leaving RGB alone.
Sourcepub const fn from_argb_u32(argb: u32) -> Color
pub const fn from_argb_u32(argb: u32) -> Color
Builds a colour from an 0xAARRGGBB integer (alpha first).
Sourcepub fn compute_luminance(self) -> f32
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].
Sourcepub fn to_linear(self) -> Color
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.
Sourcepub fn to_srgb(self) -> Color
pub fn to_srgb(self) -> Color
The inverse of Color::to_linear: linear → sRGB.
Sourcepub fn lerp(self, other: Color, t: f32) -> Color
pub fn lerp(self, other: Color, t: f32) -> Color
Linear interpolation towards other (t clamped to 0.0..=1.0).
Sourcepub fn surface_tint(self, tint: Color, elevation: f32) -> Color
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§
impl Copy for Color
Source§impl From<Color> for CupertinoDynamicColor
A plain colour as an adaptive one: the same in every condition, so that a theme takes
either where it takes a colour.
impl From<Color> for CupertinoDynamicColor
A plain colour as an adaptive one: the same in every condition, so that a theme takes either where it takes a colour.
Source§impl From<Color> for ImagePlaceholder
impl From<Color> for ImagePlaceholder
Source§impl From<CupertinoDynamicColor> for Color
An adaptive colour used without being resolved: its light, normal-contrast, base value.
impl From<CupertinoDynamicColor> for Color
An adaptive colour used without being resolved: its light, normal-contrast, base value.
Source§fn from(c: CupertinoDynamicColor) -> Color
fn from(c: CupertinoDynamicColor) -> Color
Source§impl From<MaterialColor> for Color
impl From<MaterialColor> for Color
Source§fn from(ramp: MaterialColor) -> Color
fn from(ramp: MaterialColor) -> Color
A ramp used where a single colour is expected is its primary step.
impl Pod for Color
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§
impl<T> AnyBitPattern for Twhere
T: Pod,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
Source§type Bits = T
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
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self.