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