2019-10-27 21:06:25 +00:00
|
|
|
#include "utils/color.hpp"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a hex string as input and brings it into a normalized form
|
|
|
|
*
|
|
|
|
* The input can either be only an alpha channel #AA
|
|
|
|
* or any of these forms: #RGB, #ARGB, #RRGGBB, #AARRGGBB
|
|
|
|
*
|
|
|
|
* Colors without alpha channel will get an alpha channel of FF
|
|
|
|
* The input does not have to start with '#'
|
|
|
|
*
|
|
|
|
* \returns Empty string for malformed input, either AA for the alpha only
|
|
|
|
* input or an 8 character string of the expanded form AARRGGBB
|
|
|
|
*/
|
|
|
|
static string normalize_hex(string hex) {
|
|
|
|
if (hex.length() == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// We remove the hash because it makes processing easier
|
|
|
|
if (hex[0] == '#') {
|
|
|
|
hex.erase(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hex.length() == 2) {
|
|
|
|
// We only have an alpha channel
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hex.length() == 3) {
|
2019-10-27 23:40:59 +00:00
|
|
|
// RGB -> FRGB
|
2019-10-27 21:06:25 +00:00
|
|
|
hex.insert(0, 1, 'f');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hex.length() == 4) {
|
|
|
|
// ARGB -> AARRGGBB
|
|
|
|
hex = {hex[0], hex[0], hex[1], hex[1], hex[2], hex[2], hex[3], hex[3]};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hex.length() == 6) {
|
|
|
|
// RRGGBB -> FFRRGGBB
|
|
|
|
hex.insert(0, 2, 'f');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hex.length() != 8) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
rgba::rgba() : m_value(0), m_type(NONE) {}
|
|
|
|
rgba::rgba(uint32_t value, color_type type) : m_value(value), m_type(type) {}
|
|
|
|
rgba::rgba(string hex) {
|
|
|
|
hex = normalize_hex(hex);
|
|
|
|
|
|
|
|
if (hex.length() == 0) {
|
|
|
|
m_value = 0;
|
|
|
|
m_type = NONE;
|
|
|
|
} else if (hex.length() == 2) {
|
|
|
|
m_value = std::strtoul(hex.c_str(), nullptr, 16) << 24;
|
|
|
|
m_type = ALPHA_ONLY;
|
|
|
|
} else {
|
|
|
|
m_value = std::strtoul(hex.c_str(), nullptr, 16);
|
|
|
|
m_type = ARGB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rgba::operator string() const {
|
|
|
|
char s[10];
|
2020-11-27 19:46:04 +00:00
|
|
|
size_t len = snprintf(s, 10, "#%08x", m_value);
|
2019-10-27 21:06:25 +00:00
|
|
|
return string(s, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rgba::operator==(const rgba& other) const {
|
|
|
|
if (m_type != other.m_type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (m_type) {
|
|
|
|
case NONE:
|
|
|
|
return true;
|
|
|
|
case ARGB:
|
|
|
|
return m_value == other.m_value;
|
|
|
|
case ALPHA_ONLY:
|
|
|
|
return a_int() == other.a_int();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rgba::operator uint32_t() const {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:35:31 +00:00
|
|
|
uint32_t rgba::value() const {
|
|
|
|
return this->m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
rgba::color_type rgba::type() const {
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:06:25 +00:00
|
|
|
double rgba::a() const {
|
|
|
|
return a_int() / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double rgba::r() const {
|
|
|
|
return r_int() / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double rgba::g() const {
|
|
|
|
return g_int() / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double rgba::b() const {
|
|
|
|
return b_int() / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t rgba::a_int() const {
|
2019-10-27 21:39:02 +00:00
|
|
|
return (m_value >> 24) & 0xFF;
|
2019-10-27 21:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t rgba::r_int() const {
|
2019-10-27 21:39:02 +00:00
|
|
|
return (m_value >> 16) & 0xFF;
|
2019-10-27 21:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t rgba::g_int() const {
|
2019-10-27 21:39:02 +00:00
|
|
|
return (m_value >> 8) & 0xFF;
|
2019-10-27 21:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t rgba::b_int() const {
|
2019-10-27 21:39:02 +00:00
|
|
|
return m_value & 0xFF;
|
2019-10-27 21:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool rgba::has_color() const {
|
|
|
|
return m_type != NONE;
|
|
|
|
}
|
|
|
|
|
2019-10-27 23:40:59 +00:00
|
|
|
/**
|
|
|
|
* Replaces the current alpha channel with the alpha channel of the other color
|
|
|
|
*
|
|
|
|
* Useful for ALPHA_ONLY colors
|
|
|
|
*/
|
2020-11-27 19:35:31 +00:00
|
|
|
rgba rgba::apply_alpha(rgba other) const {
|
|
|
|
uint32_t val = (m_value & 0x00FFFFFF) | (((uint32_t)other.a_int()) << 24);
|
|
|
|
return rgba(val, m_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls apply_alpha, if the given color is ALPHA_ONLY.
|
|
|
|
*/
|
|
|
|
rgba rgba::try_apply_alpha(rgba other) const {
|
|
|
|
if (other.type() == ALPHA_ONLY) {
|
|
|
|
return apply_alpha(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
2019-10-27 23:40:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:06:25 +00:00
|
|
|
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') {
|
|
|
|
hex.erase(1, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert #rrggbb to #rgb
|
|
|
|
if (hex.length() == 7) {
|
|
|
|
if (hex[1] == hex[2] && hex[3] == hex[4] && hex[5] == hex[6]) {
|
|
|
|
hex = {'#', hex[1], hex[3], hex[5]};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|