6 Color rgba(float r, float g, float b, float a)
18 static Uint8 hex2dec_digit(char c)
20 if (c >= '0' && c <= '9') {
21 return (Uint8) (c - '0');
24 if (c >= 'A' && c <= 'F') {
25 return (Uint8) (10 + c - 'A');
28 if (c >= 'a' && c <= 'f') {
29 return (Uint8) (10 + c - 'a');
35 static Uint8 parse_color_component(const char *component)
37 return (Uint8) (hex2dec_digit(component[0]) * 16 + hex2dec_digit(component[1]));
40 Color hexstr(const char *hexstr)
42 if (strlen(hexstr) != 6) {
43 return rgba(0.0f, 0.0f, 0.0f, 1.0f);
47 parse_color_component(hexstr) / 255.0f,
48 parse_color_component(hexstr + 2) / 255.0f,
49 parse_color_component(hexstr + 4) / 255.0f,
53 SDL_Color color_for_sdl(Color color)
55 const SDL_Color result = {
56 .r = (Uint8)roundf(color.r * 255.0f),
57 .g = (Uint8)roundf(color.g * 255.0f),
58 .b = (Uint8)roundf(color.b * 255.0f),
59 .a = (Uint8)roundf(color.a * 255.0f)
65 Color color_desaturate(Color c)
67 const float k = (c.r + c.g + c.b) / 3.0f;
68 return rgba(k, k, k, c.a);
71 Color color_darker(Color c, float d)
73 return rgba(fmaxf(c.r - d, 0.0f),