user3407196
user3407196

Reputation:

Unexpected curses fore/background 256 color init_pair'ing

After proper curses/window initialization, I can color_pair default color pairs, e.g. using foreground,-1 and -1,background colors, but when I start customizing pairs using custom pair #s with bg/fg colors values > 0x8 I get unexpected or wrong results.

Term's env $TERM == 'xterm-256color'

Python's curses.COLORS == 256

Python's curses.COLOR_PAIRS == 32767

Python's version == 2.7.7, curses == 2.2

#!/usr/bin/env python
"""Dumbed down code to follow:"""

import curses

# init
window = curses.initscr()
curses.start_color()
curses.use_default_colors()

# assign 'default' pairs, pairs are assigned +1 MORE than the color value!
for each in range(curses.COLORS):
    curses.init_pair(each + 1, each, -1)
for each in range(curses.COLORS):
    curses.init_pair(each + 1 + curses.COLORS, -1, each)

# custom/non-default pair
curses.init_pair(1 + 2*curses.COLORS, 0x0f, 0x15)  # white on cobalt according to colors above ???
curses.init_pair(4321, 0xd5, 0x81)  # hot pink on violet according to colors above ???

# setup
curses.meta(1)
curses.noecho()
curses.cbreak()
window.leaveok(1)
window.scrollok(0)
window.keypad(1)
window.refresh()

# print all pairs in their colors
for each in range(1 + 2*curses.COLORS):
    window.addstr(hex(each).join('  '), curses.color_pair(each))  # these are all perfect
window.addstr(hex(1 + 2*curses.COLORS).join('  '), curses.color_pair(1 + 2*curses.COLORS))  # nope: this prints 0,-1: black on default ???
window.addstr(hex(4321).join('  '), curses.color_pair(4321))  # nope: this prints 0xe1,-1: pinkish on default ???

# update
window.noutrefresh()
curses.doupdate()

# pause
window.getch()

# teardown
window.leaveok(0)
window.scrollok(1)
window.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()

Check the "???"s above. What concept am I missing? I'd like to have a color pair for every singe 256 color plus a handful of custom fg/bg?

Upvotes: 1

Views: 826

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54563

With ncurses5, you can have only 256 color pairs, because the values are stored in an 8-bit field. That is in the ncurses FAQ Why not make "xterm" equated to "xterm-256color"?.

Upvotes: 2

Related Questions