c++terminalncursesxtermwindows-terminal

Different colors on WIndows than on Linux in ncurses


I am creating an ncurses program in C++, where I redefine colors. But the colors are completely different on Windows than on Linux. On Linux, the colors work fine and look as intended. But on Windows, the colors are hideous and seemingly don't comply with the redefinitions I've given them.

void InitColors()
{
    start_color();

    if (can_change_color() && COLORS >= 256)
    {
        init_color(COLOR_BLACK, 400, 400,
                   400);
        init_color(COLOR_BLUE, 700, 700,
                   700);
        init_color(COLOR_WHITE, 900, 900,
                   900);

        init_pair(1, COLOR_WHITE,
                  COLOR_BLUE);
        init_pair(2, COLOR_BLACK,
                  COLOR_WHITE);
        bkgd(COLOR_PAIR(2));
    }
    else
    {
        // Fallback for terminals that don't support color
        if (COLORS >= 8)
        {
            init_pair(1, COLOR_WHITE, COLOR_BLUE); 
            init_pair(2, COLOR_WHITE, COLOR_BLACK); 
            init_pair(3, COLOR_BLACK,
                      COLOR_WHITE); 

            bkgd(COLOR_PAIR(2));
        }
        else
        {
            // Monochrome fallback
            init_pair(1, COLOR_WHITE, COLOR_BLACK);
            bkgd(COLOR_PAIR(1));
        }
    }
}

int main(int argc, char** argv)
{
    setlocale(LC_ALL,
              ""); 
    setlocale(LC_CTYPE, ""); 
    initscr();
    nodelay(stdscr, TRUE);
    noecho();
    raw();

    InitColors();
    clear();

    attron(COLOR_PAIR(1));

    printw("Hey there!\n");
    refresh();

    attron(COLOR_PAIR(2));

    printw("Hey there!\n");
    refresh();

    while (1) {}
}

Windows output:

windows

Linux output:

linux

I'm using WSL to test out the linux output. I'm using PDcurses on Windows.


Solution

  • Guys I fixed it by making my own color codes instead of redefining existing ones:

    const int MY_GREY1 = 100;
    const int MY_GREY2 = 101;
    const int MY_GREY3 = 102;
    
    init_color(MY_GREY1, 400, 400, 400);
    init_color(MY_GREY2, 300, 300, 300);
    init_color(MY_GREY3, 900, 900, 900);