-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.c
59 lines (51 loc) · 1.41 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rgba/rgba.h>
#include <glisy/color.h>
void
glisy_color_init(glisy_color *color, const char *name, uint32_t value) {
rgba_t rgba;
uint32_t tmp;
if (!color) return;
if (!color->name) {
color->name = "";
}
if (!color->string) {
color->string = "";
}
if (name && value == 0) {
if ('#' == name[0]) {
color->string = strdup(name);
} else if (name == strstr(name, "rgb")) {
color->string = strdup(name);
} else {
color->name = strdup(name);
}
short ok = 0;
tmp = rgba_from_string(name, &ok);
if (tmp) value = tmp;
}
rgba = rgba_new(value);
color->r = rgba.r;
color->g = rgba.g;
color->b = rgba.b;
color->a = rgba.a;
color->value = value;
}
const char *
glisy_color_to_name(const glisy_color color) {
if (strlen(color.name)) return strdup(color.name);
if (strlen(color.string)) return strdup(color.string);
rgba_t rgba = (rgba_t) { color.r, color.g, color.b, color.a };
rgba_to_string(rgba, (char *) color.name, BUFSIZ);
rgba_to_string(rgba, (char *) color.string, BUFSIZ);
return strdup(color.name);
}
const char *
glisy_color_to_string(const glisy_color color) {
if (strlen(color.string)) return strdup(color.string);
rgba_t rgba = (rgba_t) { color.r, color.g, color.b, color.a };
rgba_to_string(rgba, (char *) color.string, BUFSIZ);
return strdup(color.string);
}