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 
9 #pragma once
10 
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
34 typedef struct {
35  uint8_t r;
36  uint8_t g;
37  uint8_t b;
38 } color_rgb_t;
39 
43 typedef struct {
45  uint8_t alpha;
46 } color_rgba_t;
47 
51 typedef struct {
52  float h;
53  float s;
54  float v;
55 } color_hsv_t;
56 
64 
72 
81 void color_hex2rgb(const uint32_t hex, color_rgb_t *rgb);
82 
91 void color_rgb2hex(const color_rgb_t *rgb, uint32_t *hex);
92 
102 void color_str2rgb(const char *str, color_rgb_t *color);
103 
112 void color_rgb2str(const color_rgb_t *rgb, char *str);
113 
122 static inline void color_rgb_invert(const color_rgb_t *rgb, color_rgb_t *inv_rgb)
123 {
124  inv_rgb->r = rgb->r ^ 0xFF;
125  inv_rgb->g = rgb->g ^ 0xFF;
126  inv_rgb->b = rgb->b ^ 0xFF;
127 }
128 
139 static inline void color_rgb_shift(const color_rgb_t *rgb, color_rgb_t *out, int8_t shift)
140 {
141  if (shift > 0) {
142  out->r = rgb->r << shift;
143  out->g = rgb->g << shift;
144  out->b = rgb->b << shift;
145  } else {
146  out->r = rgb->r >> -shift;
147  out->g = rgb->g >> -shift;
148  out->b = rgb->b >> -shift;
149  }
150 }
151 
161 static inline void color_rgb_set_brightness(const color_rgb_t *rgb, color_rgb_t *out, uint8_t level)
162 {
163  out->r = ((unsigned)rgb->r * level + 128) >> 8;
164  out->g = ((unsigned)rgb->g * level + 128) >> 8;
165  out->b = ((unsigned)rgb->b * level + 128) >> 8;
166 }
167 
179 void color_rgb_complementary(const color_rgb_t *rgb, color_rgb_t *comp_rgb);
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
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:161
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:122
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:139
Data-structure for holding HSV colors.
Definition: color.h:51
float v
value [0.0 - 1.0]
Definition: color.h:54
float s
saturation value [0.0 - 1.0]
Definition: color.h:53
float h
hue value [0.0 - 360.0]
Definition: color.h:52
Data-structure describing a RGB color.
Definition: color.h:34
uint8_t b
blue value [0 - 255]
Definition: color.h:37
uint8_t r
red value [0 - 255]
Definition: color.h:35
uint8_t g
green value [0 - 255]
Definition: color.h:36
RGBA color value.
Definition: color.h:43
color_rgb_t color
RGB value.
Definition: color.h:44
uint8_t alpha
alpha value [0 - 255]
Definition: color.h:45