App: Add setPackedRGB() and getPackedRGB() to Color class

This commit is contained in:
wmayer
2024-04-16 19:35:57 +02:00
parent 8d5e2bc428
commit 331bdacd83
2 changed files with 36 additions and 5 deletions

View File

@@ -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<float>((rgb >> 24) & 0xff) / 255.0F,
static_cast<float>((rgb >> 16) & 0xff) / 255.0F,
static_cast<float>((rgb >> 8) & 0xff) / 255.0F);
// clang-format on
}
uint32_t Color::getPackedRGB() const
{
// clang-format off
return (static_cast<uint32_t>(r * 255.0F + 0.5F) << 24 |
static_cast<uint32_t>(g * 255.0F + 0.5F) << 16 |
static_cast<uint32_t>(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();
}

View File

@@ -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 <typename T>
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"