Skip to content

Commit 636b8c1

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

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/user.cpp

Lines changed: 33 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,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+
222249
QString 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+
}

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)