Moved getRandomColor method to util/Helpers.cpp (#2974)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
Paweł 2021-07-08 19:09:31 +02:00 committed by GitHub
parent 24aee42171
commit b6ee2280d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 33 deletions

View file

@ -5,9 +5,9 @@
#include "controllers/ignores/IgnorePhrase.hpp" #include "controllers/ignores/IgnorePhrase.hpp"
#include "messages/Message.hpp" #include "messages/Message.hpp"
#include "messages/MessageElement.hpp" #include "messages/MessageElement.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/Helpers.hpp"
#include "util/StreamerMode.hpp" #include "util/StreamerMode.hpp"
#include <QFileInfo> #include <QFileInfo>
@ -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() void SharedMessageBuilder::parse()
{ {
this->parseUsernameColor(); this->parseUsernameColor();

View file

@ -9,13 +9,13 @@
#include "providers/ffz/FfzBadges.hpp" #include "providers/ffz/FfzBadges.hpp"
#include "providers/twitch/TwitchBadges.hpp" #include "providers/twitch/TwitchBadges.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "providers/twitch/TwitchIrcServer.hpp" #include "providers/twitch/TwitchIrcServer.hpp"
#include "singletons/Emotes.hpp" #include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "singletons/Theme.hpp" #include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/Helpers.hpp"
#include "util/IrcHelpers.hpp" #include "util/IrcHelpers.hpp"
#include "widgets/Window.hpp" #include "widgets/Window.hpp"
@ -48,21 +48,6 @@ namespace chatterino {
namespace { 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) QStringList parseTagList(const QVariantMap &tags, const QString &key)
{ {
auto iterator = tags.find(key); auto iterator = tags.find(key);
@ -601,7 +586,8 @@ void TwitchMessageBuilder::parseUsernameColor()
if (getSettings()->colorizeNicknames && this->tags.contains("user-id")) 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_; this->message().usernameColor = this->usernameColor_;
} }
} }

View file

@ -1,5 +1,7 @@
#include "Helpers.hpp" #include "Helpers.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <QLocale> #include <QLocale>
#include <QUuid> #include <QUuid>
@ -47,4 +49,23 @@ QString kFormatNumbers(const int &number)
return QString("%1K").arg(number / 1000); 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 } // namespace chatterino

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <QColor>
#include <QString> #include <QString>
namespace chatterino { namespace chatterino {
@ -17,4 +18,6 @@ QString localizeNumbers(const int &number);
QString kFormatNumbers(const int &number); QString kFormatNumbers(const int &number);
QColor getRandomColor(const QString &userId);
} // namespace chatterino } // namespace chatterino