Move all channel functions into rgba class

Since we use rgba everywhere, extracting channels from uint32_t directly
is not necessary anymore.
This commit is contained in:
patrick96 2019-10-27 22:39:02 +01:00 committed by Patrick Ziegler
parent 3655106a14
commit 8e10c046fa
4 changed files with 11 additions and 35 deletions

View File

@ -36,12 +36,6 @@ struct rgba {
};
namespace color_util {
uint8_t alpha_channel(const uint32_t value);
uint8_t red_channel(const uint32_t value);
uint8_t green_channel(const uint32_t value);
uint8_t blue_channel(const uint32_t value);
string simplify_hex(string hex);
} // namespace color_util

View File

@ -116,35 +116,19 @@ double rgba::b() const {
}
uint8_t rgba::a_int() const {
return color_util::alpha_channel(m_value);
return (m_value >> 24) & 0xFF;
}
uint8_t rgba::r_int() const {
return color_util::red_channel(m_value);
return (m_value >> 16) & 0xFF;
}
uint8_t rgba::g_int() const {
return color_util::green_channel(m_value);
return (m_value >> 8) & 0xFF;
}
uint8_t rgba::b_int() const {
return color_util::blue_channel(m_value);
}
uint8_t color_util::alpha_channel(const uint32_t value) {
return (value >> 24) & 0xFF;
}
uint8_t color_util::red_channel(const uint32_t value) {
return (value >> 16) & 0xFF;
}
uint8_t color_util::green_channel(const uint32_t value) {
return (value >> 8) & 0xFF;
}
uint8_t color_util::blue_channel(const uint32_t value) {
return value & 0xFF;
return m_value & 0xFF;
}
bool rgba::has_color() const {

View File

@ -130,7 +130,7 @@ void tray_manager::setup(const bar_settings& bar_opts) {
// Set user-defined background color
m_opts.background = conf.get(bs, "tray-background", bar_opts.background);
if (color_util::alpha_channel(m_opts.background) != 255) {
if (m_opts.background.a_int() != 255) {
m_log.trace("tray: enable transparency");
m_opts.transparent = true;
}

View File

@ -101,17 +101,15 @@ TEST(Rgba, hasColor) {
EXPECT_FALSE(v.has_color());
}
TEST(ColorUtil, rgba) {
uint32_t color{0xCC123456};
EXPECT_EQ(0xCC, color_util::alpha_channel(color));
EXPECT_EQ(0x12, color_util::red_channel(color));
EXPECT_EQ(0x34, color_util::green_channel(color));
EXPECT_EQ(0x56, color_util::blue_channel(color));
TEST(Rgba, channel) {
rgba v{0xCC123456};
EXPECT_EQ(0xCC, v.a_int());
EXPECT_EQ(0x12, v.r_int());
EXPECT_EQ(0x34, v.g_int());
EXPECT_EQ(0x56, v.b_int());
EXPECT_EQ(0xCC / 255.0, rgba{0xCC112233}.a());
EXPECT_EQ(0x99 / 255.0, rgba{0x88449933}.g());
EXPECT_EQ(0xFF111111, static_cast<uint32_t>(rgba{"#FF111111"}));
EXPECT_EQ(0x00FFFFFF, static_cast<uint32_t>(rgba{"#00FFFFFF"}));
}
TEST(ColorUtil, simplify) {