PixelKey
NeoPixel USB Key
Loading...
Searching...
No Matches
color.h
1#ifndef COLOR_H
2#define COLOR_H
3
4#include <assert.h>
5#include <stdint.h>
6#include <stdbool.h>
7#include <stddef.h>
8
16#define RGB_MAX (255U)
18#define RGB_MAX_F32 (255.0f)
20#define RGB_RANGE (256U)
22#define RGB_RANGE_F32 (256.0f)
23
25#define HUE_MAX (359U)
27#define HUE_MAX_F32 (359.0f)
29#define HUE_RANGE (360U)
31#define HUE_RANGE_F32 (360.0f)
33#define HUE_FP_BITS (6U)
35#define HUE(x) ((uint16_t)((x) << HUE_FP_BITS))
37#define HUE_F32(x) ((uint16_t)((x) * (float)(1 << HUE_FP_BITS)))
39#define HUE_FP_TO_F32(x) ((float)(x) / (float)(1 << HUE_FP_BITS))
40
42#define SATURATION_MAX (100U)
44#define SATURATION_MAX_F32 (100.0f)
45
47#define VALUE_MAX (100U)
49#define VALUE_MAX_F32 (100.0f)
50
52#define LIGHTNESS_MAX (100U)
54#define LIGHTNESS_MAX_F32 (100.0f)
55
57typedef enum e_color_space
58{
63
69typedef struct st_color_rgb
70{
71 uint8_t blue;
72 uint8_t red;
73 uint8_t green;
75
76static_assert(offsetof(color_rgb_t, blue) == 0, "color_rgb_t layout must be B-R-G for ease of use converting to NeoPixel format.");
77static_assert(offsetof(color_rgb_t, red) == 1, "color_rgb_t layout must be B-R-G for ease of use converting to NeoPixel format.");
78static_assert(offsetof(color_rgb_t, green) == 2, "color_rgb_t layout must be B-R-G for ease of use converting to NeoPixel format.");
79
81typedef struct st_color_hsv
82{
83 uint16_t hue;
84 uint8_t saturation;
85 uint8_t value;
87
89typedef struct st_color_hsl
90{
91 uint16_t hue;
92 uint8_t saturation;
93 uint8_t lightness;
95
100typedef union u_color_kind
101{
109
111typedef struct st_color
112{
113 /* !IMPORTANT! Keep this union at the top so color_t can be cast directly to color_kind_t. */
114 union
115 {
122 };
125} color_t;
126
127static_assert(offsetof(color_t, rgb) == offsetof(color_kind_t, rgb), "The layout of color_kind_t must match that of color_t.");
128static_assert(offsetof(color_t, hsv) == offsetof(color_kind_t, hsv), "The layout of color_kind_t must match that of color_t.");
129static_assert(offsetof(color_t, hsl) == offsetof(color_kind_t, hsl), "The layout of color_kind_t must match that of color_t.");
130static_assert(offsetof(color_t, rgb) == 0, "The color representations must be at the top of the color_t struct.");
131
136extern const color_t color_red;
137extern const color_t color_orange;
138extern const color_t color_yellow;
139extern const color_t color_neon;
140extern const color_t color_green;
141extern const color_t color_seafoam;
142extern const color_t color_cyan;
143extern const color_t color_lightblue;
144extern const color_t color_blue;
145extern const color_t color_purple;
146extern const color_t color_magenta;
147extern const color_t color_pink;
148extern const color_t color_white;
149extern const color_t color_black;
150extern const color_t color_off;
151
154void color_convert(color_space_t to, color_t const * p_in, color_t * p_out);
155
156void color_convert2(color_space_t from, color_space_t to, color_kind_t const * p_in, color_kind_t * p_out);
157
158bool color_parse(char * p_str, color_t * p_color_out);
159
160void color_gamma_correct(color_rgb_t * p_in, color_rgb_t * p_out);
161
162void color_gamma_build(float gamma);
163
166#endif // COLOR_H
color_space_t
Supported color spaces.
Definition: color.h:58
void color_convert2(color_space_t from, color_space_t to, color_kind_t const *p_in, color_kind_t *p_out)
Convert a color to a different color space.
Definition: color.c:411
void color_gamma_build(float gamma)
Updates the gamma table with the provided correction factor.
Definition: color.c:679
void color_convert(color_space_t to, color_t const *p_in, color_t *p_out)
Convert a color to a different color space.
Definition: color.c:386
bool color_parse(char *p_str, color_t *p_color_out)
Parses a color from a string; must be NULL-terminated.
Definition: color.c:493
void color_gamma_correct(color_rgb_t *p_in, color_rgb_t *p_out)
Applies gamma correction to an RGB color.
Definition: color.c:658
@ COLOR_SPACE_RGB
Red-green-blue color space.
Definition: color.h:59
@ COLOR_SPACE_HSL
Hue-saturation-lightness color space.
Definition: color.h:61
@ COLOR_SPACE_HSV
Hue-saturation-value color space.
Definition: color.h:60
const color_t color_green
Green: #00FF00.
Definition: color.c:46
const color_t color_cyan
Cyan: #00FFFF.
Definition: color.c:52
const color_t color_black
Black: #000000.
Definition: color.c:73
const color_t color_lightblue
Light Blue: #0080FF.
Definition: color.c:55
const color_t color_pink
Pink: #FF0080.
Definition: color.c:67
const color_t color_seafoam
Seafoam: #00FF80.
Definition: color.c:49
const color_t color_red
Red: #FF0000.
Definition: color.c:34
const color_t color_orange
Orange: #FF8000.
Definition: color.c:37
const color_t color_blue
Blue: #0000FF.
Definition: color.c:58
const color_t color_off
Off: #000000.
Definition: color.c:76
const color_t color_magenta
Magenta: #FF00FF.
Definition: color.c:64
const color_t color_neon
Neon: #80FF00.
Definition: color.c:43
const color_t color_purple
Purple: #8000FF.
Definition: color.c:61
const color_t color_white
White: #FFFFFF.
Definition: color.c:70
const color_t color_yellow
Yellow: #FFFF00.
Definition: color.c:40
Color represented in hue-saturation-lightness color space.
Definition: color.h:90
uint16_t hue
Hue component; ranges from 0-359.
Definition: color.h:91
uint8_t saturation
Saturation component; ranges from 0-100.
Definition: color.h:92
uint8_t lightness
Lightness component; ranges from 0-100.
Definition: color.h:93
Color represented in hue-saturation-value color space.
Definition: color.h:82
uint8_t saturation
Saturation component; ranges from 0-100.
Definition: color.h:84
uint16_t hue
Hue component; ranges from ~0-359.
Definition: color.h:83
uint8_t value
Value component; ranges from 0-100.
Definition: color.h:85
Color represented in red-green-blue color space.
Definition: color.h:70
uint8_t red
Red component.
Definition: color.h:72
uint8_t green
Green component.
Definition: color.h:73
uint8_t blue
Blue component.
Definition: color.h:71
Represents a color in a defined color space.
Definition: color.h:112
color_hsv_t hsv
Values in the HSV (hue-saturation-value) color space.
Definition: color.h:119
color_space_t color_space
Color space being used for this color.
Definition: color.h:124
color_hsl_t hsl
Values in the HSL (hue-saturation-lightness) color space.
Definition: color.h:121
color_rgb_t rgb
Values in the RGB (red-green-blue) color space.
Definition: color.h:117
Union for the three color space representations.
Definition: color.h:101
color_hsl_t hsl
Values in the HSL (hue-saturation-lightness) color space.
Definition: color.h:107
color_hsv_t hsv
Values in the HSV (hue-saturation-value) color space.
Definition: color.h:105
color_rgb_t rgb
Values in the RGB (red-green-blue) color space.
Definition: color.h:103