aboutsummaryrefslogtreecommitdiff
path: root/lib/util.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2019-06-25 21:19:50 +0900
committerGitHub <noreply@github.com>2019-06-25 21:19:50 +0900
commitd3ddd394e855cfe217bf0f0d368822c9b99316bb (patch)
tree93c89f3e83291edfe88922182d4e5608a5ec62e7 /lib/util.cpp
parent93f0c8fe89f448d1d58caa757573f17102369471 (diff)
parent48ffee5fa78f44bd00d76772ad79ae4eeb6c8dc4 (diff)
downloadlibquotient-d3ddd394e855cfe217bf0f0d368822c9b99316bb.tar.gz
libquotient-d3ddd394e855cfe217bf0f0d368822c9b99316bb.zip
Merge pull request #322 from a-andreyev/aa13q-fancy-colors
Move out the logic of the hue calculation to utils
Diffstat (limited to 'lib/util.cpp')
-rw-r--r--lib/util.cpp19
1 files changed, 19 insertions, 0 deletions
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