summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-12-27 12:09:28 +0000
committerMaxime Coste <frrrwww@gmail.com>2014-12-27 12:09:28 +0000
commit71bfe5498d40ff5bf7b512b3ab9e43ead8a3ce51 (patch)
treebee4770f9448e689425a0eaeb1abc22b080d5a53 /src
parent248c1bda02236cec6bd53de35ccff22ce78bdd33 (diff)
Refactor color functions
Diffstat (limited to 'src')
-rw-r--r--src/color.cc63
1 files changed, 27 insertions, 36 deletions
diff --git a/src/color.cc b/src/color.cc
index 8f1b01c1..3abfd95f 100644
--- a/src/color.cc
+++ b/src/color.cc
@@ -1,5 +1,6 @@
#include "color.hh"
+#include "containers.hh"
#include "exception.hh"
#include "regex.hh"
@@ -8,30 +9,28 @@
namespace Kakoune
{
+static constexpr const char* color_names[] = {
+ "default",
+ "black",
+ "red",
+ "green",
+ "yellow",
+ "blue",
+ "magenta",
+ "cyan",
+ "white",
+};
+
bool is_color_name(StringView color)
{
- return color == "default" or
- color == "black" or
- color == "red" or
- color == "green" or
- color == "yellow" or
- color == "blue" or
- color == "magenta" or
- color == "cyan" or
- color == "white";
+ return contains(color_names, color);
}
Color str_to_color(StringView color)
{
- if (color == "default") return Colors::Default;
- if (color == "black") return Colors::Black;
- if (color == "red") return Colors::Red;
- if (color == "green") return Colors::Green;
- if (color == "yellow") return Colors::Yellow;
- if (color == "blue") return Colors::Blue;
- if (color == "magenta") return Colors::Magenta;
- if (color == "cyan") return Colors::Cyan;
- if (color == "white") return Colors::White;
+ auto it = find_if(color_names, [&](const char* c){ return color == c; });
+ if (it != std::end(color_names))
+ return static_cast<Colors>(it - color_names);
static const Regex rgb_regex{"rgb:[0-9a-fA-F]{6}"};
if (regex_match(color.begin(), color.end(), rgb_regex))
@@ -48,26 +47,18 @@ Color str_to_color(StringView color)
String color_to_str(Color color)
{
- switch (color.color)
+ if (color.color == Colors::RGB)
+ {
+ char buffer[11];
+ sprintf(buffer, "rgb:%02x%02x%02x", color.r, color.g, color.b);
+ return buffer;
+ }
+ else
{
- case Colors::Default: return "default";
- case Colors::Black: return "black";
- case Colors::Red: return "red";
- case Colors::Green: return "green";
- case Colors::Yellow: return "yellow";
- case Colors::Blue: return "blue";
- case Colors::Magenta: return "magenta";
- case Colors::Cyan: return "cyan";
- case Colors::White: return "white";
- case Colors::RGB:
- {
- char buffer[11];
- sprintf(buffer, "rgb:%02x%02x%02x", color.r, color.g, color.b);
- return buffer;
- }
+ size_t index = static_cast<size_t>(color.color);
+ kak_assert(index < std::end(color_names) - std::begin(color_names));
+ return color_names[index];
}
- kak_assert(false);
- return "default";
}
String option_to_string(Color color)