Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use terminal color palette with curses

I can't get the terminal color palette to work with curses.

import curses  def main(stdscr):     curses.use_default_colors()     for i in range(0,7):         stdscr.addstr("Hello", curses.color_pair(i))     stdscr.getch()  curses.wrapper(main) 

This python script yields the following screen:

enter image description here

However, I do have more colors in my gnome-terminal palette. How can I access them within curses?

like image 706
chtenb Avatar asked Aug 31 '13 18:08

chtenb


People also ask

How do you use curse colors?

To use color, you must call the start_color() function soon after calling initscr() , to initialize the default color set (the curses. wrapper() function does this automatically). Once that's done, the has_colors() function returns TRUE if the terminal in use can actually display color.


2 Answers

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.

like image 53
chtenb Avatar answered Sep 20 '22 02:09

chtenb


The terminal 'color palette' is set by the terminal application itself to map default curses colours to application-specific 'interpretations'. If you use red, the terminal can choose to display that as burgundy or cherry red, or if the user so desires, something completely different.

In other words, just use the curses colours (combined with or without the bright or blink modifiers) and things should Just Work.

I believe that the curses.use_default_colors() call merely makes transparency available; it is a direct call to the use_default_colors() ncurses API function. ncurses colors are otherwise palette based; you need to set your own color attributes per pair number with curses.init_pair() calls, then select a color pair with curses.color_pair() from the palette to display text with that specific pair; or build text attributes directly for a given addstr() call.

like image 40
Martijn Pieters Avatar answered Sep 18 '22 02:09

Martijn Pieters