]> git.lizzy.rs Git - nothing.git/blob - src/color.c
Introduce color256 constructor (#98)
[nothing.git] / src / color.c
1 #include <SDL2/SDL.h>
2
3 #include "./color.h"
4
5 color_t color(float r, float g, float b, float a)
6 {
7     const color_t result = {
8         .r = r,
9         .g = g,
10         .b = b,
11         .a = a
12     };
13
14     return result;
15 }
16
17 color_t color256(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
18 {
19     return color(
20         (float) r / 255.0f,
21         (float) g / 255.0f,
22         (float) b / 255.0f,
23         (float) a / 255.0f);
24 }
25
26 SDL_Color color_for_sdl(color_t color)
27 {
28     const SDL_Color result = {
29         .r = (Uint8)roundf(color.r * 255.0f),
30         .g = (Uint8)roundf(color.g * 255.0f),
31         .b = (Uint8)roundf(color.b * 255.0f),
32         .a = (Uint8)roundf(color.a * 255.0f)
33     };
34
35     return result;
36 }
37
38 color_t color_desaturate(color_t c)
39 {
40     const float k = (c.r + c.g + c.b) / 3.0f;
41     return color(k, k, k, c.a);
42 }