diff --git a/src/App/Color.cpp b/src/App/Color.cpp index 38e621d7cd..7a6bb6fb87 100644 --- a/src/App/Color.cpp +++ b/src/App/Color.cpp @@ -82,10 +82,10 @@ Color& Color::setPackedValue(uint32_t rgba) uint32_t Color::getPackedValue() 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 | - static_cast(a * 255.0F + 0.5F)); + return (static_cast(std::lround(r * 255.0F)) << 24 | + static_cast(std::lround(g * 255.0F)) << 16 | + static_cast(std::lround(b * 255.0F)) << 8 | + static_cast(std::lround(a * 255.0F))); // clang-format on } @@ -101,19 +101,19 @@ void Color::setPackedRGB(uint32_t rgb) 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); + return (static_cast(std::lround(r * 255.0F)) << 24 | + static_cast(std::lround(g * 255.0F)) << 16 | + static_cast(std::lround(b * 255.0F)) << 8); // clang-format on } uint32_t Color::getPackedARGB() const { // clang-format off - return (static_cast(a * 255.0F + 0.5F) << 24 | - static_cast(r * 255.0F + 0.5F) << 16 | - static_cast(g * 255.0F + 0.5F) << 8 | - static_cast(b * 255.0F + 0.5F)); + return (static_cast(std::lround(a * 255.0F)) << 24 | + static_cast(std::lround(r * 255.0F)) << 16 | + static_cast(std::lround(g * 255.0F)) << 8 | + static_cast(std::lround(b * 255.0F))); // clang-format on } @@ -131,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 + 0.5F) - << std::setw(2) << int(g * 255.0F + 0.5F) - << std::setw(2) << int(b * 255.0F + 0.5F); + << std::setw(2) << int(std::lround(r * 255.0F)) + << std::setw(2) << int(std::lround(g * 255.0F)) + << std::setw(2) << int(std::lround(b * 255.0F)); return ss.str(); } diff --git a/src/App/Color.h b/src/App/Color.h index 56941c6db3..bb374ae70d 100644 --- a/src/App/Color.h +++ b/src/App/Color.h @@ -27,6 +27,7 @@ #ifdef __GNUC__ # include #endif +#include #include #include @@ -130,9 +131,9 @@ public: 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))); + return(T(int(std::lround(r * 255.0F)), + int(std::lround(g * 255.0F)), + int(std::lround(b * 255.0F)))); // clang-format on } /**