我正在尝试通过将值 COLOR_BLUE 设置为以下 rgb 值来将文本块的背景设置为红色:(1000, 0, 0)。如果我使用 Gnome 的“终端”应用程序,背景是正确的(红色),但如果我使用 KDE 的“Konsole”,则背景不正确(蓝色)。为什么是这样?以下代码将在 Linux 上使用 compile 行进行编译:
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;
}