Skip to content

Commit

Permalink
Provide a colour code for the user
Browse files Browse the repository at this point in the history
Contributes to #296
  • Loading branch information
a-andreyev committed May 11, 2019
1 parent b467b08 commit cebca19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include <QtCore/QStringBuilder>
#include <QtCore/QElapsedTimer>

#include <QtCore/QCryptographicHash>
#include <QtCore/QtEndian>

#include <functional>

using namespace QMatrixClient;
Expand Down Expand Up @@ -424,3 +427,15 @@ void User::processEvent(const RoomMemberEvent& event, const Room* room)
updateAvatarUrl(event.avatarUrl(), d->avatarUrlForRoom(room), room);
}
}

qreal User::hueF() const
{
QByteArray hash = QCryptographicHash::hash(d->userId.toUtf8(),
QCryptographicHash::Sha1);
QDataStream dataStream(qToLittleEndian(hash).left(1));
dataStream.setByteOrder(QDataStream::LittleEndian);
quint8 hashValue;
dataStream >> hashValue;
qreal hueF = static_cast<qreal>(hashValue)/std::numeric_limits<quint8>::max()*std::numeric_limits<qreal>::max();
return hueF;
}
5 changes: 5 additions & 0 deletions lib/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace QMatrixClient
Q_OBJECT
Q_PROPERTY(QString id READ id CONSTANT)
Q_PROPERTY(bool isGuest READ isGuest CONSTANT)
Q_PROPERTY(qreal hueF READ hueF CONSTANT)
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
Q_PROPERTY(QString displayName READ displayname NOTIFY nameChanged STORED false)
Q_PROPERTY(QString fullName READ fullName NOTIFY nameChanged STORED false)
Expand Down Expand Up @@ -95,6 +96,10 @@ namespace QMatrixClient
*/
bool isGuest() const;

/** Hue color component of this user based on id.
*/
qreal hueF() const;

const Avatar& avatarObject(const Room* room = nullptr) const;
Q_INVOKABLE QImage avatar(int dimension, const Room* room = nullptr);
Q_INVOKABLE QImage avatar(int requestedWidth, int requestedHeight,
Expand Down

0 comments on commit cebca19

Please sign in to comment.