Skip to content

Commit

Permalink
Contrast calculation adjustments
Browse files Browse the repository at this point in the history
I noticed that the new default theme settings were causing the "wrong"
text color to be picked. After reviewing why the contrast values were
the way they were, I reasoned that the less lightness, the less the
ColorSource contrast matters. So I've applied a multiplication of the
average ligntess between the two colors being compared.
  • Loading branch information
ecton committed Nov 14, 2023
1 parent ca81c37 commit 494fa68
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,12 +1381,19 @@ impl ColorExt for Color {
let (other_source, other_lightness) = self.into_source_and_lightness();
let lightness_delta = other_lightness.difference_between(check_lightness);

let average_lightness = ZeroToOne::new((*check_lightness + *other_lightness) / 2.);

let source_change = check_source.contrast_between(other_source);

let other_alpha = ZeroToOne::new(self.alpha_f32());
let alpha_delta = check_alpha.difference_between(other_alpha);

ZeroToOne::new((*lightness_delta + *source_change + *alpha_delta) / 3.)
ZeroToOne::new(
(*lightness_delta
+ *average_lightness * *source_change
+ *average_lightness * *alpha_delta)
/ 3.,
)
}

fn most_contrasting(self, others: &[Self]) -> Self
Expand Down

0 comments on commit 494fa68

Please sign in to comment.