Automatically switch into a "light mode" on light backgrounds

This commit is contained in:
Emi Simpson 2022-01-08 21:08:17 -05:00
parent ace47d3225
commit 020e6c4f92
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

View file

@ -27,10 +27,19 @@ use Subtype::*;
impl Theme {
pub fn from_palette(palette: Palette) -> Self {
let base_color = img_color_to_iced(palette.dominant_color());
let luma = relative_lum(base_color);
let text_color = if luma > 0.2 {
Color {
a: 0.8,
..Color::BLACK
}
} else {
Color::WHITE
};
Theme {
base_color: img_color_to_iced(palette.dominant_color()),
text_color: Color::WHITE,
subtype: Base,
base_color, text_color,
}
}
@ -119,3 +128,17 @@ fn img_color_to_iced(color: &Rgb<u8>) -> Color {
a: 1.
}
}
pub fn relative_lum(color: Color) -> f32 {
let mut t = [color.r, color.g, color.b]
.into_iter()
.map(|val| {
if val < 0.03928 {
val / 12.92
} else {
((val+0.055)/1.055).powf(2.4)
}
});
0.2126 * t.next().unwrap() + 0.7152 * t.next().unwrap() + 0.0722 * t.next().unwrap()
}