diff options
Diffstat (limited to '')
-rw-r--r-- | src/styling.cc | 134 |
1 files changed, 62 insertions, 72 deletions
diff --git a/src/styling.cc b/src/styling.cc index 73e3db4..f408733 100644 --- a/src/styling.cc +++ b/src/styling.cc @@ -32,7 +32,9 @@ #include "styling.hh" #include "ansi-palette-json.h" +#include "base/from_trait.hh" #include "config.h" +#include "css-color-names-json.h" #include "fmt/format.h" #include "xterm-palette-json.h" #include "yajlpp/yajlpp.hh" @@ -53,19 +55,43 @@ static const struct json_path_container term_color_handler = { .with_children(term_color_rgb_handler), }; -static const struct json_path_container root_color_handler = { - yajlpp::property_handler("#") - .with_obj_provider<term_color, std::vector<term_color>>( - [](const yajlpp_provider_context& ypc, - std::vector<term_color>* palette) { - if (ypc.ypc_index >= palette->size()) { - palette->resize(ypc.ypc_index + 1); - } - return &((*palette)[ypc.ypc_index]); - }) - .with_children(term_color_handler), +static const typed_json_path_container<std::vector<term_color>> + root_color_handler = { + yajlpp::property_handler("#") + .with_obj_provider<term_color, std::vector<term_color>>( + [](const yajlpp_provider_context& ypc, + std::vector<term_color>* palette) { + if (ypc.ypc_index >= palette->size()) { + palette->resize(ypc.ypc_index + 1); + } + return &((*palette)[ypc.ypc_index]); + }) + .with_children(term_color_handler), +}; + +struct css_color_names { + std::map<std::string, std::string> ccn_name_to_color; +}; + +static const typed_json_path_container<css_color_names> css_color_names_handlers + = { + yajlpp::pattern_property_handler("(?<css_color_name>.*)") + .for_field(&css_color_names::ccn_name_to_color), }; +static const css_color_names& +get_css_color_names() +{ + static const intern_string_t iname + = intern_string::lookup(css_color_names_json.get_name()); + static const auto INSTANCE + = css_color_names_handlers.parser_for(iname) + .of(css_color_names_json.to_string_fragment()) + .unwrap(); + + return INSTANCE; +} + term_color_palette* xterm_colors() { @@ -84,13 +110,23 @@ ansi_colors() return &retval; } +template<> Result<rgb_color, std::string> -rgb_color::from_str(const string_fragment& sf) +from(string_fragment sf) { if (sf.empty()) { return Ok(rgb_color()); } + if (sf[0] != '#') { + const auto& css_colors = get_css_color_names(); + const auto& iter = css_colors.ccn_name_to_color.find(sf.to_string()); + + if (iter != css_colors.ccn_name_to_color.end()) { + sf = string_fragment::from_str(iter->second); + } + } + rgb_color rgb_out; if (sf[0] == '#') { @@ -139,74 +175,29 @@ rgb_color::from_str(const string_fragment& sf) sf)); } -bool -rgb_color::operator<(const rgb_color& rhs) const -{ - if (rc_r < rhs.rc_r) - return true; - if (rhs.rc_r < rc_r) - return false; - if (rc_g < rhs.rc_g) - return true; - if (rhs.rc_g < rc_g) - return false; - return rc_b < rhs.rc_b; -} - -bool -rgb_color::operator>(const rgb_color& rhs) const -{ - return rhs < *this; -} - -bool -rgb_color::operator<=(const rgb_color& rhs) const -{ - return !(rhs < *this); -} - -bool -rgb_color::operator>=(const rgb_color& rhs) const -{ - return !(*this < rhs); -} - -bool -rgb_color::operator==(const rgb_color& rhs) const -{ - return rc_r == rhs.rc_r && rc_g == rhs.rc_g && rc_b == rhs.rc_b; -} - -bool -rgb_color::operator!=(const rgb_color& rhs) const -{ - return !(rhs == *this); -} - term_color_palette::term_color_palette(const char* name, const string_fragment& json) { - yajlpp_parse_context ypc_xterm(intern_string::lookup(name), - &root_color_handler); - yajl_handle handle; - - handle = yajl_alloc(&ypc_xterm.ypc_callbacks, nullptr, &ypc_xterm); - ypc_xterm.with_ignore_unused(true) - .with_obj(this->tc_palette) - .with_handle(handle); - yajl_status st = ypc_xterm.parse(json); - ensure(st == yajl_status_ok); - st = ypc_xterm.complete_parse(); - ensure(st == yajl_status_ok); - yajl_free(handle); + intern_string_t iname = intern_string::lookup(name); + auto parse_res + = root_color_handler.parser_for(iname).with_ignore_unused(true).of( + json); + + if (parse_res.isErr()) { + log_error("failed to parse palette: %s -- %s", + name, + parse_res.unwrapErr()[0].to_attr_line().get_string().c_str()); + } + require(parse_res.isOk()); + this->tc_palette = parse_res.unwrap(); for (auto& xc : this->tc_palette) { xc.xc_lab_color = lab_color(xc.xc_color); } } short -term_color_palette::match_color(const lab_color& to_match) +term_color_palette::match_color(const lab_color& to_match) const { double lowest = 1000.0; short lowest_id = -1; @@ -230,7 +221,6 @@ term_color_palette::match_color(const lab_color& to_match) } namespace styling { - Result<color_unit, std::string> color_unit::from_str(const string_fragment& sf) { @@ -238,7 +228,7 @@ color_unit::from_str(const string_fragment& sf) return Ok(color_unit{semantic{}}); } - auto retval = TRY(rgb_color::from_str(sf)); + auto retval = TRY(from<rgb_color>(sf)); return Ok(color_unit{retval}); } |