c - 如何使用 mvinch 获取文本的颜色编号和颜色背景

标签 c gcc ncurses

非常感谢您之前的回答,但我还有一个问题。

如果我不知道使用的颜色是什么,我如何获取颜色文本和角色的颜色背景。 以你为例,你知道什么是颜色;然后你可以比较;但如果你不知道颜色是什么;我怎样才能比你得到颜色。

这是您发送给我的代码。

init_pair(1, COLOR_BLUE, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_WHITE, COLOR_BLACK);

attron(COLOR_PAIR(1));
mvprintw(1, 1, "Sky");

attron(COLOR_PAIR(2));
mvprintw(2, 1, "Grass");


const int color_one = mvinch(1, 1) & A_COLOR;
const int color_two = mvinch(2, 1) & A_COLOR;

attron(COLOR_PAIR(3));

if ( color_one == COLOR_PAIR(1) ) {
    mvprintw(4, 1, "Sky is blue");
}
else if ( color_one == COLOR_PAIR(2) ) {
    mvprintw(4, 1, "Sky is green");
}
else if ( color_one == COLOR_PAIR(3) ) {
    mvprintw(4, 1, "Sky is white");
}

if ( color_two == COLOR_PAIR(1) ) {
    mvprintw(5, 1, "Grass is blue");
}
else if ( color_two == COLOR_PAIR(2) ) {
    mvprintw(5, 1, "Grass is green");
}
else if ( color_two == COLOR_PAIR(3) ) {
    mvprintw(5, 1, "Grass is white");
}

refresh();
getch();
endwin();

最佳答案

要获取 COLOR_PAIR 编号,请使用:

int color_pair = PAIR_NUMBER(mvinch(1,1));

这将返回颜色对(在本例中为 1)

然后将此号码传递给:

int foreground , background;
pair_content(color_pair, &foreground, &background);

获取颜色。

如果您想要使用 RGB 颜色:

int r, g, b;
color_content(foreground, &r, &g, &b);

关于c - 如何使用 mvinch 获取文本的颜色编号和颜色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27715993/

相关文章:

c++ - -Wformat-truncation错误将2-3个字节写入2-6个字节的区域

c - AES算法的C实现

c - 段错误(核心转储)是什么意思? (快速排序代码)

c - 两个进程同时调用 open,一个 "W",另一个 "r"。发生错误?

c - subpad 返回 NULL

c - 使用 ncurses 处理转义序列? printf 是否处理转义序列?

Linux mint gdb-8.0.1 tui 支持吗?

c - 前向声明错误我无法理解

无法在 Eclipse 中使用 gcc -shared 找到库来构建 dll

c++ - 将 CRTP 与 static_assert 一起使用时出现编译器错误