Skip to main content

ColorInfo

Trait ColorInfo 

Source
pub trait ColorInfo: ToRgba {
    // Provided methods
    fn luminance(self) -> f32 { ... }
    fn is_light(self) -> bool { ... }
    fn contrast_ratio(self, other: impl ToRgba) -> f32 { ... }
    fn to_hex(self) -> String { ... }
    fn to_bytes(self) -> (u8, u8, u8, u8) { ... }
}
Expand description

Trait for computing luminance, contrast, and hex/byte serialization.

This trait has a blanket impl for all ToRgba types, so every color type gets these methods automatically:

use optic_color::*;

let c = RGB(0.5, 0.2, 0.8);
let lum = c.luminance();
let hex = c.to_hex();
let (r, g, b, a) = c.to_bytes();

Provided Methods§

Source

fn luminance(self) -> f32

Relative luminance per ITU-R BT.709.

Uses the standard coefficients: 0.2126 R + 0.7152 G + 0.0722 B.

Source

fn is_light(self) -> bool

Returns true if the luminance is greater than 0.5.

Source

fn contrast_ratio(self, other: impl ToRgba) -> f32

Compute the WCAG contrast ratio against another color.

The result is a value in 1..21. WCAG AA requires 4.5:1 for normal text; WCAG AAA requires 7:1.

Source

fn to_hex(self) -> String

Encode as a hex string: #RRGGBBAA.

Source

fn to_bytes(self) -> (u8, u8, u8, u8)

Convert to 8-bit byte channels: (r, g, b, a) in 0..255.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: ToRgba> ColorInfo for T