Automatically switch into a "light mode" on light backgrounds
This commit is contained in:
parent
ace47d3225
commit
020e6c4f92
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue