cristi.gherghina
cristi.gherghina

Reputation: 321

Changing Color Definitions ncurses C

I am learning ncurses and I have made a little program and I want to fill my window with a color.

I want to fill it with a Red color but the default COLOR_RED is to bright and drives you crazy :) Here is what I tried.

    WINDOW *wnd = initscr();

    start_color();

    init_color(COLOR_RED, 184, 142, 12);

    init_pair(1, COLOR_WHITE, COLOR_RED);

    wbkgd(wnd, COLOR_PAIR(1));
    refresh();

How can I use a modified color ?

P.S : The code makes the background still the old COLOR_RED, not my modified one.

Upvotes: 0

Views: 3837

Answers (2)

Evan
Evan

Reputation: 63

init_color() is how you create a color definition. However, you can only create "new" colors if your terminal supports more than 8 colors. Most terminals do, but do not enable this by default. To check, print the COLORS variable in the ncurses library like so:

#include <ncurses.h>
printw("My terminal supports %d colors.\n", COLORS);

If it comes out to be 8, you will only be able to modify the default colors instead of defining your own. To be able to define your own colors, you will need to use a terminal that supports more than 8 colors. To do this try the following on the command line:

echo $TERM

If this comes out to be xterm-color, then type the following at the command line to enable a 256 color terminal:

export TERM=xterm-256color

Then check the COLORS variable again. It should be updated to 256, provided all went well. You can now use init_color() to define your own colors.

More information on ncurses routines can be found here: http://invisible-island.net/ncurses/man/curs_color.3x.html#h3-Routine-Descriptions

Upvotes: 2

William McBrine
William McBrine

Reputation: 2266

The only answer is init_color(). It's just that it doesn't work on most terminals (i.e. you're stuck with the original colors). You can check for the ability with can_change_color(), but that may not always be accurate, either.

Upvotes: 0

Related Questions