是否可以定义自己的颜色(而不是更新已由 ncurse 定义的颜色)。
查阅文档后:https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/color.html 和之前的问题:ncurses multi colors on screen
我写了下面的代码,我可以在其中更新
COLOR_RED
但是如果我想用自己的颜色定义CUSTOM_GREEN
,文本确实是用合适的颜色写的但是整个背景都设置为这种颜色(而不是仅限字符)。如何不画背景只改变字符的颜色?
#include <curses.h>
#define CUSTOM_GREEN 0
int main(void) {
initscr();
start_color();
init_color(CUSTOM_GREEN, 100, 500, 100);
init_color(COLOR_RED, 500, 100, 100);
/* param 1 : color name
* param 2, 3, 4 : rgb content min = 0, max = 1000 */
init_pair(1, COLOR_BLACK, COLOR_RED);
init_pair(2, COLOR_BLACK, CUSTOM_GREEN);
attron(COLOR_PAIR(1));
printw("This should be printed in black with a red background!\n");
attron(COLOR_PAIR(2));
printw("And this in a green background!\n");
refresh();
getch();
endwin();
}
编辑1 感谢@IanAbbot 的大力帮助,这是对我有用的代码:On 可以定义自定义颜色,然后主动重新定义黑色背景
#include <curses.h>
#include <stdlib.h>
#define CUSTOM_GREEN 8
#define COLOR_BLACK 0
int main(void) {
if( initscr() == NULL )
{
printf("initscr() failed");
exit(EXIT_FAILURE);
};
start_color();
init_color(CUSTOM_GREEN, 100, 500, 100);
init_color(COLOR_BLACK, 0, 0, 0);
/* param 1 : color name
* param 2, 3, 4 : rgb content min = 0, max = 1000 */
init_pair(1, COLOR_BLACK, COLOR_RED);
init_pair(2, COLOR_BLACK, CUSTOM_GREEN);
attron(COLOR_PAIR(1));
printw("This should be printed in black with a red background!\n");
attron(COLOR_PAIR(2));
printw("And this in a green background!\n");
refresh();
getch();
endwin();
use_default_colors();
}