blob: 5e844bf8fc25c7c437bbae4cc0871efb89e5c1fe [file] [log] [blame]
micky3879b9f5e72025-07-08 18:04:53 -04001.\"***************************************************************************
2.\" Copyright 2018-2023,2024 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.46 2024/03/16 15:35:01 tom Exp $
33.TH new_pair 3X 2024-03-16 "ncurses @NCURSES_MAJOR@.@NCURSES_MINOR@" "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 \-
53dynamically 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
63These functions are an extension to the \fIcurses\fP library.
64They permit an application to dynamically allocate a color pair using
65the foreground/background colors rather than assign a fixed color pair number,
66and return an unused pair to the pool.
67.PP
68The number of colors may be related to the number of possible color
69pairs for a given terminal, or it may not:
70.bP
71While almost all terminals allow setting the color \fIattributes\fP
72independently,
73it is unlikely that your terminal allows you to modify the attributes
74of a given character cell without rewriting it.
75That is, the foreground and background colors are applied as a pair.
76.bP
77Color pairs are the \fIcurses\fP library's way of managing a color palette
78on a terminal.
79If the library does not keep track of the \fIcombinations\fP of
80colors which are displayed, it will be inefficient.
81.IP \(bu 4
82For simple terminal emulators
83with only a few dozen color combinations,
84it is convenient to use the maximum number of combinations
85as 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
93Terminals which support \fIdefault colors\fP distinct
94from \*(``ANSI colors\*(''
95add 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
103An application might use up to a few dozen color pairs to
104implement a predefined color scheme.
105.IP
106Beyond that lies in the realm of programs using the foreground
107and background colors for \*(``ASCII art\*(''
108(or some other non-textual application).
109.IP
110Also beyond those few dozen pairs, the required size for a table
111to represent the combinations grows rapidly with an increasing number of colors.
112.IP
113These functions allow a developer to let the screen library
114manage color pairs.
115.SS alloc_pair
116The \fBalloc_pair\fP function accepts parameters for
117foreground and background color, and
118checks if that color combination is already associated with a color pair.
119.bP
120If the combination already exists,
121\fBalloc_pair\fP returns the existing pair.
122.bP
123If the combination does not exist,
124\fBalloc_pair\fP allocates a new color pair and returns that.
125.bP
126If the table fills up, \fBalloc_pair\fP discards the least-recently
127allocated entry using \fBfree_pair\fP and allocates a new color pair.
128.PP
129All of the color pairs are allocated from a table of possible color pairs.
130The size of the table is determined by the terminfo \fBpairs\fP capability.
131The table is shared with \fBinit_pair\fP;
132in fact \fBalloc_pair\fP calls \fBinit_pair\fP after
133updating the \fI\%ncurses\fP library's fast index
134to the colors versus color pairs.
135.SS find_pair
136The \fBfind_pair\fP function accepts parameters for
137foreground and background color, and
138checks if that color combination is already associated with a color pair,
139returning the pair number if it has been allocated.
140Otherwise it returns \-1.
141.SS free_pair
142Marks the given color pair as unused,
143i.e., like color pair 0.
144.SH RETURN VALUE
145The \fBalloc_pair\fP function returns a color pair number in the range
1461 through \fBCOLOR_PAIRS\fP\-1, unless it encounters an error updating
147its fast index to the color pair values, preventing it from allocating
148a color pair.
149In that case, it returns \-1.
150.PP
151The \fBfind_pair\fP function returns a color pair number if the
152given color combination has been associated with a color pair,
153or \-1 if not.
154.PP
155Likewise, \fBfree_pair\fP returns \fBOK\fP unless it encounters an
156error updating the fast index or if no such color pair is in use.
157.SH PORTABILITY
158These routines are specific to \fI\%ncurses\fP.
159They were not supported on
160Version 7, BSD or System V implementations.
161It is recommended that
162any code depending on them be conditioned using \fB\%NCURSES_VERSION\fP.
163.SH AUTHORS
164Thomas Dickey
165.SH SEE ALSO
166\fB\%curs_color\fP(3X)