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