Keep theme text readable on bright accent buttons

This commit is contained in:
Samuel Onoja committed 2026-09-28 09:47:11 +01:00
1 parent f9d5d9a8a8
commit e9dcc968e1
1 file changed
+23 -5
+23 -5
View File
@@ -47,10 +47,8 @@ pub fn foreground(background: Color, preferred: Color) -> Color {
} }
} }
/// Prefer the theme's foreground for filled controls. A small shade adjustment /// Prefer the theme's foreground for filled controls by adjusting the accent's
/// is less disruptive than flipping light text to black when selecting a row. /// brightness until the text has enough contrast.
/// Very bright/dark accents still use a contrasting foreground rather than
/// changing the user's accent into a substantially different color.
pub fn accent_pair(background: Color, preferred: Color) -> (Color, Color) { pub fn accent_pair(background: Color, preferred: Color) -> (Color, Color) {
let on = composite(preferred, background); let on = composite(preferred, background);
let shade = if luminance(on) > luminance(background) { let shade = if luminance(on) > luminance(background) {
@@ -58,7 +56,7 @@ pub fn accent_pair(background: Color, preferred: Color) -> (Color, Color) {
} else { } else {
Color::WHITE Color::WHITE
}; };
for step in 0..=20 { for step in 0..=65 {
let fill = composite( let fill = composite(
Color { Color {
a: step as f32 / 100., a: step as f32 / 100.,
@@ -226,6 +224,26 @@ mod tests {
assert_eq!(actual.icon_color, Some(preferred)); assert_eq!(actual.icon_color, Some(preferred));
} }
} }
#[test]
fn gruvbox_orange_button_keeps_its_light_theme_text_when_enabled() {
let preferred = Color::from_rgb8(235, 219, 178);
let accent = Color::from_rgb8(254, 128, 25);
let mut native = cosmic::cosmic_theme::ThemeBuilder::dark()
.accent(rgba(accent).color)
.build();
apply(&mut native, preferred);
let theme = cosmic::Theme::custom(std::sync::Arc::new(native));
for state in [State::Active, State::Hovered, State::Pressed] {
let actual = style(&theme, state, false);
assert_eq!(actual.text_color, Some(preferred));
let Some(Background::Color(fill)) = actual.background else {
panic!("No button fill")
};
assert!(contrast(fill, preferred) >= 4.5);
}
}
#[test] #[test]
fn native_button_states_keep_text_and_icons_readable() { fn native_button_states_keep_text_and_icons_readable() {
for accent in [ for accent in [