Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ncurses' start_color() darken regular text?

I'm currently creating a program using the ncurses library, but have ran into a problem while starting to implement colors.

When I use the start_color() functions from ncurses, the default text color becomes a grey (close to #CCC) rather than the regular white.

Code I'm using to compare:

#include <stdlib.h>
#include <string.h>
#include <ncurses.h>

/* Converts a str to a string of chtype. */
chtype* str_to_chtype(char* str) {
    int i;
    chtype* chstr;
    chstr = malloc(sizeof(chtype) * (strlen(str)+1));
    for(i = 0; *str; ++str, ++i) {
        chstr[i] = (chtype) *str;
    }
    chstr[i] = 0;
    return chstr;
}

int main() {
    /* Without color */
    initscr();
    addchstr(str_to_chtype("Testing"));
    getch();
    endwin();

    /* With color */
    initscr();
    start_color();
    addchstr(str_to_chtype("Testing"));
    getch();
    endwin();

    return 0;
}

Images: Before start_color After start_color

Any ideas as to why this would be happening/how to fix it?

like image 584
Bitani Avatar asked Dec 29 '25 18:12

Bitani


1 Answers

That's an FAQ (see Ncurses resets my colors to white/black). What is happening is that the 8 ANSI colors which define white and black do not necessarily match your default terminal foreground and background colors. Generally those are chosen to be more intense. So ANSI white looks like a light gray. (On some terminals, ANSI black turns out to be a different shade of gray).

This issue occurs whether your terminal has 8, 16, 88, 256 colors, since all of terminals which you will encounter for larger numbers of colors follow aixterm (16) or xterm (88, 256), which use the same first set of 8 ANSI colors (black is number 0, white is number 7).

As noted, use_default_colors is an extension (not part of X/Open curses) provided to work around this problem.

like image 124
Thomas Dickey Avatar answered Dec 31 '25 08:12

Thomas Dickey