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,20 @@ 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+ return hueF;
63+ }
64+
5065 Private (QString userId, Connection* connection)
51- : userId(move(userId)), connection(connection)
66+ : userId(move(userId)), connection(connection), hueF(makeHueF(userId))
5267 { }
5368
5469 QString userId;
@@ -57,6 +72,11 @@ class User::Private
5772 QString bridged;
5873 QString mostUsedName;
5974 QMultiHash<QString, const Room*> otherNames;
75+ qreal hueF;
76+ int hue ()
77+ {
78+ return static_cast <int >(hueF*360 );
79+ }
6080 Avatar mostUsedAvatar { makeAvatar ({}) };
6181 std::vector<Avatar> otherAvatars;
6282 auto otherAvatar (QUrl url)
@@ -219,6 +239,11 @@ bool User::isGuest() const
219239 return *it == ' :' ;
220240}
221241
242+ int User::hue () const
243+ {
244+ return d->hue ();
245+ }
246+
222247QString User::name (const Room* room) const
223248{
224249 return d->nameForRoom (room);
@@ -424,3 +449,8 @@ void User::processEvent(const RoomMemberEvent& event, const Room* room)
424449 updateAvatarUrl (event.avatarUrl (), d->avatarUrlForRoom (room), room);
425450 }
426451}
452+
453+ qreal User::hueF () const
454+ {
455+ return d->hueF ;
456+ }
0 commit comments