]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/extended_color.c
ncurses 6.0 - patch 20171230
[ncurses.git] / test / extended_color.c
1 /****************************************************************************
2  * Copyright (c) 2017 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.10 2017/04/15 21:40:50 tom Exp $
30  */
31
32 #include <test.priv.h>
33
34 #if HAVE_INIT_EXTENDED_COLOR
35
36 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
37
38 static bool opt_s = FALSE;
39
40 static void
41 failed(const char *name)
42 {
43     printw("...%s failed", name);
44     getch();
45     endwin();
46     ExitProgram(EXIT_FAILURE);
47 }
48
49 static void
50 do_pair_content(SCREEN *sp, int pair)
51 {
52     int i, f, b;
53
54     if (opt_s) {
55         i = extended_pair_content_sp(sp, pair, &f, &b);
56     } else {
57         i = extended_pair_content(0, &f, &b);
58     }
59     if (i != OK)
60         failed("pair_content");
61     printw("pair %d contains (%d,%d)\n", pair, f, b);
62     getch();
63 }
64
65 static void
66 do_init_pair(SCREEN *sp, int pair, int fg, int bg)
67 {
68     int i;
69     if (opt_s) {
70         i = init_extended_pair_sp(sp, pair, fg, bg);
71     } else {
72         i = init_extended_pair(pair, fg, bg);
73     }
74     if (i != OK)
75         failed("init_pair");
76 }
77
78 static void
79 do_init_color(SCREEN *sp, int color, int adjust)
80 {
81     int r, g, b;
82     int i;
83     if (opt_s) {
84         i = extended_color_content_sp(sp, color, &r, &g, &b);
85     } else {
86         i = extended_color_content(color, &r, &g, &b);
87     }
88     if (i != OK)
89         failed("color_content");
90
91     r = (adjust + 1000 + r) % 1000;
92     g = (adjust + 1000 + g) % 1000;
93     b = (adjust + 1000 + b) % 1000;
94
95     if (opt_s) {
96         i = init_extended_color_sp(sp, color, r, g, b);
97     } else {
98         i = init_extended_color(color, r, g, b);
99     }
100     if (i != OK)
101         failed("init_color");
102 }
103
104 static void
105 do_color_set(const char *expected, int pair)
106 {
107     int i = color_set((short) pair, (void *) &pair);
108     printw("%s (%s)\n", expected, SHOW(i));
109     if (i != OK)
110         failed("color_set");
111     getch();
112 }
113
114 static void
115 show_1_rgb(SCREEN *sp, const char *name, int color, int y, int x)
116 {
117     int r, g, b;
118     int i;
119     if (opt_s) {
120         i = extended_color_content_sp(sp, color, &r, &g, &b);
121     } else {
122         i = extended_color_content(color, &r, &g, &b);
123     }
124     wmove(stdscr, y, x);
125     if (i == OK) {
126         printw("%-8s %3d/%3d/%3d", name, r, g, b);
127     } else {
128         printw("%-8s %s", name, SHOW(i));
129     }
130 }
131
132 static void
133 show_rgb(SCREEN *sp)
134 {
135     int y, x;
136     getyx(stdscr, y, x);
137     show_1_rgb(sp, "RED", COLOR_RED, y + 1, x);
138     show_1_rgb(sp, "GREEN", COLOR_GREEN, y + 2, x);
139     show_1_rgb(sp, "BLUE", COLOR_BLUE, y + 3, x);
140     wmove(stdscr, y, x);
141 }
142
143 static void
144 usage(void)
145 {
146     static const char *tbl[] =
147     {
148         "Usage: extended_color",
149         "",
150         "Options:",
151         " -s   use sp-funcs",
152         NULL
153     };
154     size_t n;
155     for (n = 0; n < SIZEOF(tbl); ++n) {
156         fprintf(stderr, "%s\n", tbl[n]);
157     }
158     ExitProgram(EXIT_FAILURE);
159 }
160
161 int
162 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
163 {
164     int i;
165     SCREEN *sp;
166
167     while ((i = getopt(argc, argv, "s")) != -1) {
168         switch (i) {
169         case 's':
170             opt_s = TRUE;
171             break;
172         default:
173             usage();
174             /* NOTREACHED */
175         }
176     }
177
178     slk_init(1);
179     sp = newterm(NULL, stdout, stdin);
180     cbreak();
181     noecho();
182
183     if (!has_colors()) {
184         endwin();
185         fprintf(stderr, "This demo requires a color terminal\n");
186         ExitProgram(EXIT_FAILURE);
187     }
188
189     start_color();
190
191     do_pair_content(sp, 0);
192
193     printw("Initializing pair 1 to red/black\n");
194     do_init_pair(sp, 1, COLOR_RED, COLOR_BLACK);
195     do_color_set("RED/BLACK", 1);
196
197     printw("Initializing pair 2 to white/blue\n");
198     do_init_pair(sp, 2, COLOR_WHITE, COLOR_BLUE);
199     do_color_set("WHITE/BLUE", 2);
200
201     printw("Initializing pair 3 to green/black\n");
202     do_init_pair(sp, 3, COLOR_GREEN, COLOR_BLACK);
203     do_color_set("GREEN/BLACK", 3);
204
205     printw("Resetting colors to pair 0\n");
206     do_color_set("Default Colors", 0);
207
208     printw("Resetting colors to pair 1\n");
209     do_color_set("RED/BLACK", 1);
210
211     printw("Drawing soft-key tabs with pair 2\n");
212     slk_attrset(A_BOLD);        /* reverse-video is hard to see */
213     if (opt_s) {
214         extended_slk_color_sp(sp, 2);
215     } else {
216         extended_slk_color(2);
217     }
218     for (i = 1; i <= 8; ++i) {
219         char temp[80];
220         sprintf(temp, "(SLK-%d)", i);
221         slk_set(i, temp, 0);
222     }
223     slk_touch();
224     slk_noutrefresh();
225
226     if (opt_s ? can_change_color_sp(sp) : can_change_color()) {
227         do_color_set("Default Colors", 0);
228         printw("Press any key to stop...\n");
229         nodelay(stdscr, TRUE);
230         while (getch() == ERR) {
231             show_rgb(sp);
232             do_init_color(sp, COLOR_RED, 1);
233             do_init_color(sp, COLOR_BLUE, -1);
234             napms(50);
235         }
236         printw("...done");
237         nodelay(stdscr, FALSE);
238         getch();
239     }
240
241     endwin();
242
243     ExitProgram(EXIT_SUCCESS);
244 }
245
246 #else
247 int
248 main(void)
249 {
250     printf("This program requires the ncurses extended color/pair functions\n");
251     ExitProgram(EXIT_FAILURE);
252 }
253 #endif