Moved getRandomColor method to util/Helpers.cpp (#2974)
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user