relative-luminance

Crates.iorelative-luminance
lib.rsrelative-luminance
version1.0.0
created_at2025-01-10 00:11:15.769895+00
updated_at2025-01-10 00:11:15.769895+00
descriptionUtlities to manage relative luminance
homepage
repositoryhttps://github.com/spenserblack/relative-luminance-rs
max_upload_size
id1510777
size24,101
Spenser Black (spenserblack)

documentation

README

relative-luminance

CI

This is a small crate that provides utilities to handle relative luminance. In short, among RGB color channels, green represents the majority of light perceived by the typical human eye, then red, then blue. This can explain why white text may be hard to read on a green (#00FF00) background, yet black text can be hard to read on a blue (#0000FF) background.

background foreground readability
#00FF00 #FFFFFF hard to read
#00FF00 #000000 readable
#0000FF #FFFFFF readable
#0000FF #000000 hard to read

Run cargo run --example contrast to see an example of how this looks.

Example usage

use relative_luminance::*;

// Choose a foreground color that contrasts with the background color
let fg_color = if bg_color.relative_luminance() > 0.5 {
    dark_color
} else {
    light_color
};

println!("{}", "Hello, World!".color(fg_color).on_color(bg_color));
Commit count: 10

cargo fmt