diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2014-11-09 16:42:35 -0800 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2014-11-09 16:42:35 -0800 |
commit | e83441395e726a01abc8df95ac996d5b4bc2f364 (patch) | |
tree | 0c86dbb37b03a245d81ce77f36cd4545027cf3de /output.cpp | |
parent | 43d23ee56e76f07451f725d915f243808ab3784a (diff) |
Make set_color take multiple colors, and choose the best supported one
As suggested in #1323
Diffstat (limited to 'output.cpp')
-rw-r--r-- | output.cpp | 65 |
1 files changed, 41 insertions, 24 deletions
@@ -488,6 +488,45 @@ void writestr(const wchar_t *str) delete[] buffer; } +rgb_color_t best_color(const std::vector<rgb_color_t> &candidates, color_support_t support) +{ + if (candidates.empty()) + { + return rgb_color_t::none(); + } + + rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none(); + for (size_t i=0; i < candidates.size(); i++) + { + const rgb_color_t &color = candidates.at(i); + if (first_rgb.is_none() && color.is_rgb()) + { + first_rgb = color; + } + if (first_named.is_none() && color.is_named()) + { + first_named = color; + } + } + // If we have both RGB and named colors, then prefer rgb if term256 is supported + rgb_color_t result = rgb_color_t::none(); + bool has_term256 = !! (support & color_support_term256); + if ((!first_rgb.is_none() && has_term256) || first_named.is_none()) + { + result = first_rgb; + } + else + { + result = first_named; + } + if (result.is_none()) + { + result = candidates.at(0); + } + return result; +} + +/* This code should be refactored to enable sharing with builtin_set_color */ rgb_color_t parse_color(const wcstring &val, bool is_background) { int is_bold=0; @@ -530,30 +569,8 @@ rgb_color_t parse_color(const wcstring &val, bool is_background) } } } - - // Pick the best candidate - rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none(); - for (size_t i=0; i < candidates.size(); i++) - { - const rgb_color_t &color = candidates.at(i); - if (color.is_rgb() && first_rgb.is_none()) - first_rgb = color; - if (color.is_named() && first_named.is_none()) - first_named = color; - } - - // If we have both RGB and named colors, then prefer rgb if term256 is supported - rgb_color_t result; - bool has_term256 = !! (output_get_color_support() & color_support_term256); - if ((!first_rgb.is_none() && has_term256) || first_named.is_none()) - { - result = first_rgb; - } - else - { - result = first_named; - } - + rgb_color_t result = best_color(candidates, output_get_color_support()); + if (result.is_none()) result = rgb_color_t::normal(); |