2013-03-25 23:35:59 +01:00
|
|
|
#include "color.hh"
|
|
|
|
|
2014-12-27 13:09:28 +01:00
|
|
|
#include "containers.hh"
|
2013-03-25 23:35:59 +01:00
|
|
|
#include "exception.hh"
|
2014-10-13 14:12:33 +02:00
|
|
|
#include "regex.hh"
|
2013-03-25 23:35:59 +01:00
|
|
|
|
2014-11-12 22:27:07 +01:00
|
|
|
#include <cstdio>
|
|
|
|
|
2013-03-25 23:35:59 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-12-27 13:09:28 +01:00
|
|
|
static constexpr const char* color_names[] = {
|
|
|
|
"default",
|
|
|
|
"black",
|
|
|
|
"red",
|
|
|
|
"green",
|
|
|
|
"yellow",
|
|
|
|
"blue",
|
|
|
|
"magenta",
|
|
|
|
"cyan",
|
|
|
|
"white",
|
|
|
|
};
|
|
|
|
|
2014-08-20 00:16:21 +02:00
|
|
|
bool is_color_name(StringView color)
|
|
|
|
{
|
2014-12-27 13:09:28 +01:00
|
|
|
return contains(color_names, color);
|
2014-08-20 00:16:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Color str_to_color(StringView color)
|
2013-03-25 23:35:59 +01:00
|
|
|
{
|
2014-12-27 13:09:28 +01:00
|
|
|
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);
|
2013-05-07 18:52:23 +02:00
|
|
|
|
|
|
|
static const Regex rgb_regex{"rgb:[0-9a-fA-F]{6}"};
|
2014-10-13 14:12:33 +02:00
|
|
|
if (regex_match(color.begin(), color.end(), rgb_regex))
|
2013-05-07 18:52:23 +02:00
|
|
|
{
|
2013-06-18 22:11:44 +02:00
|
|
|
unsigned l;
|
2014-08-20 00:16:21 +02:00
|
|
|
sscanf(color.zstr() + 4, "%x", &l);
|
2013-05-07 18:52:23 +02:00
|
|
|
return { (unsigned char)((l >> 16) & 0xFF),
|
|
|
|
(unsigned char)((l >> 8) & 0xFF),
|
|
|
|
(unsigned char)(l & 0xFF) };
|
|
|
|
}
|
2013-03-25 23:35:59 +01:00
|
|
|
throw runtime_error("Unable to parse color '" + color + "'");
|
2013-05-07 18:52:23 +02:00
|
|
|
return Colors::Default;
|
2013-03-25 23:35:59 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 00:26:43 +02:00
|
|
|
String color_to_str(Color color)
|
2013-03-25 23:35:59 +01:00
|
|
|
{
|
2014-12-27 13:09:28 +01:00
|
|
|
if (color.color == Colors::RGB)
|
|
|
|
{
|
|
|
|
char buffer[11];
|
|
|
|
sprintf(buffer, "rgb:%02x%02x%02x", color.r, color.g, color.b);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
else
|
2013-03-25 23:35:59 +01:00
|
|
|
{
|
2014-12-27 13:09:28 +01:00
|
|
|
size_t index = static_cast<size_t>(color.color);
|
|
|
|
kak_assert(index < std::end(color_names) - std::begin(color_names));
|
|
|
|
return color_names[index];
|
2013-03-25 23:35:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 00:26:43 +02:00
|
|
|
String option_to_string(Color color)
|
2013-03-29 19:31:06 +01:00
|
|
|
{
|
|
|
|
return color_to_str(color);
|
|
|
|
}
|
|
|
|
|
2014-08-20 00:16:21 +02:00
|
|
|
void option_from_string(StringView str, Color& color)
|
2013-03-29 19:31:06 +01:00
|
|
|
{
|
|
|
|
color = str_to_color(str);
|
|
|
|
}
|
|
|
|
|
2013-03-25 23:35:59 +01:00
|
|
|
}
|