color.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 - 2016 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
23 #ifndef COLOR_H
24 #define COLOR_H
25 
26 #include <stdint.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
35 typedef struct {
36  uint8_t r;
37  uint8_t g;
38  uint8_t b;
39 } color_rgb_t;
40 
44 typedef struct {
46  uint8_t alpha;
47 } color_rgba_t;
48 
52 typedef struct {
53  float h;
54  float s;
55  float v;
56 } color_hsv_t;
57 
64 void color_rgb2hsv(color_rgb_t *rgb, color_hsv_t *hsv);
65 
72 void color_hsv2rgb(color_hsv_t *hsv, color_rgb_t *rgb);
73 
82 void color_hex2rgb(const uint32_t hex, color_rgb_t *rgb);
83 
92 void color_rgb2hex(const color_rgb_t *rgb, uint32_t *hex);
93 
103 void color_str2rgb(const char *str, color_rgb_t *color);
104 
113 void color_rgb2str(const color_rgb_t *rgb, char *str);
114 
123 static inline void color_rgb_invert(const color_rgb_t *rgb, color_rgb_t *inv_rgb)
124 {
125  inv_rgb->r = rgb->r ^ 0xFF;
126  inv_rgb->g = rgb->g ^ 0xFF;
127  inv_rgb->b = rgb->b ^ 0xFF;
128 }
129 
140 static inline void color_rgb_shift(const color_rgb_t *rgb, color_rgb_t *out, int8_t shift)
141 {
142  if (shift > 0) {
143  out->r = rgb->r << shift;
144  out->g = rgb->g << shift;
145  out->b = rgb->b << shift;
146  } else {
147  out->r = rgb->r >> -shift;
148  out->g = rgb->g >> -shift;
149  out->b = rgb->b >> -shift;
150  }
151 }
152 
162 static inline void color_rgb_set_brightness(const color_rgb_t *rgb, color_rgb_t *out, uint8_t level)
163 {
164  out->r = ((unsigned)rgb->r * level + 128) >> 8;
165  out->g = ((unsigned)rgb->g * level + 128) >> 8;
166  out->b = ((unsigned)rgb->b * level + 128) >> 8;
167 }
168 
180 void color_rgb_complementary(const color_rgb_t *rgb, color_rgb_t *comp_rgb);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif /* COLOR_H */
187 
color_hsv_t::h
float h
hue value [0.0 - 360.0]
Definition: color.h:53
color_rgba_t::alpha
uint8_t alpha
alpha value [0 - 255]
Definition: color.h:46
color_hsv_t::s
float s
saturation value [0.0 - 1.0]
Definition: color.h:54
color_rgb_invert
static void color_rgb_invert(const color_rgb_t *rgb, color_rgb_t *inv_rgb)
Invert a given rgb color.
Definition: color.h:123
color_rgb_t::g
uint8_t g
green value [0 - 255]
Definition: color.h:37
color_rgba_t::color
color_rgb_t color
RGB value.
Definition: color.h:45
color_hsv_t::v
float v
value [0.0 - 1.0]
Definition: color.h:55
color_rgba_t
RGBA color value.
Definition: color.h:44
color_rgb_t::b
uint8_t b
blue value [0 - 255]
Definition: color.h:38
color_hsv2rgb
void color_hsv2rgb(color_hsv_t *hsv, color_rgb_t *rgb)
Convert HSV color to RGB color.
color_hsv_t
Data-structure for holding HSV colors.
Definition: color.h:52
color_hex2rgb
void color_hex2rgb(const uint32_t hex, color_rgb_t *rgb)
Convert a hex value of the form 0x00RRGGBB to an RGB color struct.
color_rgb_complementary
void color_rgb_complementary(const color_rgb_t *rgb, color_rgb_t *comp_rgb)
Calculate the complementary color of a given rgb color.
color_rgb_set_brightness
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:162
color_rgb_shift
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:140
color_str2rgb
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.
color_rgb_t
Data-structure describing a RGB color.
Definition: color.h:35
color_rgb2hex
void color_rgb2hex(const color_rgb_t *rgb, uint32_t *hex)
Convert a rgb struct to a hex value of the form 0x00RRGGBB.
color_rgb2str
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'.
color_rgb2hsv
void color_rgb2hsv(color_rgb_t *rgb, color_hsv_t *hsv)
Convert RGB color to HSV color.
color_rgb_t::r
uint8_t r
red value [0 - 255]
Definition: color.h:36