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

A straight-alpha sRGB colour with f32 channels in 0.0..=1.0.

Build one from bytes, a hex literal or floats, and derive the rest of a palette with Color::mix and Color::with_alpha:

use kui_core::Color;

let brand = Color::hex(0x3b5bd4ff);
assert_eq!(brand, Color::rgb8(0x3b, 0x5b, 0xd4));
let hover = brand.mix(Color::WHITE, 0.1); // a little lighter
let wash = brand.with_alpha(0.25);
assert!(hover.r > brand.r && wash.a == 0.25);
assert_eq!(brand.to_hex(), 0x3b5bd4ff);

Fields§

§r: f32§g: f32§b: f32§a: f32

Implementations§

Source§

impl Color

Source

pub const TRANSPARENT: Color

Source

pub const WHITE: Color

Source

pub const BLACK: Color

Source

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

Source

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

Source

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

Source

pub fn to_hex(&self) -> u32

This colour as 0xRRGGBBAA, rounded to eight bits a channel: what Color::hex reads.

Source

pub fn hex(v: u32) -> Color

A colour from 0xRRGGBBAA.

Source

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

Source

pub fn is_visible(&self) -> bool

Source

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

Every channel, alpha included, t of the way to other — what a tween does to a colour. Self::mix keeps this colour’s alpha.

Source

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

This colour moved t of the way toward other, per channel, with self’s alpha kept: mix(WHITE, 0.1) is “a little lighter” and mix(BLACK, 0.1) “a little darker”, which is how a palette is built from one colour (widgets::button_palette).

Straight sRGB, not a perceptual space, and the same interpolation a colour transition does. t outside 0..=1 extrapolates rather than clamping, so a caller can overshoot on purpose.

Source

pub fn luminance(self) -> f32

WCAG relative luminance, 0 (black) to 1 (white): the number a contrast decision is made from — “is this background light enough to want dark text” — rather than the average of the channels, which would call a saturated blue and a saturated green equally bright. Alpha is not in it; a translucent colour’s luminance is the luminance of what it would be over nothing.

Source

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

WCAG’s contrast ratio between two opaque colours, 1:1 to 21:1 — the number “4.5:1” (body text) and “3:1” (large text, UI edges, a focus ring) are ratios of. Symmetric. The arithmetic a palette is checked with; which pairs to check is the theme’s business.

Source

pub fn toward_contrast( self, toward: Color, on: Color, ratio: f32, from: f32, ) -> Color

self moved toward toward in steps of 0.05, starting from of the way there, until it clears ratio on on — the one loop behind “an accent that can be seen on this base”. Reaches toward itself at the end, so pick one that clears the ratio on its own (black or white on any base does), and the cap is a floor rather than a give-up. A colour that already reads at from comes back as that mix, so from = 0.0 paints one verbatim.

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 Default for Color

Source§

fn default() -> Color

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

impl From<Color> for Stop

Source§

fn from(color: Color) -> Stop

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

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> NoneValue for T
where T: Default,

Source§

type NoneType = T

Source§

fn null_value() -> T

The none-equivalent value.
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> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more