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