From b6ee2280d2e8605f48f21b9ac9a33620e86e7272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= Date: Thu, 8 Jul 2021 19:09:31 +0200 Subject: [PATCH] Moved getRandomColor method to util/Helpers.cpp (#2974) Co-authored-by: pajlada --- src/messages/SharedMessageBuilder.cpp | 17 +-------------- src/providers/twitch/TwitchMessageBuilder.cpp | 20 +++--------------- src/util/Helpers.cpp | 21 +++++++++++++++++++ src/util/Helpers.hpp | 3 +++ 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/messages/SharedMessageBuilder.cpp b/src/messages/SharedMessageBuilder.cpp index e3f1a1800..1235af3c6 100644 --- a/src/messages/SharedMessageBuilder.cpp +++ b/src/messages/SharedMessageBuilder.cpp @@ -5,9 +5,9 @@ #include "controllers/ignores/IgnorePhrase.hpp" #include "messages/Message.hpp" #include "messages/MessageElement.hpp" -#include "providers/twitch/TwitchCommon.hpp" #include "singletons/Settings.hpp" #include "singletons/WindowManager.hpp" +#include "util/Helpers.hpp" #include "util/StreamerMode.hpp" #include @@ -88,21 +88,6 @@ SharedMessageBuilder::SharedMessageBuilder( { } -namespace { - - QColor getRandomColor(const QString &v) - { - int colorSeed = 0; - for (const auto &c : v) - { - colorSeed += c.digitValue(); - } - const auto colorIndex = colorSeed % TWITCH_USERNAME_COLORS.size(); - return TWITCH_USERNAME_COLORS[colorIndex]; - } - -} // namespace - void SharedMessageBuilder::parse() { this->parseUsernameColor(); diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 766642f59..e3cb222c5 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -9,13 +9,13 @@ #include "providers/ffz/FfzBadges.hpp" #include "providers/twitch/TwitchBadges.hpp" #include "providers/twitch/TwitchChannel.hpp" -#include "providers/twitch/TwitchCommon.hpp" #include "providers/twitch/TwitchIrcServer.hpp" #include "singletons/Emotes.hpp" #include "singletons/Resources.hpp" #include "singletons/Settings.hpp" #include "singletons/Theme.hpp" #include "singletons/WindowManager.hpp" +#include "util/Helpers.hpp" #include "util/IrcHelpers.hpp" #include "widgets/Window.hpp" @@ -48,21 +48,6 @@ namespace chatterino { namespace { - QColor getRandomColor(const QVariant &userId) - { - bool ok = true; - int colorSeed = userId.toInt(&ok); - if (!ok) - { - // We were unable to convert the user ID to an integer, this means Twitch has decided to start using non-integer user IDs - // Just randomize the users color - colorSeed = std::rand(); - } - - const auto colorIndex = colorSeed % TWITCH_USERNAME_COLORS.size(); - return TWITCH_USERNAME_COLORS[colorIndex]; - } - QStringList parseTagList(const QVariantMap &tags, const QString &key) { auto iterator = tags.find(key); @@ -601,7 +586,8 @@ void TwitchMessageBuilder::parseUsernameColor() if (getSettings()->colorizeNicknames && this->tags.contains("user-id")) { - this->usernameColor_ = getRandomColor(this->tags.value("user-id")); + this->usernameColor_ = + getRandomColor(this->tags.value("user-id").toString()); this->message().usernameColor = this->usernameColor_; } } diff --git a/src/util/Helpers.cpp b/src/util/Helpers.cpp index e8bbbd134..5528ac23a 100644 --- a/src/util/Helpers.cpp +++ b/src/util/Helpers.cpp @@ -1,5 +1,7 @@ #include "Helpers.hpp" +#include "providers/twitch/TwitchCommon.hpp" + #include #include @@ -47,4 +49,23 @@ QString kFormatNumbers(const int &number) return QString("%1K").arg(number / 1000); } +QColor getRandomColor(const QString &userId) +{ + bool ok = true; + int colorSeed = userId.toInt(&ok); + if (!ok) + { + // We were unable to convert the user ID to an integer, this means Twitch started to use non-integer user IDs (or we're on IRC) + // Use sum of unicode values of all characters in id / IRC nick + colorSeed = 0; + for (const auto &c : userId) + { + colorSeed += c.digitValue(); + } + } + + const auto colorIndex = colorSeed % TWITCH_USERNAME_COLORS.size(); + return TWITCH_USERNAME_COLORS[colorIndex]; +} + } // namespace chatterino diff --git a/src/util/Helpers.hpp b/src/util/Helpers.hpp index 23c4d4f21..b30a3f331 100644 --- a/src/util/Helpers.hpp +++ b/src/util/Helpers.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace chatterino { @@ -17,4 +18,6 @@ QString localizeNumbers(const int &number); QString kFormatNumbers(const int &number); +QColor getRandomColor(const QString &userId); + } // namespace chatterino