Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit 7ad7934

Browse files
committed
Implement XEP-0392: Consistent Color Generation
1 parent f83289e commit 7ad7934

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

doc/doap.xml

+8
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ SPDX-License-Identifier: CC0-1.0
611611
<xmpp:since>1.7</xmpp:since>
612612
</xmpp:SupportedXep>
613613
</implements>
614+
<implements>
615+
<xmpp:SupportedXep>
616+
<xmpp:xep rdf:resource='https://xmpp.org/extensions/xep-0392.html'/>
617+
<xmpp:status>complete</xmpp:status>
618+
<xmpp:version>1.0.0</xmpp:version>
619+
<xmpp:since>1.10</xmpp:since>
620+
</xmpp:SupportedXep>
621+
</implements>
614622
<implements>
615623
<xmpp:SupportedXep>
616624
<xmpp:xep rdf:resource='https://xmpp.org/extensions/xep-0405.html'/>

src/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(INSTALL_HEADER_FILES
1919
base/QXmppBitsOfBinaryIq.h
2020
base/QXmppBookmarkSet.h
2121
base/QXmppByteStreamIq.h
22+
base/QXmppColorGeneration.h
2223
base/QXmppDataForm.h
2324
base/QXmppDataFormBase.h
2425
base/QXmppDiscoveryIq.h
@@ -174,6 +175,7 @@ set(SOURCE_FILES
174175
base/QXmppBitsOfBinaryIq.cpp
175176
base/QXmppBookmarkSet.cpp
176177
base/QXmppByteStreamIq.cpp
178+
base/QXmppColorGeneration.cpp
177179
base/QXmppDataForm.cpp
178180
base/QXmppDataFormBase.cpp
179181
base/QXmppDiscoveryIq.cpp
@@ -237,6 +239,7 @@ set(SOURCE_FILES
237239
base/QXmppVCardIq.cpp
238240
base/QXmppVersionIq.cpp
239241
base/compat/removed_api.cpp
242+
base/hsluv/hsluv.c
240243
# to trigger MOC
241244
base/XmppSocket.h
242245

src/base/QXmppColorGeneration.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-FileCopyrightText: 2019 Linus Jahn <[email protected]>
2+
//
3+
// SPDX-License-Identifier: LGPL-2.1-or-later
4+
5+
#include "QXmppColorGeneration.h"
6+
7+
#include "hsluv/hsluv.h"
8+
9+
#include <QCryptographicHash>
10+
11+
QXmppColorGeneration::Rgb QXmppColorGeneration::generateRgb(QStringView str)
12+
{
13+
QByteArray input = str.toUtf8();
14+
15+
// hash input through SHA-1
16+
auto hashValue = QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Sha1);
17+
18+
// the first two bytes are used to calculate the angle/hue
19+
int angle = hashValue.at(0) + hashValue.at(1) * 256;
20+
21+
double hue = double(angle) / 65536.0 * 360.0;
22+
double saturation = 100.0;
23+
double lightness = 50.0;
24+
25+
// convert to rgb values (values are between 0.0 and 1.0)
26+
double red, green, blue = 0.0;
27+
hsluv2rgb(hue, saturation, lightness, &red, &green, &blue);
28+
29+
return Rgb { quint8(red * 255.0), quint8(green * 255.0), quint8(blue * 255.0) };
30+
}

src/base/QXmppColorGeneration.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-FileCopyrightText: 2019 Linus Jahn <[email protected]>
2+
//
3+
// SPDX-License-Identifier: LGPL-2.1-or-later
4+
5+
#ifndef QXMPPCOLORGENERATION_H
6+
#define QXMPPCOLORGENERATION_H
7+
8+
#include "QXmppGlobal.h"
9+
10+
#ifdef QT_GUI_LIB
11+
#include <QColor>
12+
#endif
13+
14+
class QXmppColorGeneration
15+
{
16+
public:
17+
struct Rgb {
18+
quint8 red;
19+
quint8 green;
20+
quint8 blue;
21+
};
22+
23+
static Rgb generateRgb(QStringView str);
24+
25+
#ifdef QT_GUI_LIB
26+
static QColor generateColor(QStringView str)
27+
{
28+
auto rgb = generateRgb(str);
29+
return QColor(rgb.red, rgb.green, rgb.blue);
30+
}
31+
#endif
32+
};
33+
34+
#endif // QXMPPCOLORGENERATION_H

0 commit comments

Comments
 (0)