From 331bdacd83279d4f4a8fd15537f295f0b8f3aee8 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 16 Apr 2024 19:35:57 +0200 Subject: [PATCH] App: Add setPackedRGB() and getPackedRGB() to Color class --- src/App/Color.cpp | 24 +++++++++++++++++++++--- src/App/Color.h | 17 +++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/App/Color.cpp b/src/App/Color.cpp index 17f6e24c1b..38e621d7cd 100644 --- a/src/App/Color.cpp +++ b/src/App/Color.cpp @@ -89,6 +89,24 @@ uint32_t Color::getPackedValue() const // clang-format on } +void Color::setPackedRGB(uint32_t rgb) +{ + // clang-format off + this->set(static_cast((rgb >> 24) & 0xff) / 255.0F, + static_cast((rgb >> 16) & 0xff) / 255.0F, + static_cast((rgb >> 8) & 0xff) / 255.0F); + // clang-format on +} + +uint32_t Color::getPackedRGB() const +{ + // clang-format off + return (static_cast(r * 255.0F + 0.5F) << 24 | + static_cast(g * 255.0F + 0.5F) << 16 | + static_cast(b * 255.0F + 0.5F) << 8); + // clang-format on +} + uint32_t Color::getPackedARGB() const { // clang-format off @@ -113,9 +131,9 @@ std::string Color::asHexString() const { std::stringstream ss; ss << "#" << std::hex << std::uppercase << std::setfill('0') - << std::setw(2) << int(r * 255.0F) - << std::setw(2) << int(g * 255.0F) - << std::setw(2) << int(b * 255.0F); + << std::setw(2) << int(r * 255.0F + 0.5F) + << std::setw(2) << int(g * 255.0F + 0.5F) + << std::setw(2) << int(b * 255.0F + 0.5F); return ss.str(); } diff --git a/src/App/Color.h b/src/App/Color.h index 2cff05d547..56941c6db3 100644 --- a/src/App/Color.h +++ b/src/App/Color.h @@ -79,6 +79,14 @@ public: * \sa setPackedValue(). */ uint32_t getPackedValue() const; + /** + * Returns color as a 32 bit packed unsigned int in the form 0xRRGGBB. + */ + uint32_t getPackedRGB() const; + /** + * Sets color as a 32 bit packed unsigned int in the form 0xRRGGBB. + */ + void setPackedRGB(uint32_t); /** * Returns color as a 32 bit packed unsigned int in the form 0xAARRGGBB. */ @@ -119,8 +127,13 @@ public: * */ template - inline T asValue() const { - return(T(int(r*255.0f),int(g*255.0f),int(b*255.0f))); + inline T asValue() const + { + // clang-format off + return(T(int(r * 255.0F + 0.5F), + int(g * 255.0F + 0.5F), + int(b * 255.0F + 0.5F))); + // clang-format on } /** * returns color as hex color "#RRGGBB"