Darcendsun
Darcendsun

Reputation: 87

Background color using ncurses on Linux

first post here so I apologize for any errors.

Basically, when I run my code, everything works properly except for changing the background color. For some reason it is always grey. I'm attempting to change it to black but it's not working and I'm not sure why.

The main part of my code that I believe should be changing the background color is this: wattron(mainWindow, COLOR_BLACK);

Any help to figure out how to change my background to Black would be greatly appreciated. Thanks guys!

Here is what I have so far in case it helps give some context for my problem:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <curses.h>
#include <time.h>

// Variables
int nlines;
int ncols;
int x;
int y;
int y0;
int x0;
int input;

// Constants
const int MAX_LINES = 10;
const int MAX_COLUMNS = 10;

// Main function
int main(void) {
    WINDOW * mainWindow;

    // Initialize ncurses
    if ( (mainWindow = initscr()) == NULL) {
        fprintf(stderr, "Could not initialize ncurses!\n");
        exit(EXIT_FAILURE);
    }

    // Call function to use color
    start_color();

    // Create my own color pairs
    init_pair(1, COLOR_CYAN, COLOR_BLACK);
    init_pair(2, COLOR_BLUE, COLOR_RED);

    // First clear off the screen
    clear();

    // Move the cursor
    y = 8;
    x = 30;
    move(y, x);

    // Refresh
    refresh();

    // Test output - working
    // printw("Testing...");

    waddch(mainWindow, 'T' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, 'E' | A_UNDERLINE | COLOR_PAIR(2));
    waddch(mainWindow, 'S' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, 'T' | A_UNDERLINE | COLOR_PAIR(2));
    waddch(mainWindow, 'I' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, 'N' | A_UNDERLINE | COLOR_PAIR(2));
    waddch(mainWindow, 'G' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, '.' | A_UNDERLINE | COLOR_PAIR(2));
    waddch(mainWindow, '.' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, '.' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, '.' | A_UNDERLINE | COLOR_PAIR(2));

    // Make background a different color
    wattron(mainWindow, COLOR_BLACK);

    // Hold until user inputs a character
    input =  getch();

    // Clean up
    delwin(mainWindow);
    endwin();
    refresh();

    return EXIT_SUCCESS;
}

Upvotes: 2

Views: 4147

Answers (2)

Whatever playgames
Whatever playgames

Reputation: 13

In the call wattron(WINDOW* win, chtype ch) you cannot use COLOR_BLACK as ch because COLOR_BLACK is an integer, not an attribute (value of chtype). You should call this function using COLOR_PAIR:

wattron(mainWindow, COLOR_PAIR(0)); // The pair number 0 is the terminal's default pair (white foreground, black background). 

Note that wattron only enables specified attributes. If you want to set a background (it's reset attributes of each cell) of a window, you will better use wbkgd(WINDOW *win, chtype ch). Documentation of it you can find in the wbkgd(3ncurses) manual page.

The ncurses' tutorial that helped me the most is here. I recommend you reading the 10th Colors section.

Upvotes: 0

Falmarri
Falmarri

Reputation: 48559

You need to call wbkgd(WINDOW *win, chtype ch)

Upvotes: 1

Related Questions