Expand description
This crate helps calculate relative luminance values for
colors. This can help determine if a color looks light or dark, which may be
different from if a color is mathematically light or dark. For example, even bright
blue (#0000FF
) can appear to be dark to many people.
§Examples
use relative_luminance::{Luminance, Rgb};
let black: Rgb<f32> = Rgb { r: 0.0, g: 0.0, b: 0.0 };
let white: Rgb<f32> = Rgb { r: 1.0, g: 1.0, b: 1.0 };
let green: Rgb<f32> = Rgb { r: 0.0, g: 1.0, b: 0.0 };
let blue: Rgb<f32> = Rgb { r: 0.0, g: 0.0, b: 1.0 };
// If luminance is above 0.5, it can be considered bright.
assert_eq!(black.relative_luminance(), 0.0);
assert_eq!(white.relative_luminance(), 1.0);
assert!(green.relative_luminance() > 0.5);
assert!(blue.relative_luminance() < 0.5);
use relative_luminance::{Luminance, Rgb};
/// RGB channels in the range [0, 255].
struct MyRgb {
r: u8,
g: u8,
b: u8,
}
impl Luminance<f32> for MyRgb {
fn luminance_rgb(&self) -> Rgb<f32> {
Rgb {
// Normalizing color channels to the range [0.0, 1.0]
r: f32::from(self.r) / 255.0,
g: f32::from(self.g) / 255.0,
b: f32::from(self.b) / 255.0,
}
}
}
let black = MyRgb { r: 0, g: 0, b: 0 };
let white = MyRgb { r: 255, g: 255, b: 255 };
assert_eq!(black.relative_luminance(), 0.0);
assert_eq!(white.relative_luminance(), 1.0);
Structs§
- Rgb
- Struct for containing RGB channels that can be used for calculating luminance.
Traits§
- Luminance
- Implement this trait on your color type to provide relative luminance calculations.
- Luminance
Value - This trait is used to define numerical types that can be used to calculate relative luminance values.