Try to restore colors to hard coded xterm palette on ncurses quit

Fixes #327
This commit is contained in:
Maxime Coste 2015-09-09 13:40:52 +01:00
parent 182cc368a0
commit a685a928f7

View File

@ -71,40 +71,7 @@ static bool operator<(Color lhs, Color rhs)
template<typename T> T sq(T x) { return x * x; }
static int nc_color(Color color)
{
static std::map<Color, int> colors = {
{ Color::Default, -1 },
{ Color::Black, COLOR_BLACK },
{ Color::Red, COLOR_RED },
{ Color::Green, COLOR_GREEN },
{ Color::Yellow, COLOR_YELLOW },
{ Color::Blue, COLOR_BLUE },
{ Color::Magenta, COLOR_MAGENTA },
{ Color::Cyan, COLOR_CYAN },
{ Color::White, COLOR_WHITE },
};
static int next_color = 8;
auto it = colors.find(color);
if (it != colors.end())
return it->second;
else if (can_change_color() and COLORS > 8)
{
kak_assert(color.color == Color::RGB);
if (next_color > COLORS)
next_color = 8;
init_color(next_color,
color.r * 1000 / 255,
color.g * 1000 / 255,
color.b * 1000 / 255);
colors[color] = next_color;
return next_color++;
}
else
{
kak_assert(color.color == Color::RGB);
static const struct { unsigned char r, g, b; } builtin_colors[] = {
constexpr struct { unsigned char r, g, b; } builtin_colors[] = {
{0x00,0x00,0x00}, {0x80,0x00,0x00}, {0x00,0x80,0x00}, {0x80,0x80,0x00},
{0x00,0x00,0x80}, {0x80,0x00,0x80}, {0x00,0x80,0x80}, {0xc0,0xc0,0xc0},
{0x80,0x80,0x80}, {0xff,0x00,0x00}, {0x00,0xff,0x00}, {0xff,0xff,0x00},
@ -169,7 +136,53 @@ static int nc_color(Color color)
{0x80,0x80,0x80}, {0x8a,0x8a,0x8a}, {0x94,0x94,0x94}, {0x9e,0x9e,0x9e},
{0xa8,0xa8,0xa8}, {0xb2,0xb2,0xb2}, {0xbc,0xbc,0xbc}, {0xc6,0xc6,0xc6},
{0xd0,0xd0,0xd0}, {0xda,0xda,0xda}, {0xe4,0xe4,0xe4}, {0xee,0xee,0xee},
};
static void restore_colors()
{
if (not can_change_color())
return;
for (size_t i = 8; i < COLORS; ++i)
{
auto& c = builtin_colors[i];
init_color(i, c.r * 1000 / 255, c.g * 1000 / 255, c.b * 1000 / 255);
}
}
static int nc_color(Color color)
{
static std::map<Color, int> colors = {
{ Color::Default, -1 },
{ Color::Black, COLOR_BLACK },
{ Color::Red, COLOR_RED },
{ Color::Green, COLOR_GREEN },
{ Color::Yellow, COLOR_YELLOW },
{ Color::Blue, COLOR_BLUE },
{ Color::Magenta, COLOR_MAGENTA },
{ Color::Cyan, COLOR_CYAN },
{ Color::White, COLOR_WHITE },
};
static int next_color = 8;
auto it = colors.find(color);
if (it != colors.end())
return it->second;
else if (can_change_color() and COLORS > 8)
{
kak_assert(color.color == Color::RGB);
if (next_color > COLORS)
next_color = 8;
init_color(next_color,
color.r * 1000 / 255,
color.g * 1000 / 255,
color.b * 1000 / 255);
colors[color] = next_color;
return next_color++;
}
else
{
kak_assert(color.color == Color::RGB);
int lowestDist = INT_MAX;
int closestCol = -1;
for (int i = 0; i < std::min(256, COLORS); ++i)
@ -277,6 +290,7 @@ NCursesUI::~NCursesUI()
puts("\033[?1004l");
puts("\033[?1002l");
endwin();
restore_colors();
signal(SIGWINCH, SIG_DFL);
signal(SIGINT, SIG_DFL);
}