| 1 |
.\"*************************************************************************** |
| 2 |
.\" Copyright 2018-2024,2025 Thomas E. Dickey * |
| 3 |
.\" Copyright 2017 Free Software Foundation, Inc. * |
| 4 |
.\" * |
| 5 |
.\" Permission is hereby granted, free of charge, to any person obtaining a * |
| 6 |
.\" copy of this software and associated documentation files (the * |
| 7 |
.\" "Software"), to deal in the Software without restriction, including * |
| 8 |
.\" without limitation the rights to use, copy, modify, merge, publish, * |
| 9 |
.\" distribute, distribute with modifications, sublicense, and/or sell * |
| 10 |
.\" copies of the Software, and to permit persons to whom the Software is * |
| 11 |
.\" furnished to do so, subject to the following conditions: * |
| 12 |
.\" * |
| 13 |
.\" The above copyright notice and this permission notice shall be included * |
| 14 |
.\" in all copies or substantial portions of the Software. * |
| 15 |
.\" * |
| 16 |
.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
| 17 |
.\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 18 |
.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
| 19 |
.\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
| 20 |
.\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
| 21 |
.\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
| 22 |
.\" THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
| 23 |
.\" * |
| 24 |
.\" Except as contained in this notice, the name(s) of the above copyright * |
| 25 |
.\" holders shall not be used in advertising or otherwise to promote the * |
| 26 |
.\" sale, use or other dealings in this Software without prior written * |
| 27 |
.\" authorization. * |
| 28 |
.\"*************************************************************************** |
| 29 |
.\" |
| 30 |
.\" Author: Thomas E. Dickey |
| 31 |
.\" |
| 32 |
.\" $Id: new_pair.3x,v 1.51 2025/08/16 19:11:47 tom Exp $ |
| 33 |
.TH new_pair 3NCURSES 2025-08-16 "ncurses 6.6" "Library calls" |
| 34 |
.ie \n(.g \{\ |
| 35 |
.ds `` \(lq |
| 36 |
.ds '' \(rq |
| 37 |
.\} |
| 38 |
.el \{\ |
| 39 |
.ie t .ds `` `` |
| 40 |
.el .ds `` "" |
| 41 |
.ie t .ds '' '' |
| 42 |
.el .ds '' "" |
| 43 |
.\} |
| 44 |
. |
| 45 |
.de bP |
| 46 |
.ie n .IP \(bu 4 |
| 47 |
.el .IP \(bu 2 |
| 48 |
.. |
| 49 |
.SH NAME |
| 50 |
\fB\%alloc_pair\fP, |
| 51 |
\fB\%find_pair\fP, |
| 52 |
\fB\%free_pair\fP \- |
| 53 |
dynamically allocate \fIcurses\fR color pairs |
| 54 |
.SH SYNOPSIS |
| 55 |
.nf |
| 56 |
\fB#include <curses.h> |
| 57 |
.PP |
| 58 |
\fBint alloc_pair(int \fIfg\fP, int \fIbg\fP); |
| 59 |
\fBint find_pair(int \fIfg\fP, int \fIbg\fP); |
| 60 |
\fBint free_pair(int \fIpair\fP); |
| 61 |
.fi |
| 62 |
.SH DESCRIPTION |
| 63 |
These functions are an extension to the \fIcurses\fP library. |
| 64 |
They permit an application to dynamically allocate a color pair using |
| 65 |
the foreground/background colors rather than assign a fixed color pair number, |
| 66 |
and return an unused pair to the pool. |
| 67 |
.PP |
| 68 |
The number of colors may be related to the number of possible color |
| 69 |
pairs for a given terminal, or it may not: |
| 70 |
.bP |
| 71 |
While almost all terminals allow setting the color \fIattributes\fP |
| 72 |
independently, |
| 73 |
it is unlikely that your terminal allows you to modify the attributes |
| 74 |
of a given character cell without rewriting it. |
| 75 |
That is, the foreground and background colors are applied as a pair. |
| 76 |
.bP |
| 77 |
Color pairs are the \fIcurses\fP library's way of managing a color palette |
| 78 |
on a terminal. |
| 79 |
If the library does not keep track of the \fIcombinations\fP of |
| 80 |
colors which are displayed, it will be inefficient. |
| 81 |
.IP \(bu 4 |
| 82 |
For simple terminal emulators |
| 83 |
with only a few dozen color combinations, |
| 84 |
it is convenient to use the maximum number of combinations |
| 85 |
as the limit on color pairs: |
| 86 |
.PP |
| 87 |
.RS 8 |
| 88 |
.EX |
| 89 |
\fBCOLORS\fI * \fBCOLORS\fR |
| 90 |
.EE |
| 91 |
.RE |
| 92 |
.IP \(bu 4 |
| 93 |
Terminals which support \fIdefault colors\fP distinct |
| 94 |
from \*(``ANSI colors\*('' |
| 95 |
add to the possible combinations, producing this total: |
| 96 |
.PP |
| 97 |
.RS 8 |
| 98 |
.EX |
| 99 |
\fI( \fBCOLORS\fI + 1 ) * ( \fBCOLORS\fI + 1 )\fR |
| 100 |
.EE |
| 101 |
.RE |
| 102 |
.bP |
| 103 |
An application might use up to a few dozen color pairs to |
| 104 |
implement a color scheme. |
| 105 |
.IP |
| 106 |
Beyond that lies in the realm of programs using the foreground |
| 107 |
and background colors for \*(``ASCII art\*('' |
| 108 |
(or some other non-textual application). |
| 109 |
.IP |
| 110 |
Also beyond those few dozen pairs, the required size for a table |
| 111 |
to represent the combinations grows rapidly with an increasing number of colors. |
| 112 |
.IP |
| 113 |
These functions allow a developer to let the screen library |
| 114 |
manage color pairs. |
| 115 |
.SS alloc_pair |
| 116 |
The \fBalloc_pair\fP function accepts parameters for |
| 117 |
foreground and background color, and |
| 118 |
checks whether that color combination |
| 119 |
is already associated with a color pair. |
| 120 |
.bP |
| 121 |
If the combination already exists, |
| 122 |
\fBalloc_pair\fP returns the existing pair. |
| 123 |
.bP |
| 124 |
If the combination does not exist, |
| 125 |
\fBalloc_pair\fP allocates a new color pair and returns that. |
| 126 |
.bP |
| 127 |
If the table fills up, \fBalloc_pair\fP discards the least-recently |
| 128 |
allocated entry using \fBfree_pair\fP and allocates a new color pair. |
| 129 |
.PP |
| 130 |
All of the color pairs are allocated from a table of possible color pairs. |
| 131 |
The size of the table is determined by the terminfo \fBpairs\fP capability. |
| 132 |
The table is shared with \fBinit_pair\fP; |
| 133 |
in fact \fBalloc_pair\fP calls \fBinit_pair\fP after |
| 134 |
updating the \fI\%ncurses\fP library's fast index |
| 135 |
to the colors versus color pairs. |
| 136 |
.SS find_pair |
| 137 |
The \fBfind_pair\fP function accepts parameters for |
| 138 |
foreground and background color, and |
| 139 |
checks whether that color combination |
| 140 |
is already associated with a color pair, |
| 141 |
returning the pair number if it has been allocated. |
| 142 |
Otherwise it returns \-1. |
| 143 |
.SS free_pair |
| 144 |
Marks the given color pair as unused, |
| 145 |
i.e., like color pair 0. |
| 146 |
.SH RETURN VALUE |
| 147 |
The \fBalloc_pair\fP function returns a color pair number in the range |
| 148 |
1 through \fBCOLOR_PAIRS\fP\-1, unless it encounters an error updating |
| 149 |
its fast index to the color pair values, preventing it from allocating |
| 150 |
a color pair. |
| 151 |
In that case, it returns \-1. |
| 152 |
.PP |
| 153 |
The \fBfind_pair\fP function returns a color pair number if the |
| 154 |
given color combination has been associated with a color pair, |
| 155 |
or \-1 if not. |
| 156 |
.PP |
| 157 |
Likewise, \fBfree_pair\fP returns \fBOK\fP unless it encounters an |
| 158 |
error updating the fast index or if no such color pair is in use. |
| 159 |
.SH PORTABILITY |
| 160 |
These routines are specific to \fI\%ncurses\fP. |
| 161 |
They were not supported on |
| 162 |
Version 7, BSD or System V implementations. |
| 163 |
It is recommended that |
| 164 |
any code depending on them be conditioned using \fB\%NCURSES_VERSION\fP. |
| 165 |
.SH AUTHORS |
| 166 |
Thomas Dickey |
| 167 |
.SH SEE ALSO |
| 168 |
\fB\%color\fP(3NCURSES) |