From 8959679b3cfe29f756cb7437ff6fd4eeeab05987 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 31 Jul 2024 13:05:54 +0200 Subject: [PATCH] Make IV internal links bg semi-transparent. --- Telegram/SourceFiles/iv/iv_controller.cpp | 9 ++- Telegram/SourceFiles/ui/webview_helpers.cpp | 66 +++++++++++++++++---- Telegram/SourceFiles/ui/webview_helpers.h | 4 ++ 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/Telegram/SourceFiles/iv/iv_controller.cpp b/Telegram/SourceFiles/iv/iv_controller.cpp index 00b645505..e369ab5c0 100644 --- a/Telegram/SourceFiles/iv/iv_controller.cpp +++ b/Telegram/SourceFiles/iv/iv_controller.cpp @@ -65,7 +65,7 @@ namespace { { "box-divider-bg", &st::boxDividerBg }, { "box-divider-fg", &st::boxDividerFg }, { "light-button-fg", &st::lightButtonFg }, - { "light-button-bg-over", &st::lightButtonBgOver }, + //{ "light-button-bg-over", &st::lightButtonBgOver }, { "menu-icon-fg", &st::menuIconFg }, { "menu-icon-fg-over", &st::menuIconFgOver }, { "menu-bg", &st::menuBg }, @@ -82,7 +82,12 @@ namespace { static const auto phrases = base::flat_map>{ { "iv-join-channel", tr::lng_iv_join_channel }, }; - return Ui::ComputeStyles(map, phrases); + return Ui::ComputeStyles(map, phrases) + + ';' + + Ui::ComputeSemiTransparentOverStyle( + "light-button-bg-over", + st::lightButtonBgOver, + st::windowBg); } [[nodiscard]] QByteArray WrapPage(const Prepared &page) { diff --git a/Telegram/SourceFiles/ui/webview_helpers.cpp b/Telegram/SourceFiles/ui/webview_helpers.cpp index 24b7155ce..3be7a6359 100644 --- a/Telegram/SourceFiles/ui/webview_helpers.cpp +++ b/Telegram/SourceFiles/ui/webview_helpers.cpp @@ -10,24 +10,30 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" namespace Ui { +namespace { -[[nodiscard]] QByteArray ComputeStyles( +[[nodiscard]] QByteArray Serialize(const QColor &qt) { + if (qt.alpha() == 255) { + return '#' + + QByteArray::number(qt.red(), 16).right(2) + + QByteArray::number(qt.green(), 16).right(2) + + QByteArray::number(qt.blue(), 16).right(2); + } + return "rgba(" + + QByteArray::number(qt.red()) + "," + + QByteArray::number(qt.green()) + "," + + QByteArray::number(qt.blue()) + "," + + QByteArray::number(qt.alpha() / 255.) + ")"; +} + +} // namespace + +QByteArray ComputeStyles( const base::flat_map &colors, const base::flat_map> &phrases, bool nightTheme) { static const auto serialize = [](const style::color *color) { - const auto qt = (*color)->c; - if (qt.alpha() == 255) { - return '#' - + QByteArray::number(qt.red(), 16).right(2) - + QByteArray::number(qt.green(), 16).right(2) - + QByteArray::number(qt.blue(), 16).right(2); - } - return "rgba(" - + QByteArray::number(qt.red()) + "," - + QByteArray::number(qt.green()) + "," - + QByteArray::number(qt.blue()) + "," - + QByteArray::number(qt.alpha() / 255.) + ")"; + return Serialize((*color)->c); }; static const auto escape = [](tr::phrase<> phrase) { const auto text = phrase(tr::now); @@ -63,6 +69,40 @@ namespace Ui { return result; } +QByteArray ComputeSemiTransparentOverStyle( + const QByteArray &name, + const style::color &over, + const style::color &bg) { + const auto result = [&](const QColor &c) { + return "--td-"_q + name + ':' + Serialize(c) + ';'; + }; + if (over->c.alpha() < 255) { + return result(over->c); + } + // The most transparent color that will still give the same result. + const auto r0 = bg->c.red(); + const auto g0 = bg->c.green(); + const auto b0 = bg->c.blue(); + const auto r1 = over->c.red(); + const auto g1 = over->c.green(); + const auto b1 = over->c.blue(); + const auto mina = [](int c0, int c1) { + return (c0 == c1) + ? 0 + : (c0 > c1) + ? (((c0 - c1) * 255) / c0) + : (((c1 - c0) * 255) / (255 - c0)); + }; + const auto rmina = mina(r0, r1); + const auto gmina = mina(g0, g1); + const auto bmina = mina(b0, b1); + const auto a = std::max({ rmina, gmina, bmina }); + const auto r = (r1 * 255 - r0 * (255 - a)) / a; + const auto g = (g1 * 255 - g0 * (255 - a)) / a; + const auto b = (b1 * 255 - b0 * (255 - a)) / a; + return result(QColor(r, g, b, a)); +} + QByteArray EscapeForAttribute(QByteArray value) { return value .replace('&', "&") diff --git a/Telegram/SourceFiles/ui/webview_helpers.h b/Telegram/SourceFiles/ui/webview_helpers.h index 3d8a51066..518096479 100644 --- a/Telegram/SourceFiles/ui/webview_helpers.h +++ b/Telegram/SourceFiles/ui/webview_helpers.h @@ -20,6 +20,10 @@ namespace Ui { const base::flat_map &colors, const base::flat_map> &phrases, bool nightTheme = false); +[[nodiscard]] QByteArray ComputeSemiTransparentOverStyle( + const QByteArray &name, + const style::color &over, + const style::color &bg); [[nodiscard]] QByteArray EscapeForAttribute(QByteArray value); [[nodiscard]] QByteArray EscapeForScriptString(QByteArray value);