diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-05-14 08:00:21 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-14 08:00:21 +0900 |
commit | fc8a8bb521daeb6ef2fd3353ba516c8393c27f7e (patch) | |
tree | ae02fdbb0716f0cbd8a2340f46e5874104f7225f /lib/user.cpp | |
parent | 1a034626bcbe064ebe0ada8cdfe1a47f2d82e477 (diff) | |
parent | 74fa9bc64128d88939259ccb2ba4dca51571559a (diff) | |
download | libquotient-fc8a8bb521daeb6ef2fd3353ba516c8393c27f7e.tar.gz libquotient-fc8a8bb521daeb6ef2fd3353ba516c8393c27f7e.zip |
Merge pull request #298 from a-andreyev/aa13q-fancy-colors
Provide a colour code for the user
Diffstat (limited to 'lib/user.cpp')
-rw-r--r-- | lib/user.cpp | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/user.cpp b/lib/user.cpp index 17db5760..fdb82a38 100644 --- a/lib/user.cpp +++ b/lib/user.cpp @@ -33,6 +33,9 @@ #include <QtCore/QStringBuilder> #include <QtCore/QElapsedTimer> +#include <QtCore/QCryptographicHash> +#include <QtCore/QtEndian> + #include <functional> using namespace QMatrixClient; @@ -47,8 +50,21 @@ class User::Private return Avatar(move(url)); } + qreal makeHueF(QString userId) + { + QByteArray hash = QCryptographicHash::hash(userId.toUtf8(), + QCryptographicHash::Sha1); + QDataStream dataStream(qToLittleEndian(hash).left(2)); + dataStream.setByteOrder(QDataStream::LittleEndian); + quint16 hashValue; + dataStream >> hashValue; + qreal hueF = static_cast<qreal>(hashValue)/std::numeric_limits<quint16>::max(); + Q_ASSERT((0 <= hueF) && (hueF <= 1)); + return hueF; + } + Private(QString userId, Connection* connection) - : userId(move(userId)), connection(connection) + : userId(move(userId)), connection(connection), hueF(makeHueF(userId)) { } QString userId; @@ -57,6 +73,7 @@ class User::Private QString bridged; QString mostUsedName; QMultiHash<QString, const Room*> otherNames; + qreal hueF; Avatar mostUsedAvatar { makeAvatar({}) }; std::vector<Avatar> otherAvatars; auto otherAvatar(const QUrl& url) @@ -222,6 +239,11 @@ bool User::isGuest() const return *it == ':'; } +int User::hue() const +{ + return int(hueF()*359); +} + QString User::name(const Room* room) const { return d->nameForRoom(room); @@ -428,3 +450,7 @@ void User::processEvent(const RoomMemberEvent& event, const Room* room, updateAvatarUrl(event.avatarUrl(), d->avatarUrlForRoom(room), room); } } + +qreal User::hueF() const { + return d->hueF; +} |