xref: /netbsd-src/games/colorbars/colorbars.c (revision fdd524d4ccd2bb0c6f67401e938dabf773eb0372)
1 /*	$NetBSD: colorbars.c,v 1.1 2012/06/06 00:13:36 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2012 Nathanial Sloss <nathanialsloss@yahoo.com.au>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: colorbars.c,v 1.1 2012/06/06 00:13:36 christos Exp $");
30 
31 #include <curses.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <err.h>
35 #include <stdlib.h>
36 
37 int
38 main(void)
39 {
40 	static struct colorInfo {
41 		const char *name;
42 		int color;
43 	} colorInfo[] = {
44 		{ "Black", COLOR_BLACK },
45 		{ "Red", COLOR_RED },
46 		{ "Green", COLOR_GREEN },
47 		{ "Yellow", COLOR_YELLOW },
48 		{ "Blue", COLOR_BLUE },
49 		{ "Magenta", COLOR_MAGENTA },
50 		{ "Cyan", COLOR_CYAN },
51 		{ "White", COLOR_WHITE },
52 	};
53 	size_t lengths[__arraycount(colorInfo)];
54 
55 	static const size_t numcolors = __arraycount(colorInfo);
56 	size_t labelwidth;
57 
58 	int colorOK;
59 	int spacing, offsetx, labeloffsety, labeloffsetx;
60 
61 	if (!initscr())
62 		errx(EXIT_FAILURE, "Cannot initialize curses");
63 
64 	colorOK = has_colors();
65 	if (!colorOK) {
66 		endwin();
67 		errx(EXIT_FAILURE, "Terminal cannot display color");
68 	}
69 
70 	if (COLS < 45 || LINES < 10) {
71 		endwin();
72 		errx(EXIT_FAILURE, "Terminal size must be at least 45x10.");
73 	}
74 
75 	spacing = COLS / numcolors;
76 	offsetx = (COLS - (spacing * numcolors)) / 2;
77 
78 
79 	start_color();
80 
81 	labelwidth = 0;
82 	for (size_t i = 0; i < numcolors; i++) {
83 		lengths[i] = strlen(colorInfo[i].name);
84 		if (lengths[i] > labelwidth)
85 			labelwidth = lengths[i];
86 		init_pair(i, COLOR_WHITE, colorInfo[i].color);
87 	}
88 
89 	labeloffsetx = spacing / 2;
90 	labeloffsety = (LINES - 1 - labelwidth) / 2;
91 	clear();
92 
93 	move(0, 0);
94 
95 	for (size_t i = 0; i < numcolors; i++) {
96 		int xoffs = offsetx + spacing * i;
97 
98 		attrset(COLOR_PAIR(i));
99 
100 		for (int line = 0; line < LINES - 1; line++)
101 			for (int xpos = 0; xpos < spacing; xpos++)
102 			       mvprintw(line, xoffs + xpos, " ");
103 
104 		attrset(COLOR_PAIR(0));
105 
106 		xoffs += labeloffsetx;
107 		for (size_t line = 0; line < lengths[i]; line++)
108 			mvprintw(line + labeloffsety, xoffs, "%c",
109 			    colorInfo[i].name[line]);
110 	}
111 
112 	attrset(COLOR_PAIR(0));
113 
114 	mvprintw(LINES - 1, 0, "ANSI Color chart - Press any key to exit: ");
115 
116 	refresh();
117 
118 	getch();
119 
120 	endwin();
121 
122 	return EXIT_SUCCESS;
123 }
124 
125