]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/new_pair.3x
ncurses 6.4 - patch 20230917
[ncurses.git] / man / new_pair.3x
1 .\"***************************************************************************
2 .\" Copyright 2018-2022,2023 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.30 2023/09/16 23:38:39 tom Exp $
33 .TH new_pair 3X 2023-09-16 "ncurses 6.4" "Library calls"
34 .ie \n(.g .ds `` \(lq
35 .el       .ds `` ``
36 .ie \n(.g .ds '' \(rq
37 .el       .ds '' ''
38 .de bP
39 .ie n  .IP \(bu 4
40 .el    .IP \(bu 2
41 ..
42 .ie n .ds CW R
43 .el   \{
44 .ie \n(.g .ds CW CR
45 .el       .ds CW CW
46 .\}
47 .de NS
48 .ie n  .sp
49 .el    .sp .5
50 .ie n  .in +4
51 .el    .in +2
52 .nf
53 .ft \*(CW
54 ..
55 .de NE
56 .fi
57 .ft R
58 .ie n  .in -4
59 .el    .in -2
60 ..
61 .SH NAME
62 \fB\%alloc_pair\fP,
63 \fB\%find_pair\fP,
64 \fB\%free_pair\fP \-
65 dynamically allocate \fIcurses\fR color pairs
66 .SH SYNOPSIS
67 \fB#include <curses.h>\fP
68 .sp
69 \fBint alloc_pair(int \fIfg\fB, int \fIbg\fB);\fR
70 .br
71 \fBint find_pair(int \fIfg\fB, int \fIbg\fB);\fR
72 .br
73 \fBint free_pair(int \fIpair\fB);\fR
74 .SH DESCRIPTION
75 These functions are an extension to the curses library.
76 They permit an application to dynamically allocate a color pair using
77 the foreground/background colors rather than assign a fixed color pair number,
78 and return an unused pair to the pool.
79 .PP
80 The number of colors may be related to the number of possible color
81 pairs for a given terminal, or it may not:
82 .bP
83 While almost all terminals allow setting the color \fIattributes\fP
84 independently,
85 it is unlikely that your terminal allows you to modify the attributes
86 of a given character cell without rewriting it.
87 That is, the foreground and background colors are applied as a pair.
88 .bP
89 Color pairs are the curses library's way of managing a color palette
90 on a terminal.
91 If the library does not keep track of the \fIcombinations\fP of
92 colors which are displayed, it will be inefficient.
93 .bP
94 For simple terminal emulators
95 with only a few dozen color combinations,
96 it is convenient to use the maximum number of combinations
97 as the limit on color pairs:
98 .NS
99 \fBCOLORS\fI * \fBCOLORS\fR
100 .NE
101 .bP
102 Terminals which support \fIdefault colors\fP distinct
103 from \*(``ANSI colors\*(''
104 add to the possible combinations, producing this total:
105 .NS
106 \fI( \fBCOLORS\fI + 1 ) * ( \fBCOLORS\fI + 1 )\fR
107 .NE
108 .bP
109 An application might use up to a few dozen color pairs to
110 implement a predefined color scheme.
111 .IP
112 Beyond that lies in the realm of programs using the foreground
113 and background colors for \*(``ASCII art\*(''
114 (or some other non-textual application).
115 .IP
116 Also beyond those few dozen pairs, the required size for a table
117 to represent the combinations grows rapidly with an increasing number of colors.
118 .IP
119 These functions allow a developer to let the screen library
120 manage color pairs.
121 .SS alloc_pair
122 The \fBalloc_pair\fP function accepts parameters for
123 foreground and background color, and
124 checks if that color combination is already associated with a color pair.
125 .bP
126 If the combination already exists,
127 \fBalloc_pair\fP returns the existing pair.
128 .bP
129 If the combination does not exist,
130 \fBalloc_pair\fP allocates a new color pair and returns that.
131 .bP
132 If the table fills up, \fBalloc_pair\fP discards the least-recently
133 allocated entry using \fBfree_pair\fP and allocates a new color pair.
134 .PP
135 All of the color pairs are allocated from a table of possible color pairs.
136 The size of the table is determined by the terminfo \fBpairs\fP capability.
137 The table is shared with \fBinit_pair\fP;
138 in fact \fBalloc_pair\fP calls \fBinit_pair\fP after
139 updating the ncurses library's fast index to the colors versus color pairs.
140 .SS find_pair
141 The \fBfind_pair\fP function accepts parameters for
142 foreground and background color, and
143 checks if that color combination is already associated with a color pair,
144 returning the pair number if it has been allocated.
145 Otherwise it returns \-1.
146 .SS free_pair
147 Marks the given color pair as unused,
148 i.e., like color pair 0.
149 .SH RETURN VALUE
150 The \fBalloc_pair\fP function returns a color pair number in the range
151 1 through \fBCOLOR_PAIRS\fP\-1, unless it encounters an error updating
152 its fast index to the color pair values, preventing it from allocating
153 a color pair.
154 In that case, it returns \-1.
155 .PP
156 The \fBfind_pair\fP function returns a color pair number if the
157 given color combination has been associated with a color pair,
158 or \-1 if not.
159 .PP
160 Likewise, \fBfree_pair\fP returns \fBOK\fP unless it encounters an
161 error updating the fast index or if no such color pair is in use.
162 .SH PORTABILITY
163 These routines are specific to ncurses.
164 They were not supported on
165 Version 7, BSD or System V implementations.
166 It is recommended that
167 any code depending on them be conditioned using NCURSES_VERSION.
168 .SH SEE ALSO
169 \fBcurs_color\fP(3X).
170 .SH AUTHOR
171 Thomas Dickey.