summaryrefslogtreecommitdiffstats
path: root/core/src/theme
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-07 19:37:35 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-07 19:37:35 +0200
commit72b975ec82660b39f27b6cb015b763caf20e6483 (patch)
treeb1e312f6e29fb052d9ae9e271772b8296aef9a0f /core/src/theme
parentee86aea7f298c0bdc72733b47c40270ff38c2ba6 (diff)
downloadiced-72b975ec82660b39f27b6cb015b763caf20e6483.tar.gz
iced-72b975ec82660b39f27b6cb015b763caf20e6483.tar.bz2
iced-72b975ec82660b39f27b6cb015b763caf20e6483.zip
Pick best contrast between black/white in `theme::palette`
Diffstat (limited to 'core/src/theme')
-rw-r--r--core/src/theme/palette.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/core/src/theme/palette.rs b/core/src/theme/palette.rs
index aca72eb0..e0ff397a 100644
--- a/core/src/theme/palette.rs
+++ b/core/src/theme/palette.rs
@@ -4,7 +4,7 @@ use crate::{color, Color};
use once_cell::sync::Lazy;
use palette::color_difference::Wcag21RelativeContrast;
use palette::rgb::Rgb;
-use palette::{FromColor, Hsl, Lch, Mix};
+use palette::{FromColor, Hsl, Mix};
/// A color palette.
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -613,10 +613,15 @@ fn mix(a: Color, b: Color, factor: f32) -> Color {
fn readable(background: Color, text: Color) -> Color {
if is_readable(background, text) {
text
- } else if to_lch(background).l < 70.0 {
- Color::WHITE
} else {
- Color::BLACK
+ let white_contrast = relative_contrast(background, Color::WHITE);
+ let black_contrast = relative_contrast(background, Color::BLACK);
+
+ if white_contrast >= black_contrast {
+ Color::WHITE
+ } else {
+ Color::BLACK
+ }
}
}
@@ -631,12 +636,15 @@ fn is_readable(a: Color, b: Color) -> bool {
a_srgb.has_enhanced_contrast_text(b_srgb)
}
-fn to_hsl(color: Color) -> Hsl {
- Hsl::from_color(Rgb::from(color))
+fn relative_contrast(a: Color, b: Color) -> f32 {
+ let a_srgb = Rgb::from(a);
+ let b_srgb = Rgb::from(b);
+
+ a_srgb.relative_contrast(b_srgb)
}
-fn to_lch(color: Color) -> Lch {
- Lch::from_color(Rgb::from(color))
+fn to_hsl(color: Color) -> Hsl {
+ Hsl::from_color(Rgb::from(color))
}
fn from_hsl(hsl: Hsl) -> Color {