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.
This commit is contained in:
Humberto Alves 2022-06-27 14:40:27 +01:00 committed by Linus Groh
parent 0c2dc6be66
commit bbfafa19b4
Notes: sideshowbarker 2024-07-17 09:56:24 +09:00

View File

@ -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()) {