new_pair.3x - Man Page

new curses color-pair functions

Synopsis

#include <curses.h>

int alloc_pair(int fg, int bg);
int find_pair(int fg, int bg);
int free_pair(int pair);

Description

These functions are an extension to the curses library. They permit an application to dynamically allocate a color pair using the foreground/background colors rather than assign a fixed color pair number, and return an unused pair to the pool.

The number of colors may be related to the number of possible color pairs for a given terminal, or it may not:

alloc_pair

The alloc_pair function accepts parameters for foreground and background color, and checks if that color combination is already associated with a color pair.

  • If the combination already exists, alloc_pair returns the existing pair.
  • If the combination does not exist, alloc_pair allocates a new color pair and returns that.
  • If the table fills up, alloc_pair discards the least-recently allocated entry using free_pair and allocates a new color pair.

All of the color pairs are allocated from a table of possible color pairs. The size of the table is determined by the terminfo pairs capability. The table is shared with init_pair; in fact alloc_pair calls init_pair after updating the ncurses library's fast index to the colors versus color pairs.

find_pair

The find_pair function accepts parameters for foreground and background color, and checks if that color combination is already associated with a color pair, returning the pair number if it has been allocated. Otherwise it returns -1.

free_pair

Marks the given color pair as unused, i.e., like color pair 0.

Return Value

The alloc_pair function returns a color pair number in the range 1 through COLOR_PAIRS-1, unless it encounters an error updating its fast index to the color pair values, preventing it from allocating a color pair. In that case, it returns -1.

The find_pair function returns a color pair number if the given color combination has been associated with a color pair, or -1 if not.

Likewise, free_pair returns OK unless it encounters an error updating the fast index or if no such color pair is in use.

Portability

These routines are specific to ncurses. They were not supported on Version 7, BSD or System V implementations. It is recommended that any code depending on them be conditioned using NCURSES_VERSION.

See Also

curs_color(3X).

Author

Thomas Dickey.

Referenced By

The man pages alloc_pair.3x(3), find_pair.3x(3) and free_pair.3x(3) are aliases of new_pair.3x(3).