]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/extended_color.c
ncurses 6.1 - patch 20190810
[ncurses.git] / test / extended_color.c
1 /****************************************************************************
2  * Copyright (c) 2017-2018,2019 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: extended_color.c,v 1.13 2019/01/21 14:59:34 tom Exp $
30  */
31
32 #include <test.priv.h>
33
34 #if USE_EXTENDED_COLOR
35
36 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
37
38 #if USE_SP_FUNCS
39 static bool opt_s = FALSE;
40 #define if_opt_s(a,b) (opt_s ? (a) : (b))
41 #else
42 #define if_opt_s(a,b) (b)
43 #endif
44
45 static void
46 failed(const char *name)
47 {
48     printw("...%s failed", name);
49     getch();
50     endwin();
51     ExitProgram(EXIT_FAILURE);
52 }
53
54 static void
55 do_pair_content(SCREEN *sp, int pair)
56 {
57     int i, f, b;
58
59     (void) sp;
60     i = if_opt_s(extended_pair_content_sp(sp, pair, &f, &b),
61                  extended_pair_content(0, &f, &b));
62     if (i != OK)
63         failed("pair_content");
64     printw("pair %d contains (%d,%d)\n", pair, f, b);
65     getch();
66 }
67
68 static void
69 do_init_pair(SCREEN *sp, int pair, int fg, int bg)
70 {
71     int i;
72
73     (void) sp;
74     i = if_opt_s(init_extended_pair_sp(sp, pair, fg, bg),
75                  init_extended_pair(pair, fg, bg));
76     if (i != OK)
77         failed("init_pair");
78 }
79
80 static void
81 do_init_color(SCREEN *sp, int color, int adjust)
82 {
83     int r, g, b;
84     int i;
85
86     (void) sp;
87     i = if_opt_s(extended_color_content_sp(sp, color, &r, &g, &b),
88                  extended_color_content(color, &r, &g, &b));
89     if (i != OK)
90         failed("color_content");
91
92     r = (adjust + 1000 + r) % 1000;
93     g = (adjust + 1000 + g) % 1000;
94     b = (adjust + 1000 + b) % 1000;
95
96     i = if_opt_s(init_extended_color_sp(sp, color, r, g, b),
97                  init_extended_color(color, r, g, b));
98     if (i != OK)
99         failed("init_color");
100 }
101
102 static void
103 do_color_set(const char *expected, int pair)
104 {
105     int i = color_set((short) pair, (void *) &pair);
106     printw("%s (%s)\n", expected, SHOW(i));
107     if (i != OK)
108         failed("color_set");
109     getch();
110 }
111
112 static void
113 show_1_rgb(SCREEN *sp, const char *name, int color, int y, int x)
114 {
115     int r, g, b;
116     int i;
117
118     (void) sp;
119     i = if_opt_s(extended_color_content_sp(sp, color, &r, &g, &b),
120                  extended_color_content(color, &r, &g, &b));
121     wmove(stdscr, y, x);
122     if (i == OK) {
123         printw("%-8s %3d/%3d/%3d", name, r, g, b);
124     } else {
125         printw("%-8s %s", name, SHOW(i));
126     }
127 }
128
129 static void
130 show_rgb(SCREEN *sp)
131 {
132     int y, x;
133     getyx(stdscr, y, x);
134     show_1_rgb(sp, "RED", COLOR_RED, y + 1, x);
135     show_1_rgb(sp, "GREEN", COLOR_GREEN, y + 2, x);
136     show_1_rgb(sp, "BLUE", COLOR_BLUE, y + 3, x);
137     wmove(stdscr, y, x);
138 }
139
140 static void
141 usage(void)
142 {
143     static const char *tbl[] =
144     {
145         "Usage: extended_color",
146         "",
147         "Options:",
148         " -s   use sp-funcs",
149         NULL
150     };
151     size_t n;
152     for (n = 0; n < SIZEOF(tbl); ++n) {
153         fprintf(stderr, "%s\n", tbl[n]);
154     }
155     ExitProgram(EXIT_FAILURE);
156 }
157
158 int
159 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
160 {
161     int i;
162     SCREEN *sp;
163
164     while ((i = getopt(argc, argv, "s")) != -1) {
165         switch (i) {
166 #if USE_SP_FUNCS
167         case 's':
168             opt_s = TRUE;
169             break;
170 #endif
171         default:
172             usage();
173             /* NOTREACHED */
174         }
175     }
176
177     slk_init(1);
178     sp = newterm(NULL, stdout, stdin);
179     cbreak();
180     noecho();
181
182     if (!has_colors()) {
183         endwin();
184         fprintf(stderr, "This demo requires a color terminal\n");
185         ExitProgram(EXIT_FAILURE);
186     }
187
188     start_color();
189
190     do_pair_content(sp, 0);
191
192     printw("Initializing pair 1 to red/black\n");
193     do_init_pair(sp, 1, COLOR_RED, COLOR_BLACK);
194     do_color_set("RED/BLACK", 1);
195
196     printw("Initializing pair 2 to white/blue\n");
197     do_init_pair(sp, 2, COLOR_WHITE, COLOR_BLUE);
198     do_color_set("WHITE/BLUE", 2);
199
200     printw("Initializing pair 3 to green/black\n");
201     do_init_pair(sp, 3, COLOR_GREEN, COLOR_BLACK);
202     do_color_set("GREEN/BLACK", 3);
203
204     printw("Resetting colors to pair 0\n");
205     do_color_set("Default Colors", 0);
206
207     printw("Resetting colors to pair 1\n");
208     do_color_set("RED/BLACK", 1);
209
210     printw("Drawing soft-key tabs with pair 2\n");
211     slk_attrset(A_BOLD);        /* reverse-video is hard to see */
212     i = if_opt_s(extended_slk_color_sp(sp, 2),
213                  extended_slk_color(2));
214     for (i = 1; i <= 8; ++i) {
215         char temp[80];
216         _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "(SLK-%d)", i);
217         slk_set(i, temp, 0);
218     }
219     slk_touch();
220     slk_noutrefresh();
221
222     i = if_opt_s(can_change_color_sp(sp),
223                  can_change_color());
224     if (i) {
225         do_color_set("Default Colors", 0);
226         printw("Press any key to stop...\n");
227         nodelay(stdscr, TRUE);
228         while (getch() == ERR) {
229             show_rgb(sp);
230             do_init_color(sp, COLOR_RED, 1);
231             do_init_color(sp, COLOR_BLUE, -1);
232             napms(50);
233         }
234         printw("...done");
235         nodelay(stdscr, FALSE);
236         getch();
237     }
238
239     endwin();
240
241     ExitProgram(EXIT_SUCCESS);
242 }
243
244 #else
245 int
246 main(void)
247 {
248     printf("This program requires the ncurses extended color/pair functions\n");
249     ExitProgram(EXIT_FAILURE);
250 }
251 #endif