From 3b0785a63635e3791c3a78b434efb2da8adad863 Mon Sep 17 00:00:00 2001 From: MacDue Date: Mon, 9 May 2022 00:08:06 +0100 Subject: [PATCH] LibGUI: Invert button icons with low contrast ratios On some dark themes, it becomes impossible to dark button icons against their dark button backgrounds. This change tries to mitigate that by inverting the icon color if the contrast ratio (against the button background) is less the 4.5 (the recommended minimum for text). This is only done for icons that are a solid color (e.g. all back), where the desired icon would likely be the same inverted anyway. Fixes a lot of cases of #13978 --- Userland/Libraries/LibGUI/Button.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Userland/Libraries/LibGUI/Button.cpp b/Userland/Libraries/LibGUI/Button.cpp index 03c1587135..a7e68718f8 100644 --- a/Userland/Libraries/LibGUI/Button.cpp +++ b/Userland/Libraries/LibGUI/Button.cpp @@ -78,6 +78,13 @@ void Button::paint_event(PaintEvent& event) } if (m_icon) { + auto solid_color = m_icon->solid_color(60); + // Note: 4.5 is the minimum recommended constrast ratio for text on the web: + // (https://developer.mozilla.org/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable/Color_contrast) + // Reusing that threshold here as it seems to work reasonably well. + bool should_invert_icon = solid_color.has_value() && palette().button().contrast_ratio(*solid_color) < 4.5f; + if (should_invert_icon) + m_icon->invert(); if (is_enabled()) { if (is_hovered()) painter.blit_brightened(icon_location, *m_icon, m_icon->rect()); @@ -86,6 +93,8 @@ void Button::paint_event(PaintEvent& event) } else { painter.blit_disabled(icon_location, *m_icon, m_icon->rect(), palette()); } + if (should_invert_icon) + m_icon->invert(); } auto& font = is_checked() ? this->font().bold_variant() : this->font(); if (m_icon && !text().is_empty()) {