2 * Test app for _NET_WM_ICON formatting.
3 * Copyright © 2022 Nick Bowler
5 * Use a fake colour scheme to generate an icon of the chosen size (16x16,
6 * 24x24, 32x32 or 48x48) and display the pixels as characters.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
32 static const char *progname = "ewmhicon";
33 static const char sopts[] = "c:VH";
34 static const struct option lopts[] = {
35 { "colourseq", 1, NULL, 'c' },
36 { "version", 0, NULL, 'V' },
37 { "help", 0, NULL, 'H' },
41 #define S8TO16(x) ((x) * 0xfffful / 0xff)
42 #define RGB8(r, g, b) { \
43 0xff000000|(r ## ul)<<16|(g ## ul)<<8|(b ## ul), \
44 S8TO16(r), S8TO16(g), S8TO16(b) }
46 static const XColor colours[7][3] = {
48 { 0xffff0000, 0xffff },
49 { 0xff00ff00, 0, 0xffff },
50 { 0xff0000ff, 0, 0, 0xffff }
52 { RGB8(0x8d,0x2e,0x28), RGB8(0x6a,0x1b,0x17), RGB8(0xa1,0x48,0x42) },
53 { RGB8(0xb4,0x6e,0x28), RGB8(0x92,0x4a,0x16), RGB8(0xc7,0x90,0x4f) },
54 { RGB8(0xd8,0xb7,0x40), RGB8(0xc5,0x9f,0x39), RGB8(0xe2,0xc6,0x5d) },
55 { RGB8(0x28,0x64,0x28), RGB8(0x19,0x47,0x19), RGB8(0x4e,0x87,0x4e) },
56 { RGB8(0x00,0x34,0x71), RGB8(0x00,0x1f,0x4f), RGB8(0x00,0x52,0x8b) },
57 { RGB8(0xdc,0xdc,0xdc), RGB8(0xc0,0xc0,0xc0), RGB8(0xea,0xea,0xea) }
60 static void print_usage(FILE *f)
62 fprintf(f, "Usage: %s size\n", progname);
64 fprintf(f, "Try %s --help for more information.\n", progname);
67 static void print_help(void)
69 const struct option *opt;
74 for (opt = lopts; opt->name; opt++) {
75 if (help_print_optstring(opt, "ARG", 20))
80 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
83 static void print_version(void)
85 printf("ewmhicon (%s) %s\n", PACKAGE, PACKAGE_VERSION);
86 printf("Copyright (C) 2022 Nick Bowler\n");
87 puts("License GPLv3+: GNU GPL version 3 or any later version");
88 puts("This is free software: you are free to change and redistribute it.");
89 puts("There is NO WARRANTY, to the extent permitted by law.");
92 static const char sizes[][6] = {
93 "16x16", "24x24", "32x32", "48x48"
96 static int to_size_enum(const char *arg)
101 if (!strchr(arg, 'x')) {
102 sprintf(buf, "%.3sx%.3s", arg, arg);
106 for (i = 0; i < sizeof sizes / sizeof sizes[0]; i++) {
107 if (!strcmp(arg, sizes[i]))
114 static void gen_icon(int size, const char *colourseq)
116 void (*tilefunc)(unsigned long *, const XColor *, int, int);
117 static unsigned long icon_buf[48*48];
118 const char colourchars[21] = ".%+"",Rr""-Oo""'Yy"":Gg"";Bb"" Ww";
119 unsigned i, j, x, y, w, h, n;
122 case ICON_16x16: tilefunc = ewmh_tile16; w = h = 16; break;
123 case ICON_24x24: tilefunc = ewmh_tile24; w = h = 24; break;
124 case ICON_32x32: tilefunc = ewmh_tile32; w = h = 32; break;
125 case ICON_48x48: tilefunc = ewmh_tile48; w = h = 48; break;
129 for (i = 0; i < 9; i++) {
132 assert(colourseq[i] >= '0' && colourseq[i] <= '6');
133 c = colours[colourseq[i]-'0'];
135 tilefunc(icon_buf, c, i%3, i/3);
139 for (i = 0; i < 7; i++)
140 n -= !strchr(colourseq, '0'+i);
143 puts("static char *icon[] = {");
144 printf("\"%u %u %u 1\",\n", w, h, 3*n);
145 for (i = 0; i < 7; i++) {
146 if (!strchr(colourseq, '0'+i))
149 for (j = 0; j < 3; j++) {
150 printf("\"%c c #%.6lx\",\n", colourchars[3*i+j],
151 colours[i][j].pixel & 0xffffff);
155 for (y = 0; y < h; y++) {
157 for (x = 0; x < w; x++) {
158 unsigned long val = icon_buf[y*h+x];
161 for (i = 0; i < sizeof colourchars; i++) {
162 if (colours[i/3][i%3].pixel == val) {
170 printf("\"%.*s\n", y+1<h, ",");
175 int decode_colourseq(char *out, const char *arg)
179 for (i = 0; i < 9; i++) {
182 case 'R': case 'r': out[i] = '1'; break;
183 case 'O': case 'o': out[i] = '2'; break;
184 case 'Y': case 'y': out[i] = '3'; break;
185 case 'G': case 'g': out[i] = '4'; break;
186 case 'B': case 'b': out[i] = '5'; break;
187 case 'W': case 'w': out[i] = '6'; break;
188 case '1': case '2': case '3': case '4': case '5': case '6':
189 case '0': out[i] = arg[i]; break;
196 fprintf(stderr, "%s: error: invalid colour sequence '%s'\n",
204 int main(int argc, char **argv)
206 char colourseq[9] = "000000000";
212 while ((opt = getopt_long(argc, argv, sopts, lopts, 0)) != -1) {
215 if (decode_colourseq(colourseq, optarg) != 0)
230 if (argc != optind+1) {
231 printf("%s: error: size not specified\n", progname);
236 size = to_size_enum(argv[optind]);
238 printf("%s: error: invalid size %s\n", progname, argv[optind]);
243 gen_icon(size, colourseq);