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

Implement XEP-0392: Consisten Color Generation #672

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions LICENSES/MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
MIT License

Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions doc/doap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,14 @@ SPDX-License-Identifier: CC0-1.0
<xmpp:since>1.7</xmpp:since>
</xmpp:SupportedXep>
</implements>
<implements>
<xmpp:SupportedXep>
<xmpp:xep rdf:resource='https://xmpp.org/extensions/xep-0392.html'/>
<xmpp:status>complete</xmpp:status>
<xmpp:version>1.0.0</xmpp:version>
<xmpp:since>1.10</xmpp:since>
</xmpp:SupportedXep>
</implements>
<implements>
<xmpp:SupportedXep>
<xmpp:xep rdf:resource='https://xmpp.org/extensions/xep-0405.html'/>
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(INSTALL_HEADER_FILES
base/QXmppBitsOfBinaryIq.h
base/QXmppBookmarkSet.h
base/QXmppByteStreamIq.h
base/QXmppColorGeneration.h
base/QXmppDataForm.h
base/QXmppDataFormBase.h
base/QXmppDiscoveryIq.h
Expand Down Expand Up @@ -174,6 +175,7 @@ set(SOURCE_FILES
base/QXmppBitsOfBinaryIq.cpp
base/QXmppBookmarkSet.cpp
base/QXmppByteStreamIq.cpp
base/QXmppColorGeneration.cpp
base/QXmppDataForm.cpp
base/QXmppDataFormBase.cpp
base/QXmppDiscoveryIq.cpp
Expand Down Expand Up @@ -237,6 +239,7 @@ set(SOURCE_FILES
base/QXmppVCardIq.cpp
base/QXmppVersionIq.cpp
base/compat/removed_api.cpp
base/hsluv/hsluv.c
# to trigger MOC
base/XmppSocket.h

Expand Down
60 changes: 60 additions & 0 deletions src/base/QXmppColorGeneration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-FileCopyrightText: 2019 Linus Jahn <[email protected]>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "QXmppColorGeneration.h"

#include "hsluv/hsluv.h"

#include <QCryptographicHash>

using namespace QXmpp::Private;

///
/// \class QXmppColorGeneration
///
/// \brief Generates colors from strings as defined in \xep{0392, Consistent Color Generation}.
///
/// This way the colors are unique between different clients.
///
/// \since QXmpp 1.10
///

///
/// Generates an RGB color triple for a string.
///
QXmppColorGeneration::Rgb QXmppColorGeneration::generateRgb(QStringView str)
{
double hue = generateColorAngle(str);
double saturation = 100.0;
double lightness = 50.0;

// convert to rgb values (values are between 0.0 and 1.0)
double red, green, blue = 0.0;
hsluv2rgb(hue, saturation, lightness, &red, &green, &blue);

return Rgb { quint8(red * 255.0), quint8(green * 255.0), quint8(blue * 255.0) };
}

///
/// \fn QXmppColorGeneration::generateColor
///
/// Generates a QColor for a string.
///
/// \note This is a header-only function and it is only available when building the application
/// using QXmpp with the Qt GUI module.
///

double QXmpp::Private::generateColorAngle(QStringView str)
{
QByteArray input = str.toUtf8();

// hash input through SHA-1
auto hashValue = QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Sha1);

// the first two bytes are used to calculate the angle/hue
auto hashData = reinterpret_cast<quint8 *>(hashValue.data());
int angle = hashData[0] + hashData[1] * 256;

return double(angle) / 65536.0 * 360.0;
}
38 changes: 38 additions & 0 deletions src/base/QXmppColorGeneration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2019 Linus Jahn <[email protected]>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#ifndef QXMPPCOLORGENERATION_H
#define QXMPPCOLORGENERATION_H

#include "QXmppGlobal.h"

#ifdef QT_GUI_LIB
#include <QColor>
#endif

class QXMPP_EXPORT QXmppColorGeneration
{
public:
struct Rgb {
quint8 red;
quint8 green;
quint8 blue;
};

static Rgb generateRgb(QStringView str);

#if defined(QT_GUI_LIB) || defined(QXMPP_DOC)
static QColor generateColor(QStringView str)
{
auto rgb = generateRgb(str);
return QColor(rgb.red, rgb.green, rgb.blue);
}
#endif
};

namespace QXmpp::Private {
double generateColorAngle(QStringView str);
}

#endif // QXMPPCOLORGENERATION_H
Loading
Loading