2013-03-26 02:35:59 +04:00
|
|
|
#include "color.hh"
|
|
|
|
|
|
|
|
#include "exception.hh"
|
2017-08-29 11:23:03 +03:00
|
|
|
#include "ranges.hh"
|
2017-10-09 17:12:42 +03:00
|
|
|
#include "string_utils.hh"
|
2013-03-26 02:35:59 +04:00
|
|
|
|
2014-11-13 00:27:07 +03:00
|
|
|
#include <cstdio>
|
|
|
|
|
2013-03-26 02:35:59 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-12-27 15:09:28 +03:00
|
|
|
static constexpr const char* color_names[] = {
|
|
|
|
"default",
|
|
|
|
"black",
|
|
|
|
"red",
|
|
|
|
"green",
|
|
|
|
"yellow",
|
|
|
|
"blue",
|
|
|
|
"magenta",
|
|
|
|
"cyan",
|
|
|
|
"white",
|
2017-10-25 06:08:22 +03:00
|
|
|
"bright-black",
|
|
|
|
"bright-red",
|
|
|
|
"bright-green",
|
|
|
|
"bright-yellow",
|
|
|
|
"bright-blue",
|
|
|
|
"bright-magenta",
|
|
|
|
"bright-cyan",
|
|
|
|
"bright-white",
|
2014-12-27 15:09:28 +03:00
|
|
|
};
|
|
|
|
|
2014-08-20 02:16:21 +04:00
|
|
|
bool is_color_name(StringView color)
|
|
|
|
{
|
2014-12-27 15:09:28 +03:00
|
|
|
return contains(color_names, color);
|
2014-08-20 02:16:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Color str_to_color(StringView color)
|
2013-03-26 02:35:59 +04:00
|
|
|
{
|
2014-12-27 15:09:28 +03:00
|
|
|
auto it = find_if(color_names, [&](const char* c){ return color == c; });
|
|
|
|
if (it != std::end(color_names))
|
2015-04-25 12:47:39 +03:00
|
|
|
return static_cast<Color::NamedColor>(it - color_names);
|
2013-05-07 20:52:23 +04:00
|
|
|
|
2015-04-02 23:27:41 +03:00
|
|
|
auto hval = [&color](char c) -> int
|
2013-05-07 20:52:23 +04:00
|
|
|
{
|
2015-04-02 23:27:41 +03:00
|
|
|
if (c >= 'A' and c <= 'F')
|
|
|
|
return 10 + c - 'A';
|
|
|
|
else if (c >= 'a' and c <= 'f')
|
|
|
|
return 10 + c - 'a';
|
|
|
|
else if (c >= '0' and c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
throw runtime_error(format("invalid digit '{}' in '{}'", c, color));
|
|
|
|
};
|
|
|
|
|
|
|
|
if (color.length() == 10 and color.substr(0_byte, 4_byte) == "rgb:")
|
|
|
|
return { (unsigned char)(hval(color[4]) * 16 + hval(color[5])),
|
|
|
|
(unsigned char)(hval(color[6]) * 16 + hval(color[7])),
|
|
|
|
(unsigned char)(hval(color[8]) * 16 + hval(color[9])) };
|
|
|
|
|
2018-04-06 17:56:53 +03:00
|
|
|
throw runtime_error(format("unable to parse color: '{}'", color));
|
2015-04-25 12:47:39 +03:00
|
|
|
return Color::Default;
|
2013-03-26 02:35:59 +04:00
|
|
|
}
|
|
|
|
|
2017-09-12 06:31:57 +03:00
|
|
|
String to_string(Color color)
|
2013-03-26 02:35:59 +04:00
|
|
|
{
|
2015-04-25 12:47:39 +03:00
|
|
|
if (color.color == Color::RGB)
|
2014-12-27 15:09:28 +03:00
|
|
|
{
|
|
|
|
char buffer[11];
|
|
|
|
sprintf(buffer, "rgb:%02x%02x%02x", color.r, color.g, color.b);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
else
|
2013-03-26 02:35:59 +04:00
|
|
|
{
|
2014-12-27 15:09:28 +03: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-26 02:35:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 02:26:43 +04:00
|
|
|
String option_to_string(Color color)
|
2013-03-29 22:31:06 +04:00
|
|
|
{
|
2017-09-12 06:31:57 +03:00
|
|
|
return to_string(color);
|
2013-03-29 22:31:06 +04:00
|
|
|
}
|
|
|
|
|
2018-05-27 06:00:04 +03:00
|
|
|
Color option_from_string(Meta::Type<Color>, StringView str)
|
2013-03-29 22:31:06 +04:00
|
|
|
{
|
2018-05-27 06:00:04 +03:00
|
|
|
return str_to_color(str);
|
2013-03-29 22:31:06 +04:00
|
|
|
}
|
|
|
|
|
2013-03-26 02:35:59 +04:00
|
|
|
}
|