Skip to main content

Color

Struct Color 

Source
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}
Expand description

A non-premultiplied RGBA color with components in 0.0..=1.0, in the sRGB color space (the space design tokens are written in).

Fields§

§r: f32

Red component, 0.0..=1.0.

§g: f32

Green component, 0.0..=1.0.

§b: f32

Blue component, 0.0..=1.0.

§a: f32

Alpha (opacity), 0.0 transparent to 1.0 opaque.

Implementations§

Source§

impl Color

Source

pub const TRANSPARENT: Self

Fully transparent black.

Source

pub const BLACK: Self

Opaque black.

Source

pub const WHITE: Self

Opaque white.

Source

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

Builds a color from sRGB components and alpha, each in 0.0..=1.0.

Source

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

Builds an opaque color from sRGB components in 0.0..=1.0.

Source

pub const fn hex(rgb: u32) -> Self

Builds an opaque color from 0xRRGGBB.

Source

pub const fn hex_alpha(rgba: u32) -> Self

Builds a color from 0xRRGGBBAA.

Source

pub const fn faded(self, factor: f32) -> Self

Returns the same color, factor times as opaque as it already is. Use this on a theme’s tokens: in a glass theme they are translucent to begin with, and Self::with_alpha would make them more opaque, not less.

use ravenrs_core::Color;

let glass = Color::rgba(1.0, 1.0, 1.0, 0.2);
assert_eq!(glass.faded(0.5).a, 0.1);
assert_eq!(glass.with_alpha(0.5).a, 0.5);
Source

pub const fn with_alpha(self, a: f32) -> Self

Returns the same color with alpha replaced by a (0.0..=1.0).

Source

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

Component-wise interpolation in sRGB space, t clamped to 0..=1.

Source

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

Converts to linear-light RGB (alpha unchanged), as needed by *Srgb render targets.

Source

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

Returns the sRGB components as [r, g, b, a], without linearization.

Source

pub fn luminance(self) -> f32

WCAG relative luminance.

Source

pub fn contrast_ratio(self, other: Self) -> f32

WCAG contrast ratio between two opaque colors (1.0 to 21.0).

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

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

impl Default for Color

Source§

fn default() -> Color

Returns the “default value” for a type. Read more
Source§

impl From<Color> for Brush

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
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<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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.