2013-03-25 23:35:59 +01:00
|
|
|
#include "color.hh"
|
|
|
|
|
|
|
|
#include "exception.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-08-20 00:16:21 +02:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
Color str_to_color(StringView color)
|
2013-03-25 23:35:59 +01:00
|
|
|
{
|
2013-05-07 18:52:23 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
static const Regex rgb_regex{"rgb:[0-9a-fA-F]{6}"};
|
2014-08-20 00:16:21 +02:00
|
|
|
if (boost::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
|
|
|
{
|
2013-05-07 18:52:23 +02:00
|
|
|
switch (color.color)
|
2013-03-25 23:35:59 +01:00
|
|
|
{
|
2013-05-07 18:52:23 +02:00
|
|
|
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;
|
|
|
|
}
|
2013-03-25 23:35:59 +01:00
|
|
|
}
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(false);
|
2013-03-25 23:35:59 +01:00
|
|
|
return "default";
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|