fix(color_util): Parsing

This commit is contained in:
Michael Carlberg
2017-01-27 13:46:27 +01:00
parent af5f129b76
commit beedc5ab84
5 changed files with 61 additions and 26 deletions

View File

@ -172,9 +172,9 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
}
}
const auto parse_or_throw = [&](string key, string def) {
const auto parse_or_throw = [&](string key, unsigned int def) -> unsigned int {
try {
return color_util::parse(m_conf.get(bs, key, def));
return m_conf.get(bs, key, rgba{def});
} catch (const exception& err) {
throw application_error(sstream() << "Failed to set " << key << " (reason: " << err.what() << ")");
}
@ -192,14 +192,14 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
m_log.warn("Ignoring `%s.background` (overridden by gradient background)", bs);
}
} else {
m_opts.background = parse_or_throw("background", color_util::hex<uint16_t>(m_opts.background));
m_opts.background = parse_or_throw("background", m_opts.background);
}
// Load foreground
m_opts.foreground = parse_or_throw("foreground", color_util::hex<uint16_t>(m_opts.foreground));
m_opts.foreground = parse_or_throw("foreground", m_opts.foreground);
// Load over-/underline
auto line_color = m_conf.get(bs, "line-color", "#ffff0000"s);
auto line_color = m_conf.get(bs, "line-color", rgba{0xFFFF0000});
auto line_size = m_conf.get(bs, "line-size", 0);
m_opts.overline.size = m_conf.get(bs, "overline-size", line_size);
@ -208,7 +208,7 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
m_opts.underline.color = parse_or_throw("underline-color", line_color);
// Load border settings
auto border_color = m_conf.get(bs, "border-color", "#00000000"s);
auto border_color = m_conf.get(bs, "border-color", rgba{0x00000000});
auto border_size = m_conf.get(bs, "border-size", 0);
m_opts.borders.emplace(edge::TOP, border_settings{});

View File

@ -315,14 +315,7 @@ chrono::duration<double> config::convert(string&& value) const {
template <>
rgba config::convert(string&& value) const {
auto color = color_util::parse(value, 0);
// clang-format off
return rgba{
color_util::red_channel<unsigned char>(color) / 255.0,
color_util::green_channel<unsigned char>(color) / 255.0,
color_util::blue_channel<unsigned char>(color) / 255.0,
color_util::alpha_channel<unsigned char>(color) / 255.0};
// clang-format on
return rgba{color_util::parse(value, 0)};
}
template <>

View File

@ -77,7 +77,7 @@ void parser::codeblock(string&& data, const bar_settings& bar) {
switch (tag) {
case 'B':
m_sig.emit(change_background{parse_color(value, bar.background)});
m_sig.emit(change_background{parse_color(value, 0)});
break;
case 'F':
@ -181,11 +181,10 @@ size_t parser::text(string&& data) {
* Process color hex string and convert it to the correct value
*/
unsigned int parser::parse_color(const string& s, unsigned int fallback) {
unsigned int color{0};
if (s.empty() || s[0] == '-' || (color = color_util::parse(s, fallback)) == fallback) {
return fallback;
if (!s.empty() && s[0] != '-') {
return color_util::parse(s, fallback);
}
return color_util::premultiply_alpha(color);
return fallback;
}
/**