【发布时间】:2012-06-04 06:41:33
【问题描述】:
我正在尝试通过将值 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;
}
【问题讨论】:
-
你有没有想过这个问题?我有类似的问题,但在 OS X 中.. init_color 声称成功,但颜色没有改变。
-
@BenL。我在 macOS Catalina 下没有问题。而且我也从来没有遇到过 KDE Konsole 的问题(尽管只有 init_pair)。奇怪的。但是如果你只想设置背景红色和前景蓝色,你只需要
init_pair。