#[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: f32Implementations§
Source§impl Color
impl Color
pub const TRANSPARENT: Color
pub const WHITE: Color
pub const BLACK: Color
pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color
pub fn rgb8(r: u8, g: u8, b: u8) -> Color
pub fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color
Sourcepub fn to_hex(&self) -> u32
pub fn to_hex(&self) -> u32
This colour as 0xRRGGBBAA, rounded to eight bits a channel: what
Color::hex reads.
pub fn with_alpha(self, a: f32) -> Color
pub fn is_visible(&self) -> bool
Sourcepub fn lerp(self, other: Color, t: f32) -> Color
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.
Sourcepub fn mix(self, other: Color, t: f32) -> Color
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.
Sourcepub fn luminance(self) -> f32
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.
Sourcepub fn contrast(self, other: Color) -> f32
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.
Sourcepub fn toward_contrast(
self,
toward: Color,
on: Color,
ratio: f32,
from: f32,
) -> Color
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§
impl Copy 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§
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.