Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redefine a color pair in ncurses?

Tags:

c

ncurses

The title should say it all, so say I created a color pair like this:

init_pair(1, COLOR_RED, COLOR_BLACK);

And then I wanted to redefine it, like this:

init_pair(1, COLOR_GREEN, COLOR_BLUE);

Now this doesn't work, but are there any other functions that allow me to do so? Or heck, are there functions so that I can just use explicit foreground/background colors without using color pairs?

Thanks in advance, and let me know if this isn't clear enough.


EDIT: After trying it out, you can redefine pairs using init_pair, and the problem in my code was somewhere else.

like image 562
MiJyn Avatar asked Nov 03 '22 05:11

MiJyn


1 Answers

You can define up to 7 color pairs at a time and show them all in sequence if you like. You just CHANGE the number and refresh. for example. (we'll do it with 3) see here for an e printout of an example with 7

 #include  <curses.h>

 int COLOR_PAIR (int PAIR_NUMBER)

 init_pair(1, COLOR_GREEN, COLOR_RED);
 init_pair(2, COLOR_GREEN, COLOR_BLUE);
 init_pair(3, COLOR_RED, COLOR_BLUE);

 for (i=1, i <=3, i++){
   attroffset(A_BOLD)
   attrset(COLOR_PAIR(i))
   refresh();
   sleep(1);
}

note if you have a lot of pairs and the second colour is always the same or the first one is unique in each pair, you can turn it on using attron() e.g. attron(COLOR_PAIR(COLOR_BLUE)); (this wouldn't work in our definitions)

you can view an example of attron here

like image 171
Rachel Gallen Avatar answered Nov 08 '22 06:11

Rachel Gallen