How to use terminal color palette with curses

The following I figured out by experiment on my own pc (Ubuntu 14.04, python 3).

  • There are 256 colors (defined by the first 8 bits).
  • The other bits are used for additional attributes, such as highlighting.
  • Passing the number -1 as color falls back to the default background and foreground colors.
  • The color pair 0 (mod 256) is fixed on (-1, -1).
  • The colors 0 till 15 are the terminal palette colors.

Consider the following testing code.
Add this to your .bashrc:

# Set proper $TERM if we are running gnome-terminal
if [ "$COLORTERM" == "gnome-terminal" ]
then
    TERM=xterm-256color
fi

Put this in a python file and run it.

import curses

def main(stdscr):
    curses.start_color()
    curses.use_default_colors()
    for i in range(0, curses.COLORS):
        curses.init_pair(i + 1, i, -1)
    try:
        for i in range(0, 255):
            stdscr.addstr(str(i), curses.color_pair(i))
    except curses.ERR:
        # End of screen reached
        pass
    stdscr.getch()

curses.wrapper(main)

Running it will yield the following output.

screenshot

As you see, the colors pairs 1-16 are the terminal color palette for foreground colors.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)