-
Notifications
You must be signed in to change notification settings - Fork 2
/
Colorname.c
96 lines (81 loc) · 1.82 KB
/
Colorname.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Parse rgb.txt file and return color by name
*/
#include "nxlib.h"
#include <stdlib.h>
#include <string.h>
#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
#define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
#define ISEOL(c) ((c) == '\r' || (c) == '\n')
/* get next int from buffer, return next buf position*/
static char *
strInt(unsigned int *retint, char *buf)
{
char * q;
char tmp[256];
while (ISBLANK(*buf))
++buf;
q = tmp;
while (ISDIGIT(*buf))
*q++ = *buf++;
*q = 0;
*retint = atoi(tmp);
return buf;
}
/* return string to end of line from buffer*/
static void
strEol(char *retbuf, char *buf)
{
while (ISBLANK(*buf))
++buf;
while (!ISEOL(*buf))
*retbuf++ = *buf++;
*retbuf = 0;
}
GR_COLOR
GrGetColorByName(char *colorname, int *retr, int *retg, int *retb)
{
FILE *fp;
unsigned int r = 0, g = 0, b = 0;
char buf[256];
fp = fopen(X11_RGBTXT, "r");
if (!fp)
return 0;
while (fgets(buf, 256, fp) != NULL) {
if (buf[0] != '!') {
char *p;
char name[256];
p = strInt(&r, buf);
p = strInt(&g, p);
p = strInt(&b, p);
strEol(name, p);
if (strcasecmp(name, colorname) == 0) {
if (retr)
*retr = r;
if (retg)
*retg = g;
if (retb)
*retb = b;
return GR_RGB(r, g, b);
}
}
}
fclose(fp);
return 0;
}
Status
XAllocNamedColor(Display * dpy, Colormap cmap, _Xconst char *colorname,
XColor * hard_def, XColor * exact_def)
{
GR_COLOR c;
int r = 0, g = 0, b = 0;
/* first look up color in rgb.txt color database */
c = GrGetColorByName((char *) colorname, &r, &g, &b);
//DPRINTF("XAllocNamedColor %s = %x\n", colorname, (unsigned int) c);
hard_def->red = exact_def->red = r << 8;
hard_def->green = exact_def->green = g << 8;
hard_def->blue = exact_def->blue = b << 8;
/* Do an XAllocColor on the hardware color */
XAllocColor(dpy, cmap, hard_def);
return 1;
}