Skip to content

Truly independent web browser

UI/Qt: Derive the chrome's colors from the color theme

Andreas Kling26e1b8c63cb3
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index faefe7282e9..84a56ac0e38 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -1859,6 +1859,7 @@ void BrowserWindow::changeEvent(QEvent* event)
void BrowserWindow::appearance_changed()
{
SystemAppearance::the().apply_interface_theme(WebView::Application::settings().appearance().interface_theme);
+ SystemAppearance::the().apply_color_theme(WebView::Application::settings().appearance().color_theme);
update_menu_bar_visibility();
m_tabs_container->update_bookmarks_bar_visibility();
diff --git a/UI/Qt/LadybirdButton.cpp b/UI/Qt/LadybirdButton.cpp
index f69cfde4b12..bbb5281bd31 100644
--- a/UI/Qt/LadybirdButton.cpp
+++ b/UI/Qt/LadybirdButton.cpp
@@ -163,15 +163,15 @@ void LadybirdButton::paintEvent(QPaintEvent*)
painter.setOpacity(ChromeStyle::window_activity(*this) == ChromeStyle::WindowActivity::Inactive ? 0.55 : 1.0);
// The tile is a hairline outline, with a neutral fill only under the pointer or while pressed. The mark is in the
- // profile's color, which is the only brand color in the chrome, and doesn't change with the tile's state. A private
- // window shows its glyph in place of the logomark.
+ // color theme's logomark color, and doesn't change with the tile's state. A private window shows its glyph in place
+ // of the logomark.
auto is_private = m_is_private == WebView::IsPrivate::Yes;
auto dark = is_private || Theme::is_dark(*this);
auto fill_color = dark ? QColor(255, 255, 255) : QColor(0, 0, 0);
fill_color.setAlphaF((dark ? 0.16 : 0.12) * m_fill);
auto hairline_color = fill_color;
hairline_color.setAlphaF(Theme::has_increased_contrast() ? 0.45 : (dark ? 0.16 : 0.14));
- auto mark_color = is_private ? QColor(0xd8c8ff) : (dark ? QColor(0xa684ff) : QColor(0x4b22ad));
+ auto mark_color = is_private ? QColor(0xd8c8ff) : Theme::logomark();
QRectF tile_rect { this->tile_rect() };
painter.setPen(Qt::NoPen);
diff --git a/UI/Qt/LadybirdButton.h b/UI/Qt/LadybirdButton.h
index f54e6411c8b..110acfc2152 100644
--- a/UI/Qt/LadybirdButton.h
+++ b/UI/Qt/LadybirdButton.h
@@ -15,7 +15,7 @@ class QVariantAnimation;
namespace Ladybird {
-// The Ladybird button opens the browser's menu. It shows the logomark in the profile's color and a menu chevron, on a soft
+// The Ladybird button opens the browser's menu. It shows the logomark in the theme's color and a menu chevron, on a soft
// fill that deepens under the pointer and while the menu is open.
class LadybirdButton final : public QToolButton {
public:
diff --git a/UI/Qt/SystemAppearance.cpp b/UI/Qt/SystemAppearance.cpp
index c0812f44f85..9b478c64fdb 100644
--- a/UI/Qt/SystemAppearance.cpp
+++ b/UI/Qt/SystemAppearance.cpp
@@ -6,6 +6,7 @@
#include <AK/Platform.h>
#include <UI/Qt/SystemAppearance.h>
+#include <UI/Qt/Theme.h>
#include <QAccessibilityHints>
#include <QApplication>
@@ -20,6 +21,18 @@ SystemAppearance& SystemAppearance::the()
return *appearance;
}
+// Widgets restyle themselves on palette changes, so a change to the chrome's colors that leaves the application palette
+// as it is tells them about one.
+static void notify_widgets_of_palette_change()
+{
+ QEvent application_event(QEvent::ApplicationPaletteChange);
+ QCoreApplication::sendEvent(qApp, &application_event);
+ for (auto* widget : QApplication::allWidgets()) {
+ QEvent event(QEvent::PaletteChange);
+ QCoreApplication::sendEvent(widget, &event);
+ }
+}
+
SystemAppearance::SystemAppearance()
{
m_prefers_dark = query_system_prefers_dark();
@@ -28,14 +41,8 @@ SystemAppearance::SystemAppearance()
update();
});
// The chrome's colors depend on the contrast preference, but changing it does not change the application palette.
- // Widgets restyle themselves on palette changes, so tell them about one.
QObject::connect(QGuiApplication::styleHints()->accessibility(), &QAccessibilityHints::contrastPreferenceChanged, this, [] {
- QEvent application_event(QEvent::ApplicationPaletteChange);
- QCoreApplication::sendEvent(qApp, &application_event);
- for (auto* widget : QApplication::allWidgets()) {
- QEvent event(QEvent::PaletteChange);
- QCoreApplication::sendEvent(widget, &event);
- }
+ notify_widgets_of_palette_change();
});
observe_platform_changes();
}
@@ -69,6 +76,15 @@ void SystemAppearance::apply_interface_theme(WebView::ThemePreference theme)
}
}
+void SystemAppearance::apply_color_theme(WebView::ColorTheme theme)
+{
+ if (theme == m_color_theme)
+ return;
+ m_color_theme = theme;
+ Theme::set_color_theme(theme);
+ notify_widgets_of_palette_change();
+}
+
#if !defined(AK_OS_MACOS)
bool SystemAppearance::query_system_prefers_dark() const
{
diff --git a/UI/Qt/SystemAppearance.h b/UI/Qt/SystemAppearance.h
index 4a8092a11dd..32f12032e45 100644
--- a/UI/Qt/SystemAppearance.h
+++ b/UI/Qt/SystemAppearance.h
@@ -28,6 +28,7 @@ public:
static bool prefers_reduced_motion();
void apply_interface_theme(WebView::ThemePreference);
+ void apply_color_theme(WebView::ColorTheme);
signals:
void changed();
@@ -41,6 +42,7 @@ private:
bool m_prefers_dark { false };
WebView::ThemePreference m_interface_theme { WebView::ThemePreference::System };
+ WebView::ColorTheme m_color_theme { WebView::ColorTheme::Neutral };
};
}
diff --git a/UI/Qt/Theme.cpp b/UI/Qt/Theme.cpp
index a6c36ef7f2a..9bdc056151c 100644
--- a/UI/Qt/Theme.cpp
+++ b/UI/Qt/Theme.cpp
@@ -5,6 +5,8 @@
*/
#include <AK/Array.h>
+#include <AK/Math.h>
+#include <LibWebView/Settings.h>
#include <UI/Qt/Theme.h>
#include <QAccessibilityHints>
@@ -191,11 +193,103 @@ static PanelColors const dark_panel {
.information = rgba(10, 122, 255, 0.22),
};
+// A color theme is made from one or two base colors. Its chrome and panel colors are derived from those of the neutral
+// theme, by giving each surface the tint's hue while keeping its lightness, so contrast stays as it is.
+// NB: Violet surfaces mark private windows, so no theme should tint with a violet hue.
+struct ColorThemeDefinition {
+ // The hue the surfaces take on, or none to keep them neutral.
+ Optional<QColor> tint;
+ // The color of focus rings, selection and selected rows, or none to follow the system's accent color.
+ Optional<QColor> accent;
+};
+
+// Each tinted theme is made from a single base color, which is both its tint and its accent.
+static ColorThemeDefinition color_theme_definition(WebView::ColorTheme theme)
+{
+ auto from_base_color = [](QRgb base_color) {
+ return ColorThemeDefinition { .tint = rgb(base_color), .accent = rgb(base_color) };
+ };
+
+ switch (theme) {
+ case WebView::ColorTheme::Neutral:
+ return {};
+ case WebView::ColorTheme::Harbor:
+ return from_base_color(0x2f6fd6);
+ case WebView::ColorTheme::Glacier:
+ return from_base_color(0x1798b8);
+ case WebView::ColorTheme::Lagoon:
+ return from_base_color(0x12957f);
+ case WebView::ColorTheme::Moss:
+ return from_base_color(0x3b8f3e);
+ case WebView::ColorTheme::Saffron:
+ return from_base_color(0xc99500);
+ case WebView::ColorTheme::Copper:
+ return from_base_color(0xd0631c);
+ case WebView::ColorTheme::Cherry:
+ return from_base_color(0xd1344b);
+ case WebView::ColorTheme::Blossom:
+ return from_base_color(0xd2459a);
+ }
+ VERIFY_NOT_REACHED();
+}
+
+// How saturated the surfaces are in the tint's hue, and the text and icons on them, which take on only a trace of it.
+static constexpr float SURFACE_TINT_SATURATION = 0.22f;
+static constexpr float INK_TINT_SATURATION = 0.12f;
+
+// NB: Translucent fills, such as hovers and the address field, stay neutral, and take on the tint of the surface below.
+static void tint(QColor& color, QColor const& tint, float saturation)
+{
+ if (color.alpha() != 255)
+ return;
+ color = QColor::fromHslF(tint.hslHueF(), saturation, color.lightnessF());
+}
+
+static ChromeColors derive_chrome(ChromeColors const& neutral, Optional<QColor> const& tint_color)
+{
+ auto colors = neutral;
+ if (!tint_color.has_value())
+ return colors;
+ for (auto* surface : { &colors.strip, &colors.toolbar, &colors.sidebar, &colors.field_focused, &colors.separator, &colors.divider })
+ tint(*surface, *tint_color, SURFACE_TINT_SATURATION);
+ for (auto* ink : { &colors.text, &colors.muted, &colors.tab_title, &colors.icon, &colors.disabled, &colors.row_title })
+ tint(*ink, *tint_color, INK_TINT_SATURATION);
+ return colors;
+}
+
+static PanelColors derive_panel(PanelColors const& neutral, Optional<QColor> const& tint_color)
+{
+ auto colors = neutral;
+ if (!tint_color.has_value())
+ return colors;
+ for (auto* surface : { &colors.surface, &colors.separator, &colors.separator_soft, &colors.page })
+ tint(*surface, *tint_color, SURFACE_TINT_SATURATION);
+ for (auto* ink : { &colors.text, &colors.body, &colors.muted, &colors.shortcut })
+ tint(*ink, *tint_color, INK_TINT_SATURATION);
+ return colors;
+}
+
+static ColorThemeDefinition s_color_theme;
+static ChromeColors s_light_chrome = light_chrome;
+static ChromeColors s_dark_chrome = dark_chrome;
+static PanelColors s_light_panel = light_panel;
+static PanelColors s_dark_panel = dark_panel;
+
+void set_color_theme(WebView::ColorTheme theme)
+{
+ s_color_theme = color_theme_definition(theme);
+ s_light_chrome = derive_chrome(light_chrome, s_color_theme.tint);
+ s_dark_chrome = derive_chrome(dark_chrome, s_color_theme.tint);
+ s_light_panel = derive_panel(light_panel, s_color_theme.tint);
+ s_dark_panel = derive_panel(dark_panel, s_color_theme.tint);
+}
+
+// NB: With increased contrast, the chrome stays neutral whatever the color theme.
ChromeColors const& chrome()
{
if (has_increased_contrast())
return is_dark() ? dark_increased_contrast_chrome : light_increased_contrast_chrome;
- return is_dark() ? dark_chrome : light_chrome;
+ return is_dark() ? s_dark_chrome : s_light_chrome;
}
static constexpr auto PRIVATE_WINDOW_PROPERTY = "ladybirdPrivateWindow";
@@ -243,7 +337,7 @@ bool is_dark(QWidget const& widget)
PanelColors const& panel()
{
- return is_dark() ? dark_panel : light_panel;
+ return is_dark() ? s_dark_panel : s_light_panel;
}
struct AccentColor {
@@ -284,8 +378,46 @@ static AccentColor const& system_accent_color()
return *closest;
}
+// https://www.w3.org/TR/WCAG21/#dfn-relative-luminance
+static float relative_luminance(QColor const& color)
+{
+ auto linear = [](float channel) { return channel <= 0.04045f ? channel / 12.92f : AK::pow((channel + 0.055f) / 1.055f, 2.4f); };
+ return (0.2126f * linear(color.redF())) + (0.7152f * linear(color.greenF())) + (0.0722f * linear(color.blueF()));
+}
+
+// https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio
+static float contrast_ratio(QColor const& a, QColor const& b)
+{
+ auto lighter = max(relative_luminance(a), relative_luminance(b));
+ auto darker = min(relative_luminance(a), relative_luminance(b));
+ return (lighter + 0.05f) / (darker + 0.05f);
+}
+
+enum class Adjustment {
+ Darken,
+ Lighten,
+};
+
+// Steps the color's lightness until it has at least the given contrast with the background.
+static QColor with_contrast(QColor color, QColor const& background, float minimum_ratio, Adjustment adjustment)
+{
+ auto step = adjustment == Adjustment::Darken ? -0.02f : 0.02f;
+ while (contrast_ratio(color, background) < minimum_ratio) {
+ auto lightness = color.lightnessF() + step;
+ if (lightness <= 0 || lightness >= 1)
+ break;
+ color = QColor::fromHslF(color.hslHueF(), color.hslSaturationF(), lightness);
+ }
+ return color;
+}
+
+// The theme's accent clears 3:1 on the toolbar, darker on the light appearance and lighter on the dark one.
QColor accent()
{
+ if (s_color_theme.accent.has_value()) {
+ auto dark = is_dark();
+ return with_contrast(*s_color_theme.accent, chrome().toolbar, 3, dark ? Adjustment::Lighten : Adjustment::Darken);
+ }
auto const& accent = system_accent_color();
return is_dark() ? accent.dark : accent.light;
}
@@ -322,20 +454,36 @@ QColor selection()
return color;
}
+// The theme's accent fills primary buttons and selected rows deep enough that white text on them clears 4.5:1.
+static QColor accent_fill()
+{
+ return with_contrast(*s_color_theme.accent, Qt::white, 4.5f, Adjustment::Darken);
+}
+
QColor primary()
{
+ if (s_color_theme.accent.has_value())
+ return accent_fill();
return rgb(0x0a66d0);
}
// A selected row is filled with the primary blue, a little softer on the dark appearance.
QColor selected_row()
{
+ if (s_color_theme.accent.has_value())
+ return accent_fill();
return is_dark() ? rgb(0x2463b8) : primary();
}
-QColor brand()
+// A tinted theme's logomark clears 4.5:1 on both the strip and the toolbar, as the Ladybird button sits on either,
+// depending on where the tabs are.
+QColor logomark()
{
- return is_dark() ? rgb(0x7a4ce6) : rgb(0x6a39db);
+ if (!s_color_theme.accent.has_value())
+ return is_dark() ? rgb(0xa684ff) : rgb(0x4b22ad);
+ auto adjustment = is_dark() ? Adjustment::Lighten : Adjustment::Darken;
+ auto color = with_contrast(*s_color_theme.accent, chrome().strip, 4.5f, adjustment);
+ return with_contrast(color, chrome().toolbar, 4.5f, adjustment);
}
}
diff --git a/UI/Qt/Theme.h b/UI/Qt/Theme.h
index 66ad1c20fd5..942a6979e4f 100644
--- a/UI/Qt/Theme.h
+++ b/UI/Qt/Theme.h
@@ -6,11 +6,19 @@
#pragma once
+#include <AK/Types.h>
+
#include <QColor>
#include <QPalette>
class QWidget;
+namespace WebView {
+
+enum class ColorTheme : u8;
+
+}
+
namespace Ladybird::Theme {
// The colors of the window chrome: the tab strip, the toolbar and what sits on them.
@@ -62,6 +70,9 @@ struct PanelColors {
bool is_dark();
bool has_increased_contrast();
+// The chrome and panel colors, and the accent, are derived from the color theme's base colors.
+void set_color_theme(WebView::ColorTheme);
+
ChromeColors const& chrome();
// A private window's chrome is violet-dark in either appearance, so it never looks like a normal window. The chrome
@@ -75,8 +86,8 @@ bool is_dark(QPalette const&);
bool is_dark(QWidget const&);
PanelColors const& panel();
-// The system's accent color, adjusted so it stays clear on the chrome in light and dark appearances. It is used only
-// for focus rings, selection and load progress.
+// The color theme's accent, or the system's accent color if the theme has none, adjusted so it stays clear on the chrome
+// in light and dark appearances. It is used only for focus rings, selection and load progress.
QColor accent();
// A focus ring is a fine line of the focus edge color right outside the focused control, in a wider, fainter halo.
QColor focus_edge();
@@ -88,7 +99,8 @@ QColor primary();
// The fill of the selected row of a menu or a list navigated with the keyboard.
QColor selected_row();
-// Ladybird purple, used for the default profile's tile and brand accents.
-QColor brand();
+// The color of Ladybird's logomark on the chrome: Ladybird purple in the neutral theme, and a tinted theme's base color,
+// so the window shows a single hue.
+QColor logomark();
}
diff --git a/UI/Qt/main.cpp b/UI/Qt/main.cpp
index ef758ebb605..82bbabf04cd 100644
--- a/UI/Qt/main.cpp
+++ b/UI/Qt/main.cpp
@@ -76,6 +76,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
Ladybird::set_profile_application_icon(Ladybird::Application::profile());
#endif
Ladybird::SystemAppearance::the().apply_interface_theme(Ladybird::Application::settings().appearance().interface_theme);
+ Ladybird::SystemAppearance::the().apply_color_theme(Ladybird::Application::settings().appearance().color_theme);
app->on_open_file = [&](auto const& file_url) {
if (auto* window = app->non_private_window_if_any()) {