Skip to content

Commit 29a7ed8

Browse files
committed
Provide a colour code for the user
Contributes to #296
1 parent b467b08 commit 29a7ed8

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/user.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#include <QtCore/QStringBuilder>
3434
#include <QtCore/QElapsedTimer>
3535

36+
#include <QtCore/QCryptographicHash>
37+
#include <QtCore/QtEndian>
38+
3639
#include <functional>
3740

3841
using 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+
222247
QString 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+
}

lib/user.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ namespace QMatrixClient
3333
Q_OBJECT
3434
Q_PROPERTY(QString id READ id CONSTANT)
3535
Q_PROPERTY(bool isGuest READ isGuest CONSTANT)
36+
Q_PROPERTY(int hue READ hue CONSTANT)
37+
Q_PROPERTY(qreal hueF READ hueF CONSTANT)
3638
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
3739
Q_PROPERTY(QString displayName READ displayname NOTIFY nameChanged STORED false)
3840
Q_PROPERTY(QString fullName READ fullName NOTIFY nameChanged STORED false)
@@ -95,6 +97,15 @@ namespace QMatrixClient
9597
*/
9698
bool isGuest() const;
9799

100+
/** Hue color component of this user based on id.
101+
* The implementation is based on XEP-0392:
102+
* https://xmpp.org/extensions/xep-0392.html
103+
* Naming and ranges are the same as QColor's hue methods:
104+
* https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
105+
*/
106+
int hue() const;
107+
qreal hueF() const;
108+
98109
const Avatar& avatarObject(const Room* room = nullptr) const;
99110
Q_INVOKABLE QImage avatar(int dimension, const Room* room = nullptr);
100111
Q_INVOKABLE QImage avatar(int requestedWidth, int requestedHeight,

0 commit comments

Comments
 (0)