color.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2014-2016 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
22 #include <stdint.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
31 typedef struct {
32  uint8_t r;
33  uint8_t g;
34  uint8_t b;
35 } color_rgb_t;
36 
40 typedef struct {
42  uint8_t alpha;
43 } color_rgba_t;
44 
48 typedef struct {
49  float h;
50  float s;
51  float v;
52 } color_hsv_t;
53 
61 
69 
78 void color_hex2rgb(const uint32_t hex, color_rgb_t *rgb);
79 
88 void color_rgb2hex(const color_rgb_t *rgb, uint32_t *hex);
89 
99 void color_str2rgb(const char *str, color_rgb_t *color);
100 
109 void color_rgb2str(const color_rgb_t *rgb, char *str);
110 
119 static inline void color_rgb_invert(const color_rgb_t *rgb, color_rgb_t *inv_rgb)
120 {
121  inv_rgb->r = rgb->r ^ 0xFF;
122  inv_rgb->g = rgb->g ^ 0xFF;
123  inv_rgb->b = rgb->b ^ 0xFF;
124 }
125 
136 static inline void color_rgb_shift(const color_rgb_t *rgb, color_rgb_t *out, int8_t shift)
137 {
138  if (shift > 0) {
139  out->r = rgb->r << shift;
140  out->g = rgb->g << shift;
141  out->b = rgb->b << shift;
142  } else {
143  out->r = rgb->r >> -shift;
144  out->g = rgb->g >> -shift;
145  out->b = rgb->b >> -shift;
146  }
147 }
148 
158 static inline void color_rgb_set_brightness(const color_rgb_t *rgb, color_rgb_t *out, uint8_t level)
159 {
160  out->r = ((unsigned)rgb->r * level + 128) >> 8;
161  out->g = ((unsigned)rgb->g * level + 128) >> 8;
162  out->b = ((unsigned)rgb->b * level + 128) >> 8;
163 }
164 
176 void color_rgb_complementary(const color_rgb_t *rgb, color_rgb_t *comp_rgb);
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
void color_str2rgb(const char *str, color_rgb_t *color)
Convert a hex color string of the form 'RRGGBB' to a color_rgb_t struct.
void color_hex2rgb(const uint32_t hex, color_rgb_t *rgb)
Convert a hex value of the form 0x00RRGGBB to an RGB color struct.
void color_rgb2hsv(color_rgb_t *rgb, color_hsv_t *hsv)
Convert RGB color to HSV color.
static void color_rgb_set_brightness(const color_rgb_t *rgb, color_rgb_t *out, uint8_t level)
Change the brightness of a RGB color by multiplying it with a set factor.
Definition: color.h:158
void color_rgb2str(const color_rgb_t *rgb, char *str)
Convert a color_rgb_t struct to a hex color string of the form 'RRGGBB'.
void color_hsv2rgb(color_hsv_t *hsv, color_rgb_t *rgb)
Convert HSV color to RGB color.
void color_rgb_complementary(const color_rgb_t *rgb, color_rgb_t *comp_rgb)
Calculate the complementary color of a given rgb color.
static void color_rgb_invert(const color_rgb_t *rgb, color_rgb_t *inv_rgb)
Invert a given rgb color.
Definition: color.h:119
void color_rgb2hex(const color_rgb_t *rgb, uint32_t *hex)
Convert a rgb struct to a hex value of the form 0x00RRGGBB.
static void color_rgb_shift(const color_rgb_t *rgb, color_rgb_t *out, int8_t shift)
Shifts a given rgb color to change it's brightness.
Definition: color.h:136
Data-structure for holding HSV colors.
Definition: color.h:48
float v
value [0.0 - 1.0]
Definition: color.h:51
float s
saturation value [0.0 - 1.0]
Definition: color.h:50
float h
hue value [0.0 - 360.0]
Definition: color.h:49
Data-structure describing a RGB color.
Definition: color.h:31
uint8_t b
blue value [0 - 255]
Definition: color.h:34
uint8_t r
red value [0 - 255]
Definition: color.h:32
uint8_t g
green value [0 - 255]
Definition: color.h:33
RGBA color value.
Definition: color.h:40
color_rgb_t color
RGB value.
Definition: color.h:41
uint8_t alpha
alpha value [0 - 255]
Definition: color.h:42