]> git.lizzy.rs Git - nothing.git/blob - src/color.c
Merge pull request #927 from tsoding/788
[nothing.git] / src / color.c
1 #include <SDL.h>
2 #include <string.h>
3
4 #include "color.h"
5
6 Color rgba(float r, float g, float b, float a)
7 {
8     const Color result = {
9         .r = r,
10         .g = g,
11         .b = b,
12         .a = a
13     };
14
15     return result;
16 }
17
18 /* TODO(#928): Should the Hue in HSLA representation be in degrees? */
19 Color hsla(float h, float s, float l, float a)
20 {
21     h = fmodf(h, 360.0f);
22
23     const float c = (1.0f - fabsf(2.0f * l - 1.0f)) * s;
24     const float x = c * (1 - fabsf(fmodf(h / 60.0f, 2.0f) - 1.0f));
25     const float m = l - c / 2.0f;
26
27     Color color = {0.0f, 0.0f, 0.0f, a};
28
29     if (0.0f <= h && h < 60.0f) {
30         color = rgba(c, x, 0.0f, a);
31     } else if (60.0f <= h && h < 120.0f) {
32         color = rgba(x, c, 0.0f, a);
33     } else if (120.0f <= h && h < 180.0f) {
34         color = rgba(0.0f, c, x, a);
35     } else if (180.0f <= h && h < 240.0f) {
36         color = rgba(0.0f, x, c, a);
37     } else if (240.0f <= h && h < 300.0f) {
38         color = rgba(x, 0.0f, c, a);
39     } else if (300.0f <= h && h < 360.0f) {
40         color = rgba(c, 0.0f, x, a);
41     }
42
43     color.r += m;
44     color.g += m;
45     color.b += m;
46
47     return color;
48 }
49
50 static Uint8 hex2dec_digit(char c)
51 {
52     if (c >= '0' && c <= '9') {
53         return (Uint8) (c - '0');
54     }
55
56     if (c >= 'A' && c <= 'F') {
57         return (Uint8) (10 + c - 'A');
58     }
59
60     if (c >= 'a' && c <= 'f') {
61         return (Uint8) (10 + c - 'a');
62     }
63
64     return 0;
65 }
66
67 static Uint8 parse_color_component(const char *component)
68 {
69     return (Uint8) (hex2dec_digit(component[0]) * 16 + hex2dec_digit(component[1]));
70 }
71
72 Color hexstr(const char *hexstr)
73 {
74     if (strlen(hexstr) != 6) {
75         return rgba(0.0f, 0.0f, 0.0f, 1.0f);
76     }
77
78     return rgba(
79         parse_color_component(hexstr) / 255.0f,
80         parse_color_component(hexstr + 2) / 255.0f,
81         parse_color_component(hexstr + 4) / 255.0f,
82         1.0f);
83 }
84
85 SDL_Color color_for_sdl(Color color)
86 {
87     const SDL_Color result = {
88         .r = (Uint8)roundf(color.r * 255.0f),
89         .g = (Uint8)roundf(color.g * 255.0f),
90         .b = (Uint8)roundf(color.b * 255.0f),
91         .a = (Uint8)roundf(color.a * 255.0f)
92     };
93
94     return result;
95 }
96
97 Color color_desaturate(Color c)
98 {
99     const float k = (c.r + c.g + c.b) / 3.0f;
100     return rgba(k, k, k, c.a);
101 }
102
103 Color color_darker(Color c, float d)
104 {
105     return rgba(fmaxf(c.r - d, 0.0f),
106                 fmaxf(c.g - d, 0.0f),
107                 fmaxf(c.b - d, 0.0f),
108                 c.a);
109 }
110
111 Color color_invert(Color c)
112 {
113     return rgba(1.0f - c.r, 1.0f - c.g, 1.0f - c.b, c.a);
114 }
115
116 Color color_scale(Color c, Color fc)
117 {
118     return rgba(
119         fmaxf(fminf(c.r * fc.r, 1.0f), 0.0f),
120         fmaxf(fminf(c.g * fc.g, 1.0f), 0.0f),
121         fmaxf(fminf(c.b * fc.b, 1.0f), 0.0f),
122         fmaxf(fminf(c.a * fc.a, 1.0f), 0.0f));
123 }
124
125 int color_hex_to_stream(Color color, FILE *stream)
126 {
127     SDL_Color sdl = color_for_sdl(color);
128     return fprintf(stream, "%02x%02x%02x", sdl.r, sdl.g, sdl.b);
129 }
130
131 Color rgba_to_hsla(Color color)
132 {
133     const float max = fmaxf(color.r, fmaxf(color.g, color.b));
134     const float min = fminf(color.r, fminf(color.g, color.b));
135     const float c = max - min;
136     float hue = 0.0f;
137     float saturation = 0.0f;
138     const float lightness = (max + min) * 0.5f;
139
140     if (fabsf(c) > 1e-6) {
141         if (fabs(max - color.r) <= 1e-6) {
142             hue = 60.0f * fmodf((color.g - color.b) / c, 6.0f);
143         } else if (fabs(max - color.g) <= 1e-6) {
144             hue = 60.0f * ((color.b - color.r) / c + 2.0f);
145         } else {
146             hue = 60.0f * ((color.r - color.g) / c + 4.0f);
147         }
148
149         saturation = c / (1.0f - fabsf(2.0f * lightness - 1.0f));
150     }
151
152     // TODO(#929): Color struct is used not only for RGBA
153     //   But also for HSLA. We should make another similar struct but for HSLA
154     return rgba(hue, saturation, lightness, color.a);
155 }