forked from cbdevnet/xecho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorspec.c
50 lines (43 loc) · 1.27 KB
/
colorspec.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
unsigned short colorspec_read_byte(char* cs){
char* hexmap="0123456789abcdef";
unsigned short rv=0;
int i;
if(*cs!=0&&cs[1]!=0){
for(i=0;hexmap[i]!=0&&hexmap[i]!=cs[0];i++){
}
rv|=(i<<12);
for(i=0;hexmap[i]!=0&&hexmap[i]!=cs[1];i++){
}
rv|=(i<<8);
}
return rv;
}
XftColor colorspec_parse(char* cs, Display* display, int screen){
XftColor rv;
XRenderColor xrender_color={0,0,0,0xffff};
int i;
if(*cs=='#'){
if(strlen(cs)!=7){
fprintf(stderr, "Invalid colorspec length\n");
}
for(i=1;i<strlen(cs);i++){
if(!isxdigit(cs[i])){
fprintf(stderr, "Invalid digit in colorspec: %c\n", cs[i]);
return rv;
}
}
xrender_color.red=colorspec_read_byte(cs+1);
xrender_color.green=colorspec_read_byte(cs+3);
xrender_color.blue=colorspec_read_byte(cs+5);
fprintf(stderr, "Read colorspec %s as r:%04x g:%04x b:%04x\n", cs, xrender_color.red, xrender_color.green, xrender_color.blue);
if(!XftColorAllocValue(display, DefaultVisual(display, screen), DefaultColormap(display, screen), &xrender_color, &rv)){
fprintf(stderr, "Failed to allocate color\n");
}
}
else{
if(!XftColorAllocName(display, DefaultVisual(display, screen), DefaultColormap(display, screen), cs, &rv)){
fprintf(stderr, "Failed to get color by name\n");
}
}
return rv;
}