From bbfafa19b49703c7fe97693b3c3ed1bf9c16b728 Mon Sep 17 00:00:00 2001 From: Humberto Alves Date: Mon, 27 Jun 2022 14:40:27 +0100 Subject: [PATCH] LibGUI: Invert button icons only when the contrast ratio improves Fix the algorithm that automatically inverts solid color button icons when placed in similarly colored backgrounds. It was meant for fixing black icons in dark themed buttons. However, there may be situations where the resulting inverted version is actually worse than the original. This change prevents those cases. --- Userland/Libraries/LibGUI/Button.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibGUI/Button.cpp b/Userland/Libraries/LibGUI/Button.cpp index 3fcae0122f..d1fc40ab87 100644 --- a/Userland/Libraries/LibGUI/Button.cpp +++ b/Userland/Libraries/LibGUI/Button.cpp @@ -80,10 +80,14 @@ void Button::paint_event(PaintEvent& event) if (m_icon) { auto solid_color = m_icon->solid_color(60); - // Note: 4.5 is the minimum recommended contrast 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; + bool should_invert_icon = false; + if (solid_color.has_value()) { + auto contrast_ratio = palette().button().contrast_ratio(*solid_color); + // Note: 4.5 is the minimum recommended contrast 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. + should_invert_icon = contrast_ratio < 4.5f && contrast_ratio < palette().button().contrast_ratio(solid_color->inverted()); + } if (should_invert_icon) m_icon->invert(); if (is_enabled()) {