Move apply_alpha functionality into rgba

This commit is contained in:
patrick96 2019-10-28 00:40:59 +01:00 committed by Patrick Ziegler
parent eeab4f0d45
commit 413c911cd1
4 changed files with 28 additions and 8 deletions

View File

@ -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<action_block> m_actions;
bool m_fixedcenter;

View File

@ -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 {

View File

@ -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);

View File

@ -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') {