Move Colors enum class as NamedColor enum inside Color struct

This commit is contained in:
Maxime Coste 2015-04-25 10:47:39 +01:00
parent dc46eda279
commit bbefde9379
8 changed files with 72 additions and 72 deletions

View File

@ -30,7 +30,7 @@ Color str_to_color(StringView color)
{ {
auto it = find_if(color_names, [&](const char* c){ return color == c; }); auto it = find_if(color_names, [&](const char* c){ return color == c; });
if (it != std::end(color_names)) if (it != std::end(color_names))
return static_cast<Colors>(it - color_names); return static_cast<Color::NamedColor>(it - color_names);
auto hval = [&color](char c) -> int auto hval = [&color](char c) -> int
{ {
@ -49,12 +49,12 @@ Color str_to_color(StringView color)
(unsigned char)(hval(color[8]) * 16 + hval(color[9])) }; (unsigned char)(hval(color[8]) * 16 + hval(color[9])) };
throw runtime_error(format("Unable to parse color '{}'", color)); throw runtime_error(format("Unable to parse color '{}'", color));
return Colors::Default; return Color::Default;
} }
String color_to_str(Color color) String color_to_str(Color color)
{ {
if (color.color == Colors::RGB) if (color.color == Color::RGB)
{ {
char buffer[11]; char buffer[11];
sprintf(buffer, "rgb:%02x%02x%02x", color.r, color.g, color.b); sprintf(buffer, "rgb:%02x%02x%02x", color.r, color.g, color.b);

View File

@ -9,31 +9,31 @@ namespace Kakoune
class String; class String;
class StringView; class StringView;
enum class Colors : char
{
Default,
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
RGB,
};
struct Color struct Color
{ {
Colors color; enum NamedColor : char
unsigned char r; {
unsigned char g; Default,
unsigned char b; Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
RGB,
};
constexpr Color() : Color{Colors::Default} {} NamedColor color;
constexpr Color(Colors c) : color{c}, r{0}, g{0}, b{0} {} unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 0;
constexpr Color() : Color{Default} {}
constexpr Color(NamedColor c) : color{c} {}
constexpr Color(unsigned char r, unsigned char g, unsigned char b) constexpr Color(unsigned char r, unsigned char g, unsigned char b)
: color{Colors::RGB}, r{r}, g{g}, b{b} {} : color{RGB}, r{r}, g{g}, b{b} {}
}; };
constexpr bool operator==(Color lhs, Color rhs) constexpr bool operator==(Color lhs, Color rhs)
@ -57,7 +57,7 @@ bool is_color_name(StringView color);
inline size_t hash_value(const Color& val) inline size_t hash_value(const Color& val)
{ {
return val.color == Colors::RGB ? return val.color == Color::RGB ?
hash_values(val.color, val.r, val.g, val.b) hash_values(val.color, val.r, val.g, val.b)
: hash_value(val.color); : hash_value(val.color);
} }

View File

@ -25,7 +25,7 @@ struct Face
Color bg; Color bg;
Attribute attributes; Attribute attributes;
constexpr Face(Color fg = Colors::Default, Color bg = Colors::Default, constexpr Face(Color fg = Color::Default, Color bg = Color::Default,
Attribute attributes = Attribute::Normal) Attribute attributes = Attribute::Normal)
: fg{fg}, bg{bg}, attributes{attributes} {} : fg{fg}, bg{bg}, attributes{attributes} {}
}; };

View File

@ -91,21 +91,21 @@ CandidateList FaceRegistry::complete_alias_name(StringView prefix,
FaceRegistry::FaceRegistry() FaceRegistry::FaceRegistry()
: m_aliases{ : m_aliases{
{ "PrimarySelection", Face{ Colors::White, Colors::Blue } }, { "PrimarySelection", Face{ Color::White, Color::Blue } },
{ "SecondarySelection", Face{ Colors::Black, Colors::Blue } }, { "SecondarySelection", Face{ Color::Black, Color::Blue } },
{ "PrimaryCursor", Face{ Colors::Black, Colors::White } }, { "PrimaryCursor", Face{ Color::Black, Color::White } },
{ "SecondaryCursor", Face{ Colors::Black, Colors::White } }, { "SecondaryCursor", Face{ Color::Black, Color::White } },
{ "LineNumbers", Face{ Colors::Default, Colors::Default } }, { "LineNumbers", Face{ Color::Default, Color::Default } },
{ "LineNumberCursor", Face{ Colors::Default, Colors::Default, Attribute::Reverse } }, { "LineNumberCursor", Face{ Color::Default, Color::Default, Attribute::Reverse } },
{ "MenuForeground", Face{ Colors::White, Colors::Blue } }, { "MenuForeground", Face{ Color::White, Color::Blue } },
{ "MenuBackground", Face{ Colors::Blue, Colors::White } }, { "MenuBackground", Face{ Color::Blue, Color::White } },
{ "Information", Face{ Colors::Black, Colors::Yellow } }, { "Information", Face{ Color::Black, Color::Yellow } },
{ "Error", Face{ Colors::Black, Colors::Red } }, { "Error", Face{ Color::Black, Color::Red } },
{ "StatusLine", Face{ Colors::Cyan, Colors::Default } }, { "StatusLine", Face{ Color::Cyan, Color::Default } },
{ "StatusCursor", Face{ Colors::Black, Colors::Cyan } }, { "StatusCursor", Face{ Color::Black, Color::Cyan } },
{ "Prompt", Face{ Colors::Yellow, Colors::Default } }, { "Prompt", Face{ Color::Yellow, Color::Default } },
{ "MatchingChar", Face{ Colors::Default, Colors::Default, Attribute::Bold } }, { "MatchingChar", Face{ Color::Default, Color::Default, Attribute::Bold } },
{ "Search", Face{ Colors::Default, Colors::Default, Attribute::Underline } }, { "Search", Face{ Color::Default, Color::Default, Attribute::Underline } },
} }
{} {}

View File

@ -154,9 +154,9 @@ void apply_highlighter(const Context& context,
auto apply_face = [](const Face& face) auto apply_face = [](const Face& face)
{ {
return [&face](DisplayAtom& atom) { return [&face](DisplayAtom& atom) {
if (face.fg != Colors::Default) if (face.fg != Color::Default)
atom.face.fg = face.fg; atom.face.fg = face.fg;
if (face.bg != Colors::Default) if (face.bg != Color::Default)
atom.face.bg = face.bg; atom.face.bg = face.bg;
if (face.attributes != Attribute::Normal) if (face.attributes != Attribute::Normal)
atom.face.attributes |= face.attributes; atom.face.attributes |= face.attributes;
@ -760,7 +760,7 @@ void expand_unprintable(const Context& context, HighlightFlags flags, DisplayBuf
if (next.coord() < atom_it->end()) if (next.coord() < atom_it->end())
atom_it = line.split(atom_it, next.coord()); atom_it = line.split(atom_it, next.coord());
atom_it->replace(str); atom_it->replace(str);
atom_it->face = { Colors::Red, Colors::Black }; atom_it->face = { Color::Red, Color::Black };
break; break;
} }
it = next; it = next;
@ -800,7 +800,7 @@ HighlighterAndId create_flag_lines_highlighter(HighlighterParameters params)
String content = it != lines.end() ? std::get<2>(*it) : empty; String content = it != lines.end() ? std::get<2>(*it) : empty;
content += String(' ', width - content.char_length()); content += String(' ', width - content.char_length());
DisplayAtom atom{std::move(content)}; DisplayAtom atom{std::move(content)};
atom.face = { it != lines.end() ? std::get<1>(*it) : Colors::Default , bg }; atom.face = { it != lines.end() ? std::get<1>(*it) : Color::Default , bg };
line.insert(line.begin(), std::move(atom)); line.insert(line.begin(), std::move(atom));
} }
}; };

View File

@ -225,16 +225,16 @@ public:
DisplayLine mode_line() const override DisplayLine mode_line() const override
{ {
AtomList atoms = { { to_string(context().selections().size()) + " sel", Face(Colors::Blue) } }; AtomList atoms = { { to_string(context().selections().size()) + " sel", Face(Color::Blue) } };
if (m_params.count != 0) if (m_params.count != 0)
{ {
atoms.push_back({ "; param=", Face(Colors::Yellow) }); atoms.push_back({ "; param=", Face(Color::Yellow) });
atoms.push_back({ to_string(m_params.count), Face(Colors::Green) }); atoms.push_back({ to_string(m_params.count), Face(Color::Green) });
} }
if (m_params.reg != '"') if (m_params.reg != '"')
{ {
atoms.push_back({ "; reg=", Face(Colors::Yellow) }); atoms.push_back({ "; reg=", Face(Color::Yellow) });
atoms.push_back({ StringView(m_params.reg).str(), Face(Colors::Green) }); atoms.push_back({ StringView(m_params.reg).str(), Face(Color::Green) });
} }
return atoms; return atoms;
} }
@ -515,7 +515,7 @@ public:
DisplayLine mode_line() const override DisplayLine mode_line() const override
{ {
return { "menu", Face(Colors::Yellow) }; return { "menu", Face(Color::Yellow) };
} }
KeymapMode keymap_mode() const override { return KeymapMode::Menu; } KeymapMode keymap_mode() const override { return KeymapMode::Menu; }
@ -755,7 +755,7 @@ public:
DisplayLine mode_line() const override DisplayLine mode_line() const override
{ {
return { "prompt", Face(Colors::Yellow) }; return { "prompt", Face(Color::Yellow) };
} }
KeymapMode keymap_mode() const override { return KeymapMode::Prompt; } KeymapMode keymap_mode() const override { return KeymapMode::Prompt; }
@ -842,7 +842,7 @@ public:
DisplayLine mode_line() const override DisplayLine mode_line() const override
{ {
return { "enter key", Face(Colors::Yellow) }; return { "enter key", Face(Color::Yellow) };
} }
KeymapMode keymap_mode() const override { return m_keymap_mode; } KeymapMode keymap_mode() const override { return m_keymap_mode; }
@ -1009,8 +1009,8 @@ public:
DisplayLine mode_line() const override DisplayLine mode_line() const override
{ {
auto num_sel = context().selections().size(); auto num_sel = context().selections().size();
return {AtomList{ { "insert ", Face(Colors::Green) }, return {AtomList{ { "insert ", Face(Color::Green) },
{ to_string(num_sel) + " sel", Face(Colors::Blue) } }}; { to_string(num_sel) + " sel", Face(Color::Blue) } }};
} }
KeymapMode keymap_mode() const override { return KeymapMode::Insert; } KeymapMode keymap_mode() const override { return KeymapMode::Insert; }

View File

@ -62,7 +62,7 @@ static void set_attribute(WINDOW* window, int attribute, bool on)
static bool operator<(Color lhs, Color rhs) static bool operator<(Color lhs, Color rhs)
{ {
if (lhs.color == rhs.color and lhs.color == Colors::RGB) if (lhs.color == rhs.color and lhs.color == Color::RGB)
return lhs.r == rhs.r ? (lhs.g == rhs.g ? lhs.b < rhs.b return lhs.r == rhs.r ? (lhs.g == rhs.g ? lhs.b < rhs.b
: lhs.g < rhs.g) : lhs.g < rhs.g)
: lhs.r < rhs.r; : lhs.r < rhs.r;
@ -74,15 +74,15 @@ template<typename T> T sq(T x) { return x * x; }
static int nc_color(Color color) static int nc_color(Color color)
{ {
static std::map<Color, int> colors = { static std::map<Color, int> colors = {
{ Colors::Default, -1 }, { Color::Default, -1 },
{ Colors::Black, COLOR_BLACK }, { Color::Black, COLOR_BLACK },
{ Colors::Red, COLOR_RED }, { Color::Red, COLOR_RED },
{ Colors::Green, COLOR_GREEN }, { Color::Green, COLOR_GREEN },
{ Colors::Yellow, COLOR_YELLOW }, { Color::Yellow, COLOR_YELLOW },
{ Colors::Blue, COLOR_BLUE }, { Color::Blue, COLOR_BLUE },
{ Colors::Magenta, COLOR_MAGENTA }, { Color::Magenta, COLOR_MAGENTA },
{ Colors::Cyan, COLOR_CYAN }, { Color::Cyan, COLOR_CYAN },
{ Colors::White, COLOR_WHITE }, { Color::White, COLOR_WHITE },
}; };
static int next_color = 8; static int next_color = 8;
@ -91,7 +91,7 @@ static int nc_color(Color color)
return it->second; return it->second;
else if (can_change_color() and COLORS > 8) else if (can_change_color() and COLORS > 8)
{ {
kak_assert(color.color == Colors::RGB); kak_assert(color.color == Color::RGB);
if (next_color > COLORS) if (next_color > COLORS)
next_color = 8; next_color = 8;
init_color(next_color, init_color(next_color,
@ -103,7 +103,7 @@ static int nc_color(Color color)
} }
else else
{ {
kak_assert(color.color == Colors::RGB); kak_assert(color.color == Color::RGB);
static const struct { unsigned char r, g, b; } builtin_colors[] = { static const struct { unsigned char r, g, b; } builtin_colors[] = {
{0x00,0x00,0x00}, {0x80,0x00,0x00}, {0x00,0x80,0x00}, {0x80,0x80,0x00}, {0x00,0x00,0x00}, {0x80,0x00,0x00}, {0x00,0x80,0x00}, {0x80,0x80,0x00},
{0x00,0x00,0x80}, {0x80,0x00,0x80}, {0x00,0x80,0x80}, {0xc0,0xc0,0xc0}, {0x00,0x00,0x80}, {0x80,0x00,0x80}, {0x00,0x80,0x80}, {0xc0,0xc0,0xc0},
@ -213,7 +213,7 @@ static void set_face(WINDOW* window, Face face)
if (current_pair != -1) if (current_pair != -1)
wattroff(window, COLOR_PAIR(current_pair)); wattroff(window, COLOR_PAIR(current_pair));
if (face.fg != Colors::Default or face.bg != Colors::Default) if (face.fg != Color::Default or face.bg != Color::Default)
{ {
current_pair = get_color_pair(face); current_pair = get_color_pair(face);
wattron(window, COLOR_PAIR(current_pair)); wattron(window, COLOR_PAIR(current_pair));
@ -376,7 +376,7 @@ void NCursesUI::draw(const DisplayBuffer& display_buffer,
++line_index; ++line_index;
} }
set_face(m_window, { Colors::Blue, Colors::Default }); set_face(m_window, { Color::Blue, Color::Default });
while (line_index < m_dimensions.line + (m_status_on_top ? 1 : 0)) while (line_index < m_dimensions.line + (m_status_on_top ? 1 : 0))
{ {
wmove(m_window, (int)line_index++, 0); wmove(m_window, (int)line_index++, 0);

View File

@ -95,7 +95,7 @@ public:
void write(Color color) void write(Color color)
{ {
write(color.color); write(color.color);
if (color.color == Colors::RGB) if (color.color == Color::RGB)
{ {
write(color.r); write(color.r);
write(color.g); write(color.g);
@ -188,8 +188,8 @@ template<>
Color read<Color>(int socket) Color read<Color>(int socket)
{ {
Color res; Color res;
res.color = read<Colors>(socket); res.color = read<Color::NamedColor>(socket);
if (res.color == Colors::RGB) if (res.color == Color::RGB)
{ {
res.r = read<unsigned char>(socket); res.r = read<unsigned char>(socket);
res.g = read<unsigned char>(socket); res.g = read<unsigned char>(socket);