1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:58:11 +00:00

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
This commit is contained in:
MacDue 2022-05-09 00:08:06 +01:00 committed by Linus Groh
parent 73b05364e8
commit 3b0785a636

View file

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