-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess-rgb-color.c
74 lines (71 loc) · 2.52 KB
/
process-rgb-color.c
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
/*** file process-rgb-color.c from https://github.com/bstarynk/misc-basile/ ***/
/*** SPDX-License-Identifier: GPLv3+ ***/
/*******************************************************************************
* © Copyright 2024 Basile STARYNKEVITCH (France)
* email <[email protected]>
* License: GPLv3+ (file COPYING-GPLv3)
* This software is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
char *progname;
int
main (int argc, char **argv)
{
char linebuf[128];
char colorbuf[80];
progname = argv[0];
if (argc > 1 && !strcmp (argv[1], "--help"))
{
printf
("%s: process the /etc/X11/rgb.txt file and emit on stdout C macro expressions\n"
"RPS_RGB_COLOR(Red,Green,Blue,\"colorname\");\n", progname);
return 0;
}
char *rgbfilename = "/etc/X11/rgb.txt";
if (argc > 1 && !access (argv[1], R_OK))
rgbfilename = argv[1];
FILE *rgbf = fopen (rgbfilename, "r");
if (!rgbf)
{
fprintf (stderr, "%s: cannot fopen %s - %s\n", progname, rgbfilename,
strerror (errno));
exit (EXIT_FAILURE);
};
int nbcolors = 0;
int widestcolor = 0;
do
{
memset (linebuf, 0, sizeof (linebuf));
memset (colorbuf, 0, sizeof (colorbuf));
if (!fgets (linebuf, sizeof (linebuf) - 4, rgbf))
break;
int r = 0, g = 0, b = 0, i = 0;
if (sscanf (linebuf, "%d %d %d %64[A-Za-z0-9 ]%n",
&r, &g, &b, colorbuf, &i) <= 3)
continue;
if (widestcolor < (int) strlen (colorbuf))
widestcolor = (int) strlen (colorbuf);
nbcolors++;
printf ("RPS_RGB_COLOR(%3d,%3d,%3d,\"%s\")\n", r, g, b, colorbuf);
}
while (!feof (rgbf));
printf ("#define RPS_NB_RGB_COLORS %d\n", nbcolors);
printf ("#define RPS_WIDEST_RGB_COLOR %d /*bytes*/\n", widestcolor);
printf ("/// end of colors\n");
return 0;
}