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,25 @@ class User::Private
4750 return Avatar (move (url));
4851 }
4952
53+ int makeHue (qreal hueF)
54+ {
55+ return static_cast <int >(hueF*360 );
56+ }
57+
58+ qreal makeHueF (QString userId)
59+ {
60+ QByteArray hash = QCryptographicHash::hash (userId.toUtf8 (),
61+ QCryptographicHash::Sha1);
62+ QDataStream dataStream (qToLittleEndian (hash).left (2 ));
63+ dataStream.setByteOrder (QDataStream::LittleEndian);
64+ quint16 hashValue;
65+ dataStream >> hashValue;
66+ qreal hueF = static_cast <qreal>(hashValue)/std::numeric_limits<quint16>::max ();
67+ return hueF;
68+ }
69+
5070 Private (QString userId, Connection* connection)
51- : userId(move(userId)), connection(connection)
71+ : userId(move(userId)), connection(connection), hueF(makeHueF(userId)), hue(makeHue(hueF))
5272 { }
5373
5474 QString userId;
@@ -57,6 +77,8 @@ class User::Private
5777 QString bridged;
5878 QString mostUsedName;
5979 QMultiHash<QString, const Room*> otherNames;
80+ qreal hueF;
81+ int hue;
6082 Avatar mostUsedAvatar { makeAvatar ({}) };
6183 std::vector<Avatar> otherAvatars;
6284 auto otherAvatar (QUrl url)
@@ -219,6 +241,11 @@ bool User::isGuest() const
219241 return *it == ' :' ;
220242}
221243
244+ int User::hue () const
245+ {
246+ return d->hue ;
247+ }
248+
222249QString User::name (const Room* room) const
223250{
224251 return d->nameForRoom (room);
@@ -424,3 +451,8 @@ void User::processEvent(const RoomMemberEvent& event, const Room* room)
424451 updateAvatarUrl (event.avatarUrl (), d->avatarUrlForRoom (room), room);
425452 }
426453}
454+
455+ qreal User::hueF () const
456+ {
457+ return d->hueF ;
458+ }
0 commit comments