From 8e10c046fab5334cd78226a627d5a43ce7d231c8 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sun, 27 Oct 2019 22:39:02 +0100
Subject: [PATCH] Move all channel functions into rgba class
Since we use rgba everywhere, extracting channels from uint32_t directly
is not necessary anymore.
---
include/utils/color.hpp | 6 ------
src/utils/color.cpp | 24 ++++--------------------
src/x11/tray_manager.cpp | 2 +-
tests/unit_tests/utils/color.cpp | 14 ++++++--------
4 files changed, 11 insertions(+), 35 deletions(-)
diff --git a/include/utils/color.hpp b/include/utils/color.hpp
index f1951f20..ef614bf3 100644
--- a/include/utils/color.hpp
+++ b/include/utils/color.hpp
@@ -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
diff --git a/src/utils/color.cpp b/src/utils/color.cpp
index 277a3fd7..db0cb1c5 100644
--- a/src/utils/color.cpp
+++ b/src/utils/color.cpp
@@ -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 {
diff --git a/src/x11/tray_manager.cpp b/src/x11/tray_manager.cpp
index 13a09ab9..e9627f45 100644
--- a/src/x11/tray_manager.cpp
+++ b/src/x11/tray_manager.cpp
@@ -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;
}
diff --git a/tests/unit_tests/utils/color.cpp b/tests/unit_tests/utils/color.cpp
index 5ae9a823..5f1a8437 100644
--- a/tests/unit_tests/utils/color.cpp
+++ b/tests/unit_tests/utils/color.cpp
@@ -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(rgba{"#FF111111"}));
- EXPECT_EQ(0x00FFFFFF, static_cast(rgba{"#00FFFFFF"}));
}
TEST(ColorUtil, simplify) {