https://github.com/spenserblack/relative-luminance-rs
Get the relative luminance of a color
https://github.com/spenserblack/relative-luminance-rs
color contrast luminance luminosity theme
Last synced: 3 months ago
JSON representation
Get the relative luminance of a color
- Host: GitHub
- URL: https://github.com/spenserblack/relative-luminance-rs
- Owner: spenserblack
- License: apache-2.0
- Created: 2025-01-08T20:34:00.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-14T13:43:16.000Z (about 1 year ago)
- Last Synced: 2025-10-08T13:48:25.884Z (8 months ago)
- Topics: color, contrast, luminance, luminosity, theme
- Language: Rust
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# relative-luminance
[](https://crates.io/crates/relative-luminance)
[](https://github.com/spenserblack/relative-luminance-rs/actions/workflows/ci.yml)
This is a small crate that provides utilities to handle
[relative luminance][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
```rust
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));
```
[relative-luminance]: https://en.wikipedia.org/wiki/Relative_luminance