Explorar o código

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
MacDue %!s(int64=3) %!d(string=hai) anos
pai
achega
3b0785a636
Modificáronse 1 ficheiros con 9 adicións e 0 borrados
  1. 9 0
      Userland/Libraries/LibGUI/Button.cpp

+ 9 - 0
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()) {