From e34fd72956472cc3a147e8babffce2ea797e9f72 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 29 Nov 2024 07:52:55 +0100 Subject: [PATCH] App: Extend/change color API: * Also set/get alpha value in setValue/asValue * Add new static method fromValue --- src/App/Color.h | 20 ++++++++++++++++++-- src/Gui/TaskElementColors.cpp | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/App/Color.h b/src/App/Color.h index c03a4fc11f..c0241d1329 100644 --- a/src/App/Color.h +++ b/src/App/Color.h @@ -32,6 +32,7 @@ #include +// NOLINTBEGIN(readability-magic-numbers) namespace App { @@ -54,6 +55,10 @@ struct color_traits { return static_cast(ct.blueF()); } + float alphaF() const + { + return static_cast(ct.alphaF()); + } int red() const { return ct.red(); @@ -180,7 +185,7 @@ public: void setValue(const T& q) { color_traits ct {q}; - set(ct.redF(), ct.greenF(), ct.blueF()); + set(ct.redF(), ct.greenF(), ct.blueF(), ct.alphaF()); } /** * returns a template type e.g. Qt color equivalent to FC color @@ -192,9 +197,19 @@ public: // clang-format off return color_traits::makeColor(int(std::lround(r * 255.0F)), int(std::lround(g * 255.0F)), - int(std::lround(b * 255.0F))); + int(std::lround(b * 255.0F)), + int(std::lround(a * 255.0F))); // clang-format on } + /** + * creates FC Color from template type, e.g. Qt QColor + */ + template + static Color fromValue(const T& q) + { + color_traits ct {q}; + return Color(ct.redF(), ct.greenF(), ct.blueF(), ct.alphaF()); + } /** * returns color as hex color "#RRGGBB" * @@ -212,5 +227,6 @@ public: }; } // namespace App +// NOLINTEND(readability-magic-numbers) #endif // APP_COLOR_H diff --git a/src/Gui/TaskElementColors.cpp b/src/Gui/TaskElementColors.cpp index 0ccd998e20..539ad997a8 100644 --- a/src/Gui/TaskElementColors.cpp +++ b/src/Gui/TaskElementColors.cpp @@ -196,7 +196,7 @@ public: auto item = ui->elementList->item(i); auto col = item->data(Qt::UserRole).value(); std::string sub = qPrintable(item->data(Qt::UserRole + 1).value()); - info.emplace(sub, App::Color(col.redF(), col.greenF(), col.blueF(), col.alphaF())); + info.emplace(sub, App::Color::fromValue(col)); } if (!App::GetApplication().getActiveTransaction()) { App::GetApplication().setActiveTransaction("Set colors");