Debian Codegraph @

color.c

raw
1 /* $Id: color.c,v 1.42 2024/12/05 00:38:44 tom Exp $ */
2
3 #include <vttest.h>
4 #include <draw.h>
5 #include <esc.h>
6
7 #define MAX_COLORS 8
8
9 #define COLOR_BLACK 0
10 #define COLOR_RED 1
11 #define COLOR_GREEN 2
12 #define COLOR_YELLOW 3
13 #define COLOR_BLUE 4
14 #define COLOR_MAGENTA 5
15 #define COLOR_CYAN 6
16 #define COLOR_WHITE 7
17
18 static const char *colors[MAX_COLORS] =
19 {
20 "black", /* 30, 40 */
21 "red", /* 31, 41 */
22 "green", /* 32, 42 */
23 "yellow", /* 33, 43 */
24 "blue", /* 34, 44 */
25 "magenta", /* 35, 45 */
26 "cyan", /* 36, 46 */
27 "white" /* 37, 47 */
28 };
29
30 int do_colors = FALSE;
31
32 /*
33 * Pick an unusual color combination for testing, just in case the user's
34 * got the background set to something different.
35 */
36 static void
37 c_sgr(const char *s)
38 {
39 char temp[80];
40 int reset = FALSE;
41
42 (void) strcpy(temp, s);
43 if (*temp == ';' || *temp == 0) {
44 reset = TRUE;
45 } else {
46 char *t;
47 for (t = temp; *t != 0; t++) {
48 if (((t[0] == '0')
49 && (t == temp || t[-1] == ';')
50 && (t[1] == 0 || t[1] == ';'))
51 || ((t[0] == ';')
52 && (t[1] == ';'))) {
53 reset = TRUE;
54 break;
55 }
56 }
57 }
58
59 if (reset && do_colors) {
60 sprintf(temp + strlen(temp), ";%d;%d", COLOR_YELLOW + 30, COLOR_BLUE + 40);
61 }
62 sgr(temp);
63 }
64
65 /*
66 * Some terminals will reset colors with SGR-0; I've added the 39, 49 codes for
67 * those that are ISO compliant. (The black/white codes are for emulators
68 * written by people who don't bother reading standards).
69 */
70 static void
71 reset_all_colors(void)
72 {
73 sgr("0;40;37;39;49");
74 sgr("0");
75 }
76
77 static void
78 set_background(int bg)
79 {
80 if (do_colors) {
81 char temp[80];
82 (void) sprintf(temp, "4%d", bg);
83 sgr(temp);
84 }
85 }
86
87 static void
88 set_color_pair(int fg, int bg)
89 {
90 if (do_colors) {
91 char temp[80];
92 (void) sprintf(temp, "3%d;4%d", fg, bg);
93 sgr(temp);
94 }
95 }
96
97 static void
98 set_foreground(int fg)
99 {
100 if (do_colors) {
101 char temp[80];
102 (void) sprintf(temp, "3%d", fg);
103 sgr(temp);
104 }
105 }
106
107 static void
108 set_test_colors(void)
109 {
110 c_sgr("0");
111 }
112
113 static void
114 reset_test_colors(void)
115 {
116 /* Now, set the background again just in case there's a glitch */
117 set_foreground(COLOR_WHITE);
118 set_background(COLOR_BLACK);
119 }
120
121 /* Graphic rendition requires special handling with color, since SGR-0
122 * is supposed to reset the colors as well.
123 */
124 static void
125 show_graphic_rendition(void)
126 {
127 ed(2);
128 /* *INDENT-OFF* */
129 cup( 1,20); printxx("Color/Graphic rendition test pattern:");
130 cup( 4, 1); c_sgr("0"); tprintf("vanilla");
131 cup( 4,40); c_sgr("0;1"); tprintf("bold");
132 cup( 6, 6); c_sgr(";4"); tprintf("underline");
133 cup( 6,45); c_sgr(";1");c_sgr("4");tprintf("bold underline");
134 cup( 8, 1); c_sgr("0;5"); tprintf("blink");
135 cup( 8,40); c_sgr("0;5;1"); tprintf("bold blink");
136 cup(10, 6); c_sgr("0;4;5"); tprintf("underline blink");
137 cup(10,45); c_sgr("0;1;4;5"); tprintf("bold underline blink");
138 cup(12, 1); c_sgr("1;4;5;0;7"); tprintf("negative");
139 cup(12,40); c_sgr("0;1;7"); tprintf("bold negative");
140 cup(14, 6); c_sgr("0;4;7"); tprintf("underline negative");
141 cup(14,45); c_sgr("0;1;4;7"); tprintf("bold underline negative");
142 cup(16, 1); c_sgr("1;4;;5;7"); tprintf("blink negative");
143 cup(16,40); c_sgr("0;1;5;7"); tprintf("bold blink negative");
144 cup(18, 6); c_sgr("0;4;5;7"); tprintf("underline blink negative");
145 cup(18,45); c_sgr("0;1;4;5;7"); tprintf("bold underline blink negative");
146 cup(20, 6); c_sgr(""); set_foreground(9); tprintf("original foreground");
147 cup(20,45); c_sgr(""); set_background(9); tprintf("original background");
148 /* *INDENT-ON* */
149
150 c_sgr(""); /* same as c_sgr("0") */
151
152 decscnm(FALSE); /* Inverse video off */
153 cup(max_lines - 1, 1);
154 el(0);
155 tprintf("Dark background. ");
156 holdit();
157
158 decscnm(TRUE); /* Inverse video */
159 cup(max_lines - 1, 1);
160 el(0);
161 tprintf("Light background. ");
162 holdit();
163
164 decscnm(FALSE);
165 }
166
167 static void
168 show_line_deletions(void)
169 {
170 int row;
171
172 ed(2);
173 cup(1, 1);
174 printxx("This test deletes every third line from a list, marking cursor with '*'.\n");
175 printxx("The foreground and background should be yellow(orange) and blue, respectively.\n");
176
177 for (row = 5; row <= max_lines; row++) {
178 cup(row, 1);
179 tprintf(" row %3d: this is some text", row);
180 }
181 for (row = 7; row <= max_lines; row += 2 /* 3 - deletion */ ) {
182 cup(row, 8);
183 dl(1);
184 putchar('*'); /* cursor should be in column 1 */
185 }
186 cup(3, 1);
187 holdit();
188 }
189
190 static void
191 show_line_insertions(void)
192 {
193 int row;
194
195 ed(2);
196 cup(1, 1);
197 printxx("This test inserts after every second line in a list, marking cursor with '*'.\n");
198 printxx("The foreground and background should be yellow(orange) and blue, respectively.\n");
199
200 for (row = 5; row <= max_lines; row++) {
201 cup(row, 1);
202 tprintf(" row %3d: this is some text", row);
203 }
204 for (row = 7; row <= max_lines; row += 3 /* 2 + insertion */ ) {
205 cup(row, 8);
206 il(1);
207 putchar('*'); /* cursor should be in column 1 */
208 }
209 cup(3, 1);
210 holdit();
211 }
212
213 static int
214 show_test_pattern(MENU_ARGS)
215 /* generate a color test pattern */
216 {
217 int i, j, k;
218
219 reset_all_colors();
220 ed(2);
221 cup(1, 1);
222 printxx("There are %d color combinations", MAX_COLORS * MAX_COLORS);
223
224 for (k = 0; k <= 11; k += 11) {
225 cup(k + 2, 1);
226 tprintf("%dx%d matrix of foreground/background colors, bright *",
227 MAX_COLORS, MAX_COLORS);
228
229 if (k) {
230 sgr("1");
231 tprintf("on");
232 sgr("0");
233 } else {
234 tprintf("off");
235 }
236 tprintf("*");
237
238 for (i = 0; i < MAX_COLORS; i++) {
239 cup(k + 3, (i + 1) * 8 + 1);
240 tprintf("%s", colors[i]);
241 }
242
243 for (i = 0; i < MAX_COLORS; i++) {
244 cup(k + i + 4, 1);
245 tprintf("%s", colors[i]);
246 }
247
248 for (i = 0; i < MAX_COLORS; i++) {
249 for (j = 0; j < MAX_COLORS; j++) {
250 if (k)
251 sgr("1");
252 set_color_pair(j, i);
253 cup(k + 4 + i, (j + 1) * 8 + 1);
254 tprintf("Hello");
255 reset_all_colors();
256 }
257 }
258 }
259 reset_all_colors();
260 cup(max_lines - 1, 1);
261 return MENU_HOLD;
262 }
263
264 /*
265 * Clear around the box for simple_bce_test().
266 */
267 static void
268 simple_bce_erases(const BOX *box)
269 {
270 int i;
271
272 cup(box->top - 1, min_cols / 2);
273 ed(1); /* clear from home to cursor */
274 cuf(1);
275 el(0); /* clear from cursor to end of line */
276
277 cup(box->bottom + 1, min_cols / 2);
278 ed(0); /* clear from cursor to end */
279 cub(1);
280 el(1); /* clear to beginning of line */
281
282 for (i = box->top; i <= box->bottom; i++) {
283 cup(i, box->left - 1);
284 el(1);
285 cup(i, box->right + 1);
286 el(0);
287 }
288 }
289
290 /*
291 * "Real" color terminals support bce (background color erase).
292 *
293 * Set the foreground and background colors to something that's unusual.
294 * Then clear the screen (the background should stick) and draw some nested
295 * boxes (because that's simple). Use the ED, EL controls to clear away the
296 * outer box, so we can exercise the various parameter combinations of each
297 * of these.
298 */
299 static int
300 simple_bce_test(MENU_ARGS)
301 {
302 BOX box1;
303 BOX box2;
304
305 static const char *text1[] =
306 {
307 "The screen background should be blue, with a box made of asterisks",
308 " and this caption, in orange (non-bold yellow). ",
309 " There should be no cells with the default foreground or background.",
310 NULL
311 };
312 static const char *text2[] =
313 {
314 "The screen background should be black, with a box made of asterisks",
315 " and this caption, in white (actually gray - it is not bold). ",
316 " Only the asterisk box should be in color.",
317 NULL
318 };
319
320 if (make_box_params(&box1, 3, 10) < 0
321 || make_box_params(&box2, 7, 18) < 0)
322 return MENU_NOHOLD;
323
324 set_test_colors();
325 decaln();
326
327 draw_box_filled(&box1, 'X');
328 draw_box_outline(&box2, '*');
329
330 simple_bce_erases(&box2);
331
332 draw_box_caption(&box2, 1, text1);
333
334 cup(max_lines - 1, 1);
335 holdit();
336
337 reset_test_colors();
338
339 simple_bce_erases(&box2);
340
341 draw_box_caption(&box2, 1, text2);
342
343 cup(max_lines - 1, 1);
344 holdit();
345
346 reset_all_colors();
347 return MENU_NOHOLD;
348 }
349
350 /*
351 * Clear around the box for fancy_bce_test().
352 */
353 static void
354 fancy_bce_erases(const BOX *box)
355 {
356 int i;
357
358 cup(box->top - 1, min_cols / 2);
359 ed(1); /* clear from home to cursor */
360 cuf(1);
361 el(0); /* clear from cursor to end of line */
362
363 cup(box->bottom + 1, min_cols / 2);
364 ed(0); /* clear from cursor to end */
365 cub(1);
366 el(1); /* clear to beginning of line */
367
368 for (i = box->top; i <= box->bottom; i++) {
369 int first;
370 int limit;
371
372 cup(i, box->left - 1);
373 el(1);
374 cup(i, box->right + 1);
375 limit = min_cols - box->right;
376 first = i + 1 - box->top;
377 if (first > limit)
378 first = limit;
379 dch(first);
380 limit -= first;
381 if (limit > 0)
382 ech(limit);
383 }
384 }
385
386 /*
387 * Scroll the box up/down to check if the colors are being shifted in.
388 */
389 static void
390 fancy_bce_shifts(const BOX *box)
391 {
392 int i;
393 int limit = box->top - 1;
394
395 decsclm(TRUE); /* slow it down a little */
396 cup(1, 1);
397 for (i = 0; i < limit; ++i)
398 dl(1);
399
400 for (i = 0; i < limit; ++i)
401 ri();
402 decsclm(FALSE);
403 }
404
405 /*
406 * Scroll the box up/down to check if the colors are being shifted in. Erase
407 * the colors with ECH and DCH.
408 */
409 static int
410 fancy_bce_test(MENU_ARGS)
411 {
412 BOX box1;
413 BOX box2;
414
415 static const char *text1[] =
416 {
417 "The screen background should be blue, with a box made of asterisks",
418 " and this caption, in orange (non-bold yellow). ",
419 " There should be no cells with the default foreground or background.",
420 NULL
421 };
422 static const char *text2[] =
423 {
424 "The screen background should be black, with a box made of asterisks",
425 " and this caption, in white (actually gray - it is not bold). ",
426 " Only the asterisk box should be in color.",
427 NULL
428 };
429
430 if (make_box_params(&box1, 3, 10) < 0
431 || make_box_params(&box2, 7, 18) < 0)
432 return MENU_NOHOLD;
433
434 set_test_colors();
435 decaln();
436
437 draw_box_filled(&box1, 'X');
438 draw_box_outline(&box2, '*');
439
440 fancy_bce_erases(&box2);
441 draw_box_caption(&box2, 1, text1);
442 fancy_bce_shifts(&box2);
443
444 cup(max_lines - 1, 1);
445 holdit();
446
447 reset_test_colors();
448
449 fancy_bce_erases(&box2);
450 draw_box_caption(&box2, 1, text2);
451 fancy_bce_shifts(&box2);
452
453 cup(max_lines - 1, 1);
454 holdit();
455
456 reset_all_colors();
457 return MENU_NOHOLD;
458 }
459
460 static int
461 test_color_movements(MENU_ARGS)
462 {
463 set_test_colors();
464
465 tst_movements(PASS_ARGS);
466 reset_all_colors();
467 return MENU_NOHOLD;
468 }
469
470 static int
471 test_color_screen(MENU_ARGS)
472 {
473 set_test_colors();
474
475 /* The rest of the test can be done nicely with the standard vt100 test
476 * for insert/delete, since it doesn't modify SGR.
477 */
478 tst_screen(PASS_ARGS);
479 reset_all_colors();
480 return MENU_NOHOLD;
481 }
482
483 /*
484 * Test the insert/delete line/character operations for color (bce) terminals
485 * We'll test insert/delete line operations specially, because it is very hard
486 * to see what is happening with the accordion test when it does not work.
487 */
488 static int
489 test_color_insdel(MENU_ARGS)
490 {
491 set_test_colors();
492
493 show_line_insertions();
494 show_line_deletions();
495
496 /* The rest of the test can be done nicely with the standard vt100 test
497 * for insert/delete, since it doesn't modify SGR.
498 */
499 tst_insdel(PASS_ARGS);
500 reset_all_colors();
501 return MENU_NOHOLD;
502 }
503
504 /*
505 * Test the other ECMA-48 features with color setup.
506 */
507 static int
508 test_ecma48_misc(MENU_ARGS)
509 {
510 set_test_colors();
511
512 tst_ecma48_misc(PASS_ARGS);
513 reset_all_colors();
514 return MENU_NOHOLD;
515 }
516
517 static int
518 test_bce_color(MENU_ARGS)
519 {
520 set_test_colors();
521
522 do_scrolling();
523 show_graphic_rendition();
524 reset_all_colors();
525 return MENU_NOHOLD;
526 }
527
528 /*
529 * VT220 and higher implement the 22, 24, 25 and 27 codes.
530 * VT510 implements concealed text.
531 *
532 * ISO 6429 specifies additional SGR codes so that one needn't use SGR 0
533 * to reset everything before switching, e.g., set/clear pairs are
534 * bold 1/22
535 * faint 2/22
536 * italics 3/23
537 * underline 4/24
538 * blink 5/25
539 * inverse 7/27
540 * concealed 8/28
541 */
542 static int
543 test_iso_6429_sgr(MENU_ARGS)
544 {
545 set_test_colors();
546
547 ed(2);
548 /* *INDENT-OFF* */
549 cup( 1,20); printxx("Extended/Graphic rendition test pattern:");
550 cup( 4, 1); c_sgr("0"); tprintf("vanilla");
551 cup( 4,40); c_sgr("0;1"); tprintf("bold");
552 cup( 6, 6); c_sgr("22;4"); tprintf("underline");
553 cup( 6,45); c_sgr("24;1;4"); tprintf("bold underline");
554 cup( 8, 1); c_sgr("22;24;5"); tprintf("blink");
555 cup( 8,40); c_sgr("25;5;1"); tprintf("bold blink");
556 cup(10, 6); c_sgr("22;4;5"); tprintf("underline blink");
557 cup(10,45); c_sgr("24;25;1;4;5"); tprintf("bold underline blink");
558 cup(12, 1); c_sgr("22;24;25;7"); tprintf("negative");
559 cup(12,40); c_sgr("1"); tprintf("bold negative");
560 cup(14, 6); c_sgr("22;4;7"); tprintf("underline negative");
561 cup(14,45); c_sgr("1;4;7"); tprintf("bold underline negative");
562 cup(16, 1); c_sgr("22;24;5;7"); tprintf("blink negative");
563 cup(16,40); c_sgr("1"); tprintf("bold blink negative");
564 cup(18, 6); c_sgr("22;4"); tprintf("underline blink negative");
565 cup(18,45); c_sgr("1"); tprintf("bold underline blink negative");
566 cup(20, 6); c_sgr(""); set_foreground(9); tprintf("original foreground");
567 cup(20,45); c_sgr(""); set_background(9); tprintf("original background");
568 cup(22, 1); c_sgr(";8"); tprintf("concealed");
569 cup(22,40); c_sgr("8;7"); tprintf("concealed negative");
570 /* *INDENT-ON* */
571
572 c_sgr(""); /* same as c_sgr("0") */
573 tprintf(" <- concealed text");
574
575 decscnm(FALSE); /* Inverse video off */
576 cup(max_lines - 1, 1);
577 el(0);
578 tprintf("Dark background. ");
579 holdit();
580
581 decscnm(TRUE); /* Inverse video */
582 cup(max_lines - 1, 1);
583 el(0);
584 tprintf("Light background. ");
585 holdit();
586
587 decscnm(FALSE);
588 cup(max_lines - 1, 1);
589 el(0);
590 tprintf("Dark background. ");
591 holdit();
592
593 reset_all_colors();
594 return MENU_NOHOLD;
595 }
596
597 /*
598 */
599 static int
600 test_SGR_0(MENU_ARGS)
601 {
602 vt_move(1, 1);
603 println(the_title);
604 println("");
605 println("ECMA-48 states that SGR 0 \"cancels the effect of any preceding occurrence");
606 println("of SGR in the data stream regardless of the setting of the graphic rendition");
607 println("combination mode (GRCM)\".");
608 println("");
609 println("");
610
611 reset_all_colors();
612 printxx("You should see only black:");
613 sgr("30;40");
614 tprintf("SGR 30 and SGR 40 don't work");
615 reset_all_colors();
616 println(":up to here");
617
618 reset_all_colors();
619 printxx("You should see only white:");
620 sgr("37;47");
621 tprintf("SGR 37 and SGR 47 don't work");
622 reset_all_colors();
623 println(":up to here");
624
625 reset_all_colors();
626 printxx("You should see text here: ");
627 sgr("30;40");
628 sgr("0");
629 tprintf("SGR 0 reset works (explicit 0)");
630 println("");
631
632 reset_all_colors();
633 printxx("................and here: ");
634 sgr("37;47");
635 sgr("");
636 tprintf("SGR 0 reset works (default param)");
637 println("");
638
639 reset_all_colors();
640 holdit();
641 return MENU_NOHOLD;
642 }
643
644 /*
645 * Allow user to test the same screens w/o colors.
646 */
647 int
648 toggle_color_mode(MENU_ARGS)
649 {
650 do_colors = !do_colors;
651 return MENU_NOHOLD;
652 }
653
654 /*
655 * VT100s of course never did colors, ANSI or otherwise. This test is for
656 * xterm.
657 */
658 static int
659 test_vt100_colors(MENU_ARGS)
660 {
661 /* *INDENT-OFF* */
662 static MENU colormenu[] = {
663 { "Exit", NULL },
664 { "Test of cursor movements", test_color_movements },
665 { "Test of screen features", test_color_screen },
666 { "Test Insert/Delete Char/Line", test_color_insdel, },
667 { "", NULL }
668 };
669 /* *INDENT-ON* */
670
671 int save_colors = do_colors;
672
673 do_colors = TRUE;
674
675 do {
676 vt_clear(2);
677 __(title(0), println("Test VT102-style features with BCE"));
678 __(title(2), println("Choose test type:"));
679 } while (menu(colormenu));
680
681 do_colors = save_colors;
682
683 return MENU_NOHOLD;
684 }
685
686 /*
687 * For terminals that support ANSI/ISO colors, work through a graduated
688 * set of tests that first display colors (if the terminal does indeed
689 * support them), then exercise the associated reset, clear operations.
690 */
691 int
692 tst_colors(MENU_ARGS)
693 {
694 /* *INDENT-OFF* */
695 static MENU colormenu[] = {
696 { "Exit", NULL },
697 { txt_override_color, toggle_color_mode, },
698 { "Display color test-pattern", show_test_pattern, },
699 { "Test SGR-0 color reset", test_SGR_0, },
700 { "Test BCE-style clear line/display (ED, EL)", simple_bce_test, },
701 { "Test BCE-style clear line/display (ECH, Indexing)", fancy_bce_test, },
702 { "Test of VT102-style features with BCE", test_vt100_colors, },
703 { "Test other ISO-6429 features with BCE", test_ecma48_misc },
704 { "Test screen features with BCE", test_bce_color, },
705 { "Test screen features with ISO 6429 SGR 22-27 codes", test_iso_6429_sgr, },
706 { "", NULL }
707 };
708 /* *INDENT-ON* */
709
710 int save_colors = do_colors;
711
712 do_colors = TRUE;
713
714 do {
715 vt_clear(2);
716 sprintf(txt_override_color, "%s color-switching", STR_ENABLE(do_colors));
717 __(title(0), println("ISO 6429 colors"));
718 __(title(2), println("Choose test type:"));
719 } while (menu(colormenu));
720
721 do_colors = save_colors;
722
723 return MENU_NOHOLD;
724 }