Skip to content

Commit fc8a8bb

Browse files
authored
Merge pull request #298 from a-andreyev/aa13q-fancy-colors
Provide a colour code for the user
2 parents 1a03462 + 74fa9bc commit fc8a8bb

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/user.cpp

Lines changed: 27 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,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+
225247
QString 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+
}

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)