-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclr.c
56 lines (52 loc) · 1.33 KB
/
clr.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
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "post.h"
/* returns a static buffer */
char *clr_str(int c)
{
static char clr_buf[32];
if (!c)
return "0";
sprintf(clr_buf, "#%02x%02x%02x", CLR_R(c), CLR_G(c), CLR_B(c));
return clr_buf;
}
static struct color {
char *name;
int value;
} colors[] = {
{"black", CLR_RGB(0, 0, 0)},
{"red", CLR_RGB(0xff, 0, 0)},
{"green", CLR_RGB(0, 0xff, 0)},
{"yellow", CLR_RGB(0xff, 0xff, 0)},
{"blue", CLR_RGB(0, 0, 0xff)},
{"magenta", CLR_RGB(0xff, 0, 0xff)},
{"cyan", CLR_RGB(0, 0xff, 0xff)},
{"white", CLR_RGB(0xff, 0xff, 0xff)},
};
/* read color component */
static int clrcomp(char *s, int len)
{
static char *digs = "0123456789abcdef";
int n = 0;
int i;
for (i = 0; i < len; i++)
if (strchr(digs, tolower(s[i])))
n = n * 16 + (strchr(digs, tolower(s[i])) - digs);
return len == 1 ? n * 255 / 15 : n;
}
int clr_get(char *s)
{
int i;
if (s[0] == '#' && strlen(s) == 7)
return CLR_RGB(clrcomp(s + 1, 2), clrcomp(s + 3, 2), clrcomp(s + 5, 2));
if (s[0] == '#' && strlen(s) == 4)
return CLR_RGB(clrcomp(s + 1, 1), clrcomp(s + 2, 1), clrcomp(s + 3, 1));
if (isdigit(s[0]) && atoi(s) >= 0 && atoi(s) < LEN(colors))
return colors[atoi(s)].value;
for (i = 0; i < LEN(colors); i++)
if (!strcmp(colors[i].name, s))
return colors[i].value;
return 0;
}