1

NCURSES_VERSION "5.7".

I checked COLOR_PAIRS in my terminal(iTerm2) is 32767 by below.

FILE *outputfile;
outputfile = fopen(“d.txt”, “a”);
fprintf(outputfile, “%d\n”, COLOR_PAIRS);
fclose(outputfile);

I tried the below code.


#include <ncurses.h>

int main()
{
    initscr();
    start_color();

    short cnt = 1;
    for (short bg = 0; bg < 100; bg++) {
    for (short fg = 0; fg < 100; fg++) {
        init_pair(cnt, fg, bg);
        attron(COLOR_PAIR(cnt));
        addstr("aaa");
        attroff(COLOR_PAIR(cnt));
        cnt++;
    }
    }

    refresh();
    getch();
    endwin();

    return 0;
}

It works well.

enter image description here

Even though you changed the cnt as 10, it works as well.

    short cnt = 10;

However, if you switch the cnt to 500, it gets broken.


#include <ncurses.h>

int main()
{
    initscr();
    start_color();

    short cnt = 500;
    for (short bg = 0; bg < 100; bg++) {
    for (short fg = 0; fg < 100; fg++) {
        init_pair(cnt, fg, bg);
        attron(COLOR_PAIR(cnt));
        addstr("aaa");
        attroff(COLOR_PAIR(cnt));
        cnt++;
    }
    }

    refresh();
    getch();
    endwin();

    return 0;
}

enter image description here

After investigating, I think this gets broken around 200 or higher.

I just don't understand why this happens and how to avoid. From my understanding, I can set pair_colors up to 32768.

===

P.S.

I tried the code below as well. And it's broken.

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

int main()
{
    initscr();
    start_color();

    // int a = 255; works
    // int a = 256; prints 0 pair color
    int a = 257; // broken
    init_pair(a, 4, 52);
    attron(COLOR_PAIR(a));
    addstr("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    attroff(COLOR_PAIR(a));

    refresh();
    getch();
    endwin();
    return 0;
}

It looks like init_pair only accepts 256?

eeeeeeeeengo
  • 133
  • 6
  • `I checked PAIR_COLORS in my terminal is 32767.` How did you check this? (It's `COLOR_PAIRS`, not `PAIR_COLORS`) – Daniel Kleinstein Aug 15 '21 at 20:26
  • @DanielKleinstein sorry it's COLOR_PAIRS, I added the code that I used for checking in the beginning of this post. – eeeeeeeeengo Aug 15 '21 at 20:32
  • maybe https://stackoverflow.com/questions/55972033/ncurses-init-extended-pair-cant-create-more-than-255-color-pairs ??? – eeeeeeeeengo Aug 16 '21 at 21:27
  • 1
    You have to use `attr_on` or `attr_set` if you want to use a color pair number greater than 255, and you must not use the `COLOR_PAIR` macro (because that will mask the color pair number to 8 bits). This is somewhat explained in the attron/attr_on manpage but not very well. Check the ncurses examples for working examples, though. – rici Aug 19 '21 at 05:47
  • 1
    Also, I think you need to use init_extended_pair and you might need to link with ncursesw. – rici Aug 19 '21 at 05:56

0 Answers0