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
4 changed files with 28 additions and 33 deletions
+21
View File
@@ -1,5 +1,7 @@
#include "Helpers.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <QLocale>
#include <QUuid>
@@ -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