7 Color rgba(float r, float g, float b, float a)
19 /* TODO(#928): Should the Hue in HSLA representation be in degrees? */
20 Color hsla(float h, float s, float l, float a)
24 const float c = (1.0f - fabsf(2.0f * l - 1.0f)) * s;
25 const float x = c * (1 - fabsf(fmodf(h / 60.0f, 2.0f) - 1.0f));
26 const float m = l - c / 2.0f;
28 Color color = {0.0f, 0.0f, 0.0f, a};
30 if (0.0f <= h && h < 60.0f) {
31 color = rgba(c, x, 0.0f, a);
32 } else if (60.0f <= h && h < 120.0f) {
33 color = rgba(x, c, 0.0f, a);
34 } else if (120.0f <= h && h < 180.0f) {
35 color = rgba(0.0f, c, x, a);
36 } else if (180.0f <= h && h < 240.0f) {
37 color = rgba(0.0f, x, c, a);
38 } else if (240.0f <= h && h < 300.0f) {
39 color = rgba(x, 0.0f, c, a);
40 } else if (300.0f <= h && h < 360.0f) {
41 color = rgba(c, 0.0f, x, a);
51 static Uint8 hex2dec_digit(char c)
53 if (c >= '0' && c <= '9') {
54 return (Uint8) (c - '0');
57 if (c >= 'A' && c <= 'F') {
58 return (Uint8) (10 + c - 'A');
61 if (c >= 'a' && c <= 'f') {
62 return (Uint8) (10 + c - 'a');
68 static Uint8 parse_color_component(const char *component)
70 return (Uint8) (hex2dec_digit(component[0]) * 16 + hex2dec_digit(component[1]));
73 Color hexstr(const char *hexstr)
75 if (strlen(hexstr) != 6) {
76 return rgba(0.0f, 0.0f, 0.0f, 1.0f);
80 parse_color_component(hexstr) / 255.0f,
81 parse_color_component(hexstr + 2) / 255.0f,
82 parse_color_component(hexstr + 4) / 255.0f,
86 SDL_Color color_for_sdl(Color color)
88 const SDL_Color result = {
89 .r = (Uint8)roundf(color.r * 255.0f),
90 .g = (Uint8)roundf(color.g * 255.0f),
91 .b = (Uint8)roundf(color.b * 255.0f),
92 .a = (Uint8)roundf(color.a * 255.0f)
98 Color color_desaturate(Color c)
100 const float k = (c.r + c.g + c.b) / 3.0f;
101 return rgba(k, k, k, c.a);
104 Color color_darker(Color c, float d)
106 return rgba(fmaxf(c.r - d, 0.0f),
107 fmaxf(c.g - d, 0.0f),
108 fmaxf(c.b - d, 0.0f),
112 Color color_invert(Color c)
114 return rgba(1.0f - c.r, 1.0f - c.g, 1.0f - c.b, c.a);
117 Color color_scale(Color c, Color fc)
120 fmaxf(fminf(c.r * fc.r, 1.0f), 0.0f),
121 fmaxf(fminf(c.g * fc.g, 1.0f), 0.0f),
122 fmaxf(fminf(c.b * fc.b, 1.0f), 0.0f),
123 fmaxf(fminf(c.a * fc.a, 1.0f), 0.0f));
126 int color_hex_to_stream(Color color, FILE *stream)
128 SDL_Color sdl = color_for_sdl(color);
129 return fprintf(stream, "%02x%02x%02x", sdl.r, sdl.g, sdl.b);
132 Color rgba_to_hsla(Color color)
134 const float max = fmaxf(color.r, fmaxf(color.g, color.b));
135 const float min = fminf(color.r, fminf(color.g, color.b));
136 const float c = max - min;
138 float saturation = 0.0f;
139 const float lightness = (max + min) * 0.5f;
141 if (fabsf(c) > 1e-6) {
142 if (fabs(max - color.r) <= 1e-6) {
143 hue = 60.0f * fmodf((color.g - color.b) / c, 6.0f);
144 } else if (fabs(max - color.g) <= 1e-6) {
145 hue = 60.0f * ((color.b - color.r) / c + 2.0f);
147 hue = 60.0f * ((color.r - color.g) / c + 4.0f);
150 saturation = c / (1.0f - fabsf(2.0f * lightness - 1.0f));
153 // TODO(#929): Color struct is used not only for RGBA
154 // But also for HSLA. We should make another similar struct but for HSLA
155 return rgba(hue, saturation, lightness, color.a);