From 413c911cd15eefda158780959519181e43d8cc2f Mon Sep 17 00:00:00 2001
From: patrick96
Date: Mon, 28 Oct 2019 00:40:59 +0100
Subject: [PATCH] Move apply_alpha functionality into rgba
---
include/components/renderer.hpp | 8 ++++----
include/utils/color.hpp | 9 +++++++++
src/components/builder.cpp | 8 +++++---
src/utils/color.cpp | 11 ++++++++++-
4 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/include/components/renderer.hpp b/include/components/renderer.hpp
index 4d7e56af..8f494d94 100644
--- a/include/components/renderer.hpp
+++ b/include/components/renderer.hpp
@@ -129,10 +129,10 @@ class renderer
alignment m_align;
std::bitset<3> m_attr;
int m_font{0};
- rgba m_bg{0U};
- rgba m_fg{0U};
- rgba m_ol{0U};
- rgba m_ul{0U};
+ rgba m_bg{};
+ rgba m_fg{};
+ rgba m_ol{};
+ rgba m_ul{};
vector m_actions;
bool m_fixedcenter;
diff --git a/include/utils/color.hpp b/include/utils/color.hpp
index ef614bf3..7d18ec59 100644
--- a/include/utils/color.hpp
+++ b/include/utils/color.hpp
@@ -12,6 +12,14 @@ struct rgba {
*/
uint32_t m_value;
+ /**
+ * NONE marks this instance as invalid. If such a color is encountered, it
+ * should be treated as if no color was set.
+ *
+ * ALPHA_ONLY is used for color strings that only have an alpha channel (#AA)
+ * these kinds should be combined with another color that has RGB channels
+ * before they are used to render anything.
+ */
enum color_type { NONE, ARGB, ALPHA_ONLY } m_type{NONE};
explicit rgba();
@@ -33,6 +41,7 @@ struct rgba {
uint8_t b_int() const;
bool has_color() const;
+ void apply_alpha(rgba other);
};
namespace color_util {
diff --git a/src/components/builder.cpp b/src/components/builder.cpp
index 78fda543..e01227e3 100644
--- a/src/components/builder.cpp
+++ b/src/components/builder.cpp
@@ -261,7 +261,8 @@ void builder::font_close() {
void builder::background(rgba color) {
if (color.m_type == color.ALPHA_ONLY) {
rgba bg = m_bar.background;
- color.m_value |= bg;
+ bg.apply_alpha(color);
+ color = bg;
}
auto hex = color_util::simplify_hex(color);
@@ -282,8 +283,9 @@ void builder::background_close() {
*/
void builder::color(rgba color) {
if (color.m_type == color.ALPHA_ONLY) {
- rgba bg = m_bar.foreground;
- color.m_value |= bg;
+ rgba fg = m_bar.foreground;
+ fg.apply_alpha(color);
+ color = fg;
}
auto hex = color_util::simplify_hex(color);
diff --git a/src/utils/color.cpp b/src/utils/color.cpp
index 15d987fd..2ec28448 100644
--- a/src/utils/color.cpp
+++ b/src/utils/color.cpp
@@ -30,7 +30,7 @@ static string normalize_hex(string hex) {
}
if (hex.length() == 3) {
- // RGB -> ARGB
+ // RGB -> FRGB
hex.insert(0, 1, 'f');
}
@@ -134,6 +134,15 @@ bool rgba::has_color() const {
return m_type != NONE;
}
+/**
+ * Replaces the current alpha channel with the alpha channel of the other color
+ *
+ * Useful for ALPHA_ONLY colors
+ */
+void rgba::apply_alpha(rgba other) {
+ m_value = (m_value & 0x00FFFFFF) | (other.a_int() << 24);
+}
+
string color_util::simplify_hex(string hex) {
// convert #ffrrggbb to #rrggbb
if (hex.length() == 9 && std::toupper(hex[1]) == 'F' && std::toupper(hex[2]) == 'F') {