new_pair(3NCURSES)

πŸ’– NAME πŸ’–

alloc_pair, find_pair, free_pair β€” dynamically allocate curses color pairs (this is a 3NCURSES library call situation, bestie πŸŽ€)


✨ SYNOPSIS ✨

#include <curses.h>

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

🌈 DESCRIPTION 🌈

okay so these functions? they're an extension to the curses library. not the OG vanilla stuff, the ncurses glow-up. πŸ’…

They let an application dynamically allocate a color pair using the foreground/background colors instead of you having to assign a fixed color pair number yourself, and they hand an unused pair back to the pool. very demure, very mindful of your resources.

so the number of colors may be related to the number of possible color pairs for a given terminal… or it may not. it's giving "it depends" 😭 here's the tea:

  • While almost all terminals allow setting the color attributes independently, it is unlikely that your terminal lets you modify the attributes of a given character cell without rewriting it. i.e., the foreground and background colors are applied as a pair. they're a package deal, no splitting them up πŸ’ž
  • Color pairs are the curses library's way of managing a color palette on a terminal. If the library does not keep track of the combinations of colors which are displayed, it will be inefficient. no cap.
  • For simple terminal emulators with only a few dozen color combinations, it's convenient to use the maximum number of combinations as the limit on color pairs:
COLORS * COLORS
  • Terminals which support default colors distinct from β€œANSI colors” add to the possible combinations, producing this total:
( COLORS + 1 ) * ( COLORS + 1 )
  • An application might use up to a few dozen color pairs to implement a color scheme.

Beyond that lies in the realm of programs using the foreground and background colors for β€œASCII art” (or some other non-textual application).

Also beyond those few dozen pairs, the required size for a table to represent the combinations grows rapidly with an increasing number of colors. like it snowballs, bestie 🫠

These functions allow a developer to let the screen library manage color pairs. main character energy: the library handles it, you just vibe.

πŸ¦‹ alloc_pair πŸ¦‹

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

  • If the combination already exists, alloc_pair returns the existing pair. reusing, eco-friendly queen πŸ‘‘
  • 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. yeah it evicts the oldest one, brutal but necessary πŸ’€

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 whether that color combination is already associated with a color pair, returning the pair number if it has been allocated. Otherwise it returns -1. no pair for you, sorry babe πŸ™ˆ

πŸŽ€ free_pair πŸŽ€

Marks the given color pair as unused, i.e., like color pair 0. it's back on the shelf, untouched, brand new ✨


πŸ’œ 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. periodt. don't get caught out here πŸ’…

lowkey important: if you're writing code that needs to run anywhere that isn't ncurses, wrap that call in NCURSES_VERSION or you WILL be humbled 😭

πŸ‘‘ AUTHORS πŸ‘‘

Thomas Dickey


🌟 SEE ALSO 🌟

color(3NCURSES)


and that's the whole page, bestie!! go forth and allocate responsibly 🫢

πŸ’– this was new_pair(3NCURSES) from ncurses 6.6, dated 2025-08-16 β€” slay, stay colorful, and may your fast index never error out πŸ’–

πŸ“¦ where this page comes from