0

I want to use 256 colours background and 256 colours foreground for my application using curses. This requires 65536 colour pairs: which seems to be supported. curses.COLOR_PAIRS reports 65536. This is my code:

def main(stdscr):
    curses.use_default_colors()
    for i in range(0, curses.COLORS):
        for j in range(0, curses.COLORS - 1): #a bit of a hack, but it should work anyway.
            curses.init_pair(i * curses.COLORS + j + 1, i, j) #colour 1 is hardwired, so we have to omit one colour

...

    srdscr.addstr(y, x, char, curses.color_pair(fg * curses.COLORS + bg)) #ERROR

Curses hapily allows me to define 65535 (since 0 is hardwired) colour pairs, but won't let me use them. What's going on? How can I use more than 256 colour pairs?

Here's the exact error message, variable names differ of course.

Traceback (most recent call last):
  File "client.py", line 173, in <module>
    a.mainLoop()
  File "client.py", line 134, in mainLoop
    curses.wrapper(self.main)
  File "/usr/local/lib/python3.10/curses/__init__.py", line 94, in wrapper
    return func(stdscr, *args, **kwds)
  File "client.py", line 169, in main
    self.drawScreen()
  File "client.py", line 152, in drawScreen
    win.draw()
  File "client.py", line 121, in draw
    self.s.addstr(y, x, char[2], curses.color_pair(char[0] * 256 + char[1]))
ValueError: Color number is greater than COLORS (256).

I'm using python 3.10 on Linux, I've tried with 3.9, 3.8.5 etc and nothing has worked.

Jachdich
  • 782
  • 8
  • 23
  • perhaps this line: `for i in range(0, curses.COLORS):`, since you're using the full range for **`i`** later in the init_pair call. – Thomas Dickey Sep 26 '20 at 14:41
  • @ThomasDickey what's wrong with it? – Jachdich Sep 26 '20 at 14:42
  • This part "curses.COLOR_PAIRS reports 65536" is because the python curses is using ncurses ABI 5 (if it were using ABI 6, it would get the extended numbers readily enough -- there shouldn't be any code in its binding which forces colors to a 16-bit short value for instance). – Thomas Dickey Sep 26 '20 at 14:43
  • @ThomasDickey Hang on I'm confused: I don't have `ncurses` <6.2 installed, and also 65536 is enough, so why can I define that number but not use it? – Jachdich Sep 26 '20 at 14:45
  • ncurses 6.x can always be compiled for ABI 5 (most packagers have gotten around to using ABI 6, but not all). Also, the limitation might be in the python binding (which I've not studied...). – Thomas Dickey Sep 26 '20 at 14:52
  • @ThomasDickey yeah seems probable that it's the python bindings. Thanks for your help! I'm probably just gonna have to find an alternative to `curses` – Jachdich Sep 26 '20 at 14:53

0 Answers0