aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Andreyev <aa13q@ya.ru>2019-05-15 16:36:02 +0300
committerAlexey Andreyev <aa13q@ya.ru>2019-05-23 02:12:56 +0300
commit48ffee5fa78f44bd00d76772ad79ae4eeb6c8dc4 (patch)
tree7dd9996a3e89f36a942617a740fd33c069871e1e
parentd6f39dcb0de69322479f287514a8c36afcb3fe7b (diff)
downloadlibquotient-48ffee5fa78f44bd00d76772ad79ae4eeb6c8dc4.tar.gz
libquotient-48ffee5fa78f44bd00d76772ad79ae4eeb6c8dc4.zip
Move out the logic of the hue calculation to utils
-rw-r--r--lib/user.cpp20
-rw-r--r--lib/util.cpp19
-rw-r--r--lib/util.h8
3 files changed, 28 insertions, 19 deletions
diff --git a/lib/user.cpp b/lib/user.cpp
index 7f3f11f6..7b695618 100644
--- a/lib/user.cpp
+++ b/lib/user.cpp
@@ -33,9 +33,6 @@
#include <QtCore/QStringBuilder>
#include <QtCore/QElapsedTimer>
-#include <QtCore/QCryptographicHash>
-#include <QtCore/QtEndian>
-
#include <functional>
using namespace QMatrixClient;
@@ -50,23 +47,8 @@ class User::Private
return Avatar(move(url));
}
- qreal makeHueF()
- {
- Q_ASSERT(!userId.isEmpty());
- QByteArray hash = QCryptographicHash::hash(userId.toUtf8(),
- QCryptographicHash::Sha1);
- QDataStream dataStream(qToLittleEndian(hash).left(2));
- dataStream.setByteOrder(QDataStream::LittleEndian);
- quint16 hashValue;
- dataStream >> hashValue;
- const auto hueF =
- qreal(hashValue)/std::numeric_limits<quint16>::max();
- Q_ASSERT((0 <= hueF) && (hueF <= 1));
- return hueF;
- }
-
Private(QString userId, Connection* connection)
- : userId(move(userId)), connection(connection), hueF(makeHueF())
+ : userId(move(userId)), connection(connection), hueF(stringToHueF(this->userId))
{ }
QString userId;
diff --git a/lib/util.cpp b/lib/util.cpp
index 4e17d2f9..01d4e77b 100644
--- a/lib/util.cpp
+++ b/lib/util.cpp
@@ -23,6 +23,10 @@
#include <QtCore/QDir>
#include <QtCore/QStringBuilder>
+#include <QtCore/QCryptographicHash>
+#include <QtCore/QtEndian>
+#include <QtCore/QDataStream>
+
static const auto RegExpOptions =
QRegularExpression::CaseInsensitiveOption
| QRegularExpression::OptimizeOnFirstUsageOption
@@ -93,6 +97,21 @@ QString QMatrixClient::cacheLocation(const QString& dirName)
return cachePath;
}
+qreal QMatrixClient::stringToHueF(const QString &string)
+{
+ Q_ASSERT(!string.isEmpty());
+ QByteArray hash = QCryptographicHash::hash(string.toUtf8(),
+ QCryptographicHash::Sha1);
+ QDataStream dataStream(qToLittleEndian(hash).left(2));
+ dataStream.setByteOrder(QDataStream::LittleEndian);
+ quint16 hashValue;
+ dataStream >> hashValue;
+ const auto hueF =
+ qreal(hashValue)/std::numeric_limits<quint16>::max();
+ Q_ASSERT((0 <= hueF) && (hueF <= 1));
+ return hueF;
+}
+
// Tests for function_traits<>
#ifdef Q_CC_CLANG
diff --git a/lib/util.h b/lib/util.h
index f08c1c95..eda817a1 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -314,5 +314,13 @@ namespace QMatrixClient
* \param dir path to cache directory relative to the standard cache path
*/
QString cacheLocation(const QString& dirName);
+
+ /** Hue color component of based of the hash of the string.
+ * The implementation is based on XEP-0392:
+ * https://xmpp.org/extensions/xep-0392.html
+ * Naming and range are the same as QColor's hueF method:
+ * https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
+ */
+ qreal stringToHueF(const QString& string);
} // namespace QMatrixClient