I'm writing a program in Python using the curses
module in the standard library.
I want my program to just exit if it can't use custom colors I specify with RGB triples.
So I have some starter code that looks like:
import curses
def main(stdscr):
if not curses.can_change_color():
raise Exception('Cannot change color')
curses.init_color(curses.COLOR_BLACK, 999, 0, 0)
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
stdscr.addstr('hello', curses.color_pair(1))
stdscr.addstr(' world', curses.color_pair(2))
stdscr.getch()
curses.wrapper(main)
And the result I get is:
I expected the black to be replaced by red.
Am I misunderstanding the docs? How can I get curses
to respect custom RGB colors I want to use? Or at least fail and tell me that the terminal doesn't support it?
The docs for curses here seem to suggest that on failure it will return an error, and the CPython source seems to propagate curses errors pretty faithfully.
In case it is relevant, I'm on OS X 10.11, and I'm testing on Python3 I installed with Homebrew. But I get the same effect with OS X's builtin Python interpreter as well.
EDIT:
Slightly modified sample code to display color content:
import curses
def main(stdscr):
if not curses.can_change_color():
raise Exception('Cannot change color')
stdscr.addstr(1, 0, repr(curses.color_content(curses.COLOR_BLACK)))
curses.init_color(curses.COLOR_BLACK, 999, 0, 0)
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
stdscr.addstr(0, 0, 'hello', curses.color_pair(1))
stdscr.addstr(' world', curses.color_pair(2))
stdscr.addstr(2, 0, repr(curses.color_content(curses.COLOR_BLACK)))
stdscr.getch()
curses.wrapper(main)