pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}Expand description
A color represented as RGB components. All values are compile-time constants with zero runtime cost.
Fields§
§r: u8Red component (0–255).
g: u8Green component (0–255).
b: u8Blue component (0–255).
Implementations§
Source§impl Color
impl Color
Sourcepub const fn new(r: u8, g: u8, b: u8) -> Self
pub const fn new(r: u8, g: u8, b: u8) -> Self
Construct a Color from RGB components.
§Examples
use chromata::Color;
let red = Color::new(255, 0, 0);
assert_eq!(red.r, 255);
assert_eq!(red.g, 0);Sourcepub const fn from_hex(hex: u32) -> Self
pub const fn from_hex(hex: u32) -> Self
Construct a Color from a 24-bit hex value (0xRRGGBB).
§Examples
use chromata::Color;
let c = Color::from_hex(0x1d2021);
assert_eq!(c.r, 0x1d);
assert_eq!(c.g, 0x20);
assert_eq!(c.b, 0x21);Sourcepub const fn to_hex(self) -> u32
pub const fn to_hex(self) -> u32
Return the color as a 24-bit hex value.
§Examples
use chromata::Color;
let c = Color::new(0x1d, 0x20, 0x21);
assert_eq!(c.to_hex(), 0x1d2021);Sourcepub fn to_css_hex(self) -> String
pub fn to_css_hex(self) -> String
Return the color as a CSS hex string like “#1d2021”.
§Examples
use chromata::Color;
let c = Color::from_hex(0x1d2021);
assert_eq!(c.to_css_hex(), "#1d2021");Sourcepub const fn to_f32(self) -> (f32, f32, f32)
pub const fn to_f32(self) -> (f32, f32, f32)
Convert to an (f32, f32, f32) tuple in [0.0, 1.0] range.
§Examples
use chromata::Color;
let white = Color::new(255, 255, 255);
let (r, g, b) = white.to_f32();
assert!((r - 1.0).abs() < f32::EPSILON);Sourcepub fn luminance(self) -> f64
pub fn luminance(self) -> f64
Relative luminance per WCAG 2.0.
Returns a value between 0.0 (black) and 1.0 (white).
§Examples
use chromata::Color;
let black = Color::new(0, 0, 0);
let white = Color::new(255, 255, 255);
assert!((black.luminance()).abs() < 0.001);
assert!((white.luminance() - 1.0).abs() < 0.001);Sourcepub fn contrast_ratio(self, other: Color) -> f64
pub fn contrast_ratio(self, other: Color) -> f64
WCAG contrast ratio between two colors.
Returns a value between 1.0 (identical) and 21.0 (black vs white).
§Examples
use chromata::Color;
let black = Color::new(0, 0, 0);
let white = Color::new(255, 255, 255);
let ratio = black.contrast_ratio(white);
assert!(ratio > 20.0); // ~21:1 for black/whiteSourcepub fn lerp(self, other: Color, t: f32) -> Color
pub fn lerp(self, other: Color, t: f32) -> Color
Linear interpolation between two colors.
t is clamped to [0.0, 1.0]. Interpolation is performed in sRGB space.
§Examples
use chromata::Color;
let black = Color::new(0, 0, 0);
let white = Color::new(255, 255, 255);
let mid = black.lerp(white, 0.5);
assert_eq!(mid, Color::new(127, 127, 127));Trait Implementations§
impl Copy for Color
impl Eq 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
Mutably borrows from an owned value. Read more