1

I'm using init_color(); in ncurses in C to try to define new RGB color values. However, init_color(); does not take affect and change the default colors once I run the program.

I have tried moving around the init_color(); statements before and after both the init_pair(); statements and start_color(); but have had no luck. I also have tried using different values (ASCII, and numbers from other sources) in place of ex. COLOR_MAGENTA, for the first argument in one of the init_color(); statements, but also no luck. My start_color();, init_color(); and init_pair(); statements are all within a main function before the rest of the program. My terminal (using cloud9/cs50) supports 256 colors (checked using terminal commands). Also all color definitions are above function 'main'.

int main(int argc, char *argv[])
{
    // ensure that number of arguments is as expected
    if (argc != 1)
    {
      fprintf(stderr, "Usage: ./lemonade\n");
      return 1;
    }

// start up ncurses
if (!startup())
{
    fprintf(stderr, "Error starting up ncurses\n");
    return 2;
}

// initialize colors
start_color();

// re-asign specific RGB value to colors
init_color(COLOR_MAGENTA, 254, 160, 207);
init_color(COLOR_GREEN, 37, 244, 82);
init_color(COLOR_BLUE, 96, 82, 186);

// used cyan for a different greeen
init_color(COLOR_CYAN, 46, 243, 74);

// used yellow for a grey
init_color(COLOR_YELLOW, 156, 156, 156);

// used red for a purple
init_color(COLOR_RED, 208, 196, 253);

// initilaize color pairs
init_pair(LOGO_PAIR, COLOR_MAGENTA, COLOR_GREEN);
init_pair(DRAWBORDERSSPECIAL_PAIR, COLOR_BLACK, COLOR_GREEN);
init_pair(BORDERS_PAIR, COLOR_WHITE, COLOR_BLACK);
init_pair(SPECIALNEXT_PAIR, COLOR_BLACK, COLOR_GREEN);
init_pair(SUNNYBLUE_PAIR, COLOR_WHITE, COLOR_BLUE);
init_pair(WEATHERGREEN_PAIR, COLOR_WHITE, COLOR_CYAN);
init_pair(CLOUDYGREY_PAIR, COLOR_WHITE, COLOR_YELLOW);
init_pair(HOTPURPLE_PAIR, COLOR_WHITE, COLOR_RED);

// clean
clean();            // clean includes (refresh(); and clear();)

// draw borders
drawborders();

// run screen 1
screenone();





// support color test                           
mvprintw(6, 50, "My terminal supports %d colors.", COLORS);

// has_color(); test    
if (has_colors() == FALSE)
{
    mvprintw(7, 50, "Your terminal does not support color \n");
}

// can_change_color(); test     
if (can_change_color() == FALSE)
{
    mvprintw(8, 50, "Can_change_color is false \n");
}

I expected the init_color(); statements to take affect and change the default colors (Ex. Magenta, black, etc.) to the newly assigned specific RGB values, but they remain the same once the program runs.

I added checks to the number of colors supported, has_colors();, and can_change_color();. The number of colors support returns 8, has_colors(); returns true, and finally can_change_color(); returns false. Thank you for suggesting using has_colors(); and can_change_color(); although this seems to be the issue I'm not sure where to go from here?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Miles
  • 21
  • 4
  • 1
    Possible duplicate of [Changing Color Definitions ncurses C](https://stackoverflow.com/questions/27642696/changing-color-definitions-ncurses-c) – Thomas Dickey Apr 12 '19 at 08:10

1 Answers1

0

Hmm, I cannot see your calls to has_colors() and can_change_color(), which should be used to detect if you're even allowed to do this on your system?

This is the first thing you should be checking. It may be that color changing is not permitted in your environment.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Currently ```can_change_color();``` returns true, as does ``has_color();```. I used the terminal command "echo $TERM" and received 'xterm-256color'. I also used "tput colors" and received '256'. I'm not sure why the ```init_color();``` statements won't take effect even thought the terminal supports 256 color? – Miles Apr 12 '19 at 15:44