From 529ef64257102f32f042e043da1877d74c8b1caf Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 20 Aug 2019 19:03:20 +0300 Subject: [PATCH] Sort colors by hue distance. --- .../window/themes/window_theme_editor.cpp | 18 ++++++++++++++++-- .../themes/window_theme_editor_block.cpp | 14 ++++++++++++++ .../window/themes/window_theme_editor_block.h | 2 ++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Telegram/SourceFiles/window/themes/window_theme_editor.cpp b/Telegram/SourceFiles/window/themes/window_theme_editor.cpp index 02fa2dfe66..ea256af1b8 100644 --- a/Telegram/SourceFiles/window/themes/window_theme_editor.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme_editor.cpp @@ -234,6 +234,8 @@ private: } void applyEditing(const QString &name, const QString ©Of, QColor value); + void sortByAccentDistance(); + EditorBlock::Context _context; QString _path; @@ -332,6 +334,11 @@ Fn Editor::Inner::exportCallback() { } void Editor::Inner::filterRows(const QString &query) { + if (query == ":sort-by-accent-distance") { + sortByAccentDistance(); + filterRows(QString()); + return; + } _existingRows->filterRows(query); _newRows->filterRows(query); } @@ -416,8 +423,8 @@ bool Editor::Inner::readData() { return false; } - auto rows = style::main_palette::data(); - for_const (auto &row, rows) { + const auto rows = style::main_palette::data(); + for (const auto &row : rows) { auto name = bytesToUtf8(row.name); auto description = bytesToUtf8(row.description); if (!_existingRows->feedDescription(name, description)) { @@ -442,9 +449,16 @@ bool Editor::Inner::readData() { } } } + return true; } +void Editor::Inner::sortByAccentDistance() { + const auto accent = *_existingRows->find("windowBgActive"); + _existingRows->sortByDistance(accent); + _newRows->sortByDistance(accent); +} + bool Editor::Inner::readExistingRows() { QFile f(_path); if (!f.open(QIODevice::ReadOnly)) { diff --git a/Telegram/SourceFiles/window/themes/window_theme_editor_block.cpp b/Telegram/SourceFiles/window/themes/window_theme_editor_block.cpp index c9cc016008..e38b4e0cc1 100644 --- a/Telegram/SourceFiles/window/themes/window_theme_editor_block.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme_editor_block.cpp @@ -394,6 +394,20 @@ bool EditorBlock::feedDescription(const QString &name, const QString &descriptio return false; } +void EditorBlock::sortByDistance(const QColor &to) { + auto toHue = qreal(); + auto toSaturation = qreal(); + auto toLightness = qreal(); + to.getHsvF(&toHue, &toSaturation, &toLightness); + ranges::sort(_data, ranges::less(), [&](const Row &row) { + auto fromHue = qreal(); + auto fromSaturation = qreal(); + auto fromLightness = qreal(); + row.value().getHsvF(&fromHue, &fromSaturation, &fromLightness); + return (toSaturation > 0.01) ? std::abs(fromHue - toHue) : 1.; + }); +} + template void EditorBlock::enumerateRows(Callback callback) { if (isSearch()) { diff --git a/Telegram/SourceFiles/window/themes/window_theme_editor_block.h b/Telegram/SourceFiles/window/themes/window_theme_editor_block.h index 45ad9df51f..aef178dcc2 100644 --- a/Telegram/SourceFiles/window/themes/window_theme_editor_block.h +++ b/Telegram/SourceFiles/window/themes/window_theme_editor_block.h @@ -72,6 +72,8 @@ public: bool feedDescription(const QString &name, const QString &description); + void sortByDistance(const QColor &to); + protected: void paintEvent(QPaintEvent *e) override; int resizeGetHeight(int newWidth) override;