3333#include < QtCore/QStringBuilder>
3434#include < QtCore/QElapsedTimer>
3535
36+ #include < QtCore/QCryptographicHash>
37+ #include < QtCore/QtEndian>
38+
3639#include < functional>
3740
3841using namespace QMatrixClient ;
@@ -47,8 +50,21 @@ class User::Private
4750 return Avatar (move (url));
4851 }
4952
53+ qreal makeHueF (QString userId)
54+ {
55+ QByteArray hash = QCryptographicHash::hash (userId.toUtf8 (),
56+ QCryptographicHash::Sha1);
57+ QDataStream dataStream (qToLittleEndian (hash).left (2 ));
58+ dataStream.setByteOrder (QDataStream::LittleEndian);
59+ quint16 hashValue;
60+ dataStream >> hashValue;
61+ qreal hueF = static_cast <qreal>(hashValue)/std::numeric_limits<quint16>::max ();
62+ Q_ASSERT ((0 <= hueF) && (hueF <= 1 ));
63+ return hueF;
64+ }
65+
5066 Private (QString userId, Connection* connection)
51- : userId(move(userId)), connection(connection)
67+ : userId(move(userId)), connection(connection), hueF(makeHueF(userId))
5268 { }
5369
5470 QString userId;
@@ -57,6 +73,7 @@ class User::Private
5773 QString bridged;
5874 QString mostUsedName;
5975 QMultiHash<QString, const Room*> otherNames;
76+ qreal hueF;
6077 Avatar mostUsedAvatar { makeAvatar ({}) };
6178 std::vector<Avatar> otherAvatars;
6279 auto otherAvatar (const QUrl& url)
@@ -222,6 +239,11 @@ bool User::isGuest() const
222239 return *it == ' :' ;
223240}
224241
242+ int User::hue () const
243+ {
244+ return int (hueF ()*359 );
245+ }
246+
225247QString User::name (const Room* room) const
226248{
227249 return d->nameForRoom (room);
@@ -428,3 +450,7 @@ void User::processEvent(const RoomMemberEvent& event, const Room* room,
428450 updateAvatarUrl (event.avatarUrl (), d->avatarUrlForRoom (room), room);
429451 }
430452}
453+
454+ qreal User::hueF () const {
455+ return d->hueF ;
456+ }
0 commit comments