diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index df7d9f4da..a26b1c1b2 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -10,6 +10,8 @@ #include #include +#include "utils.h" + using namespace Quotient; class TestRoom : public NeoChatRoom @@ -71,13 +73,6 @@ private Q_SLOTS: void linkPreviewsReject(); }; -static QColor polishColor(qreal hueF) -{ - const auto lightness = static_cast(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF(); - // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal - return QColor::fromHslF(hueF, 1, -0.7 * lightness + 0.9, 1); -} - void TextHandlerTest::initTestCase() { connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); @@ -565,7 +560,7 @@ void TextHandlerTest::receiveRichEmote() auto author = room->user(event->senderId()); const QString testInputString = QStringLiteral("This is an emote."); const QString testOutputString = QStringLiteral("* hueF()).name() + QStringLiteral("\">@example:example.org This is an emote."); + + Utils::getUserColor(author->hueF()).name() + QStringLiteral("\">@example:example.org This is an emote."); TextHandler testTextHandler; testTextHandler.setData(testInputString); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c3a229a00..7b37b1e82 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -125,6 +125,7 @@ add_library(neochat STATIC locationhelper.h events/pollevent.cpp pollhandler.cpp + utils.h ) ecm_qt_declare_logging_category(neochat diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index caec0f83b..9760e3fc2 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -46,6 +46,7 @@ #include "neochatconfig.h" #include "notificationsmanager.h" #include "texthandler.h" +#include "utils.h" #include #include @@ -408,13 +409,6 @@ QDateTime NeoChatRoom::lastActiveTime() return messageEvents().rbegin()->get()->originTimestamp(); } -static QColor polishColor(qreal hueF) -{ - const auto lightness = static_cast(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF(); - // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal - return QColor::fromHslF(hueF, 1, -0.7 * lightness + 0.9, 1); -} - QVariantList NeoChatRoom::getUsers(const QString &keyword, int limit) const { const auto userList = users(); @@ -426,7 +420,7 @@ QVariantList NeoChatRoom::getUsers(const QString &keyword, int limit) const QVariantMap userVariant{{QStringLiteral("id"), user.id()}, {QStringLiteral("displayName"), user.displayname(this)}, {QStringLiteral("avatarMediaId"), user.avatarMediaId(this)}, - {QStringLiteral("color"), polishColor(user.hueF())}}; + {QStringLiteral("color"), Utils::getUserColor(user.hueF())}}; matchedList.append(QVariant::fromValue(userVariant)); count++; @@ -467,7 +461,7 @@ QVariantMap NeoChatRoom::getUser(User *user) const {QStringLiteral("displayName"), user->displayname(this)}, {QStringLiteral("avatarSource"), avatarForMember(user)}, {QStringLiteral("avatarMediaId"), user->avatarMediaId(this)}, - {QStringLiteral("color"), polishColor(user->hueF())}, + {QStringLiteral("color"), Utils::getUserColor(user->hueF())}, {QStringLiteral("object"), QVariant::fromValue(user)}, }; } @@ -548,7 +542,7 @@ QString NeoChatRoom::eventToString(const RoomEvent &evt, Qt::TextFormat format, if (prettyPrint) { subjectName = QStringLiteral("%3") - .arg(e.userId(), polishColor(user(e.userId())->hueF()).name(), subjectName); + .arg(e.userId(), Utils::getUserColor(user(e.userId())->hueF()).name(), subjectName); } // The below code assumes senderName output in AuthorRole diff --git a/src/texthandler.cpp b/src/texthandler.cpp index 9355fd1a5..709af649e 100644 --- a/src/texthandler.cpp +++ b/src/texthandler.cpp @@ -16,6 +16,8 @@ #include +#include "utils.h" + static const QStringList allowedTags = { QStringLiteral("font"), QStringLiteral("del"), QStringLiteral("h1"), QStringLiteral("h2"), QStringLiteral("h3"), QStringLiteral("h4"), QStringLiteral("h5"), QStringLiteral("h6"), QStringLiteral("blockquote"), QStringLiteral("p"), QStringLiteral("a"), QStringLiteral("ul"), @@ -77,13 +79,6 @@ QString TextHandler::handleSendText() return outputString; } -static QColor polishColor(qreal hueF) -{ - const auto lightness = static_cast(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF(); - // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal - return QColor::fromHslF(hueF, 1, -0.7 * lightness + 0.9, 1); -} - QString TextHandler::handleRecieveRichText(Qt::TextFormat inputFormat, const NeoChatRoom *room, const Quotient::RoomEvent *event, bool stripNewlines) { m_pos = 0; @@ -145,7 +140,7 @@ QString TextHandler::handleRecieveRichText(Qt::TextFormat inputFormat, const Neo if (e->msgtype() == Quotient::MessageEventType::Emote) { auto author = room->user(e->senderId()); QString emoteString = QStringLiteral("* senderId() + QStringLiteral("\" style=\"color:") - + polishColor(author->hueF()).name() + QStringLiteral("\">") + author->displayname(room) + QStringLiteral(" "); + + Utils::getUserColor(author->hueF()).name() + QStringLiteral("\">") + author->displayname(room) + QStringLiteral(" "); if (outputString.startsWith(QStringLiteral("

"))) { outputString.insert(3, emoteString); } else { diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 000000000..1c7ddc97d --- /dev/null +++ b/src/utils.h @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: 2023 James Graham +// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + +#include +#include +#include + +namespace Utils +{ + +/** + * @brief Get a color for a user from a hueF value. + * + * The lightness of the color will be modified depending on the current palette in + * order to maintain contrast. + */ +inline QColor getUserColor(qreal hueF) +{ + const auto lightness = static_cast(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF(); + // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal + return QColor::fromHslF(hueF, 1, -0.7 * lightness + 0.9, 1); +} +}