tr: missing casts for isdigit()
[neatroff.git] / clr.c
blob91ca560d51163edbda94938599886620f2e64e5f
1 /* color management */
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include "roff.h"
8 static struct color {
9 char *name;
10 int value;
11 } colors[] = {
12 {"black", CLR_RGB(0, 0, 0)},
13 {"red", CLR_RGB(0xff, 0, 0)},
14 {"green", CLR_RGB(0, 0xff, 0)},
15 {"yellow", CLR_RGB(0xff, 0xff, 0)},
16 {"blue", CLR_RGB(0, 0, 0xff)},
17 {"magenta", CLR_RGB(0xff, 0, 0xff)},
18 {"cyan", CLR_RGB(0, 0xff, 0xff)},
19 {"white", CLR_RGB(0xff, 0xff, 0xff)},
22 /* returns a static buffer */
23 char *clr_str(int c)
25 static char clr_buf[32];
26 if (!c)
27 return "0";
28 sprintf(clr_buf, "#%02x%02x%02x", CLR_R(c), CLR_G(c), CLR_B(c));
29 return clr_buf;
32 /* read color component */
33 static int ccom(char *s, int len)
35 static char *digs = "0123456789abcdef";
36 int n = 0;
37 int i;
38 for (i = 0; i < len; i++)
39 if (strchr(digs, tolower(s[i])))
40 n = n * 16 + (strchr(digs, tolower(s[i])) - digs);
41 return len == 1 ? n * 255 / 15 : n;
44 int clr_get(char *s)
46 int c = (unsigned char) s[0];
47 int i;
48 if (c == '#' && strlen(s) == 7)
49 return CLR_RGB(ccom(s + 1, 2), ccom(s + 3, 2), ccom(s + 5, 2));
50 if (c == '#' && strlen(s) == 4)
51 return CLR_RGB(ccom(s + 1, 1), ccom(s + 2, 1), ccom(s + 3, 1));
52 if (c == '#' && strlen(s) == 3)
53 return CLR_RGB(ccom(s + 1, 2), ccom(s + 1, 2), ccom(s + 1, 2));
54 if (c == '#' && strlen(s) == 2)
55 return CLR_RGB(ccom(s + 1, 1), ccom(s + 1, 1), ccom(s + 1, 1));
56 if (isdigit(c) && atoi(s) >= 0 && atoi(s) < LEN(colors))
57 return colors[atoi(s)].value;
58 for (i = 0; i < LEN(colors); i++)
59 if (!strcmp(colors[i].name, s))
60 return colors[i].value;
61 return 0;