-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.c
69 lines (56 loc) · 1.45 KB
/
color.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
// カラーモジュール
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include <gc.h>
typedef struct {
char *name;
XColor color;
} XColorEntry;
typedef struct {
char *name;
XftColor color;
} XftColorEntry;
#define MAX_COLORS 1024
// static XColorEntry x_color_table[1024];
// static size_t num_x_colors;
static XftColorEntry xft_color_table[1024];
static size_t num_xft_colors;
static Display *disp;
static Colormap colormap;
static Visual *visual;
void ColorInitialize(Display *aDisp)
{
disp = aDisp;
colormap = DefaultColormap(disp, DefaultScreen(disp));
visual = DefaultVisual(disp, DefaultScreen(disp));
}
bool ColorIsInitialized()
{
return disp != NULL;
}
unsigned long ColorGetPixel(const char *name)
{
assert(ColorIsInitialized());
XColor color;
XParseColor(disp, colormap, name, &color);
XAllocColor(disp, colormap, &color);
return color.pixel;
}
XftColor *ColorGetXftColor(const char *name)
{
for (int i = 0; i < num_xft_colors; i++) {
if (strcmp(xft_color_table[i].name, name) == 0) {
return &xft_color_table[i].color;
}
}
if (num_xft_colors == MAX_COLORS) {
fprintf(stderr, "color table full");
exit(1);
}
xft_color_table[num_xft_colors].name = GC_STRDUP(name);
XftColorAllocName(disp, visual, colormap, name, &xft_color_table[num_xft_colors].color);
return &xft_color_table[num_xft_colors++].color;
}