我试图将文本块的背景设置为红色,通过将COLOR_BLUE的值设置为rgb值:(1000, 0, 0)。如果我使用Gnome的“终端”应用程序,则背景正确(红色),但如果我使用KDE的“Konsole”,则不正确(蓝色)。这是为什么?以下代码将在Linux上编译,编译行如下:
g++ filename.cpp -lcurses
我通过以下方式将我的TERM类型设置为xterm-256color:
export TERM=xterm-256color
这里是代码:
#include <ncurses.h>
#include <cassert>
#include <csignal>
static bool stop = false;
void sigAbortHandler(int _sig)
{
stop = true;
}
int main(int _argc, char **_argv)
{
signal(SIGABRT, &sigAbortHandler);
WINDOW *window = initscr();
if (!has_colors())
{
delwin(window);
endwin();
perror("You must enable colors in your console");
}
if (!can_change_color())
{
delwin(window);
endwin();
perror("Error: unable to change colors, "
"trying setting your TERM type to enable colors");
}
assert(start_color() == OK);
keypad(stdscr, TRUE);
cbreak();
noecho();
curs_set(0);
nodelay(window, true);
int background = COLOR_BLUE;
assert(init_color(background, 1000, 0, 0) == OK);
int foreground = 2;
assert(init_color(foreground, 0, 0, 0) == OK);
int pair = 1;
assert(init_pair(pair, foreground, background) == OK);
assert(wattron(window, COLOR_PAIR(pair)) == OK);
short r, g, b;
color_content(background, &r, &g, &b);
assert(mvwprintw(window, 10, 10, "color content: %d, %d, %d", r, g, b) == OK);
assert(wrefresh(window) == OK);
assert(wattroff(window, COLOR_PAIR(pair)) == OK);
while (!stop)
{
}
delwin(window);
endwin();
return 0;
}