From 95c909b33755294a0227ded3c6941ec87d9d50fe Mon Sep 17 00:00:00 2001
From: Leon Richardt <leon.richardt@gmail.com>
Date: Sun, 22 Mar 2020 13:14:07 +0100
Subject: [PATCH] Better Highlights: Fix Wrong Color for Migrated Phrases
 (#1568)

* Better Highlights: Fix wrong color for migrated phrases

Prior to this commit, no default color was set when an "old" highlight
phrase (one added prior to #1320 / 5957551) was deserialized. This
commit makes highlight phrases uses the default self-highlight color for
these situations. This approach is reasonably sensible since that color
is also similar to the old highlight color.

Fixes #1565.

* Update default self-highlight color

The new default color was suggested in [1] by @RAnders00. Refer to the
link for further information.

[1]: https://github.com/Chatterino/chatterino2/issues/1565#issuecomment-590441625

* Theme: Remove highlight color

As highlight color is independent of the selected theme now, the member
has been removed from the `Theme` singleton. Instead, the fallback theme
color is defined in `HighlightPhrase` now.

Uses of `themes->messages.backgrounds.highlighted` have been replaced
with `HighlightPhrase::FALLBACK_COLOR` accordingly.

* Update src/controllers/highlights/HighlightPhrase.hpp

attempt to just remove constexpr

Co-Authored-By: Ruben Anders <ruben.anders@robotty.de>

* Initialize FALLBACK_COLOR outside header file

* Rename FALLBACK_COLOR to FALLBACK_HIGHLIGHT_COLOR

In preparation for the next commit introducing a new variable.

* Moved subscription highlight color into HighlightPhrase

* Use actual subscription color as a fallback

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
Co-authored-by: Ruben Anders <ruben.anders@robotty.de>
---
 src/BaseTheme.cpp                              | 15 ---------------
 src/BaseTheme.hpp                              |  2 --
 src/controllers/highlights/HighlightPhrase.cpp |  3 +++
 src/controllers/highlights/HighlightPhrase.hpp |  9 +++++++++
 src/providers/colors/ColorProvider.cpp         | 14 +++++++-------
 src/singletons/Theme.cpp                       | 11 -----------
 6 files changed, 19 insertions(+), 35 deletions(-)

diff --git a/src/BaseTheme.cpp b/src/BaseTheme.cpp
index 99ddddb24..306f13f5c 100644
--- a/src/BaseTheme.cpp
+++ b/src/BaseTheme.cpp
@@ -159,21 +159,6 @@ void AB_THEME_CLASS::actuallyUpdate(double hue, double multiplier)
     this->messages.backgrounds.regular = getColor(0, sat, 1);
     this->messages.backgrounds.alternate = getColor(0, sat, 0.96);
 
-    if (isLight_)
-    {
-        this->messages.backgrounds.highlighted =
-            blendColors(themeColor, this->messages.backgrounds.regular, 0.8);
-    }
-    else
-    {
-        // REMOVED
-        // this->messages.backgrounds.highlighted =
-        //    QColor(getSettings()->highlightColor);
-    }
-
-    this->messages.backgrounds.subscription =
-        blendColors(QColor("#C466FF"), this->messages.backgrounds.regular, 0.7);
-
     // this->messages.backgrounds.resub
     // this->messages.backgrounds.whisper
     this->messages.disabled = getColor(0, sat, 1, 0.6);
diff --git a/src/BaseTheme.hpp b/src/BaseTheme.hpp
index d99808133..36e14c07f 100644
--- a/src/BaseTheme.hpp
+++ b/src/BaseTheme.hpp
@@ -64,8 +64,6 @@ public:
         struct {
             QColor regular;
             QColor alternate;
-            QColor highlighted;
-            QColor subscription;
             // QColor whisper;
         } backgrounds;
 
diff --git a/src/controllers/highlights/HighlightPhrase.cpp b/src/controllers/highlights/HighlightPhrase.cpp
index 40942df3d..94529b62f 100644
--- a/src/controllers/highlights/HighlightPhrase.cpp
+++ b/src/controllers/highlights/HighlightPhrase.cpp
@@ -2,6 +2,9 @@
 
 namespace chatterino {
 
+QColor HighlightPhrase::FALLBACK_HIGHLIGHT_COLOR = QColor(127, 63, 73, 127);
+QColor HighlightPhrase::FALLBACK_SUB_COLOR = QColor(196, 102, 255, 100);
+
 bool HighlightPhrase::operator==(const HighlightPhrase &other) const
 {
     return std::tie(this->pattern_, this->hasSound_, this->hasAlert_,
diff --git a/src/controllers/highlights/HighlightPhrase.hpp b/src/controllers/highlights/HighlightPhrase.hpp
index 72849bb66..cbabb3e41 100644
--- a/src/controllers/highlights/HighlightPhrase.hpp
+++ b/src/controllers/highlights/HighlightPhrase.hpp
@@ -70,6 +70,13 @@ public:
     const QUrl &getSoundUrl() const;
     const std::shared_ptr<QColor> getColor() const;
 
+    /*
+     * XXX: Use the constexpr constructor here once we are building with
+     * Qt>=5.13.
+     */
+    static QColor FALLBACK_HIGHLIGHT_COLOR;
+    static QColor FALLBACK_SUB_COLOR;
+
 private:
     QString pattern_;
     bool hasAlert_;
@@ -132,6 +139,8 @@ struct Deserialize<chatterino::HighlightPhrase> {
         chatterino::rj::getSafe(value, "color", encodedColor);
 
         auto _color = QColor(encodedColor);
+        if (!_color.isValid())
+            _color = chatterino::HighlightPhrase::FALLBACK_HIGHLIGHT_COLOR;
 
         return chatterino::HighlightPhrase(_pattern, _hasAlert, _hasSound,
                                            _isRegex, _isCaseSensitive,
diff --git a/src/providers/colors/ColorProvider.cpp b/src/providers/colors/ColorProvider.cpp
index e1be8d6f6..43d651248 100644
--- a/src/providers/colors/ColorProvider.cpp
+++ b/src/providers/colors/ColorProvider.cpp
@@ -64,7 +64,6 @@ void ColorProvider::initTypeColorMap()
 {
     // Read settings for custom highlight colors and save them in map.
     // If no custom values can be found, set up default values instead.
-    auto backgrounds = getApp()->themes->messages.backgrounds;
 
     QString customColor = getSettings()->selfHighlightColor;
     if (QColor(customColor).isValid())
@@ -76,7 +75,8 @@ void ColorProvider::initTypeColorMap()
     {
         this->typeColorMap_.insert(
             {ColorType::SelfHighlight,
-             std::make_shared<QColor>(backgrounds.highlighted)});
+             std::make_shared<QColor>(
+                 HighlightPhrase::FALLBACK_HIGHLIGHT_COLOR)});
     }
 
     customColor = getSettings()->subHighlightColor;
@@ -89,7 +89,7 @@ void ColorProvider::initTypeColorMap()
     {
         this->typeColorMap_.insert(
             {ColorType::Subscription,
-             std::make_shared<QColor>(backgrounds.subscription)});
+             std::make_shared<QColor>(HighlightPhrase::FALLBACK_SUB_COLOR)});
     }
 
     customColor = getSettings()->whisperHighlightColor;
@@ -102,7 +102,8 @@ void ColorProvider::initTypeColorMap()
     {
         this->typeColorMap_.insert(
             {ColorType::Whisper,
-             std::make_shared<QColor>(backgrounds.highlighted)});
+             std::make_shared<QColor>(
+                 HighlightPhrase::FALLBACK_HIGHLIGHT_COLOR)});
     }
 }
 
@@ -115,9 +116,8 @@ void ColorProvider::initDefaultColors()
     this->defaultColors_.emplace_back(143, 48, 24, 127);   // Red-ish
     this->defaultColors_.emplace_back(28, 141, 117, 127);  // Cyan-ish
 
-    auto backgrounds = getApp()->themes->messages.backgrounds;
-    this->defaultColors_.push_back(backgrounds.highlighted);
-    this->defaultColors_.push_back(backgrounds.subscription);
+    this->defaultColors_.push_back(HighlightPhrase::FALLBACK_HIGHLIGHT_COLOR);
+    this->defaultColors_.push_back(HighlightPhrase::FALLBACK_SUB_COLOR);
 }
 
 }  // namespace chatterino
diff --git a/src/singletons/Theme.cpp b/src/singletons/Theme.cpp
index 1876126e2..0411e860e 100644
--- a/src/singletons/Theme.cpp
+++ b/src/singletons/Theme.cpp
@@ -48,8 +48,6 @@ void Theme::actuallyUpdate(double hue, double multiplier)
         this->splits.resizeHandleBackground = QColor(0, 148, 255, 0x20);
     }
 
-    this->messages.backgrounds.highlighted = QColor(140, 84, 89, 127);
-
     this->splits.header.background = getColor(0, sat, flat ? 1 : 0.9);
     this->splits.header.border = getColor(0, sat, flat ? 1 : 0.85);
     this->splits.header.text = this->messages.textColors.regular;
@@ -74,15 +72,6 @@ void Theme::actuallyUpdate(double hue, double multiplier)
     this->splits.background = getColor(0, sat, 1);
     this->splits.dropPreview = QColor(0, 148, 255, 0x30);
     this->splits.dropPreviewBorder = QColor(0, 148, 255, 0xff);
-
-    // Highlighted Messages
-    // hidden setting from PR #744 - if set it will overwrite theme color
-    // TODO: implement full theme support
-    if (getSettings()->highlightColor != "")
-    {
-        this->messages.backgrounds.highlighted =
-            QColor(getSettings()->highlightColor.getValue());
-    }
 }
 
 void Theme::normalizeColor(QColor &color)