Loading...
Searching...
No Matches
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
29extern "C" {
30#endif
31
35typedef struct {
36 uint8_t r;
37 uint8_t g;
38 uint8_t b;
40
44typedef struct {
46 uint8_t alpha;
48
52typedef struct {
53 float h;
54 float s;
55 float v;
57
65
73
82void color_hex2rgb(const uint32_t hex, color_rgb_t *rgb);
83
92void color_rgb2hex(const color_rgb_t *rgb, uint32_t *hex);
93
103void color_str2rgb(const char *str, color_rgb_t *color);
104
113void color_rgb2str(const color_rgb_t *rgb, char *str);
114
123static 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
140static 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
162static 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
181
182#ifdef __cplusplus
183}
184#endif
185
186#endif /* COLOR_H */
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:162
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:123
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:140
Data-structure for holding HSV colors.
Definition color.h:52
float v
value [0.0 - 1.0]
Definition color.h:55
float s
saturation value [0.0 - 1.0]
Definition color.h:54
float h
hue value [0.0 - 360.0]
Definition color.h:53
Data-structure describing a RGB color.
Definition color.h:35
uint8_t b
blue value [0 - 255]
Definition color.h:38
uint8_t r
red value [0 - 255]
Definition color.h:36
uint8_t g
green value [0 - 255]
Definition color.h:37
RGBA color value.
Definition color.h:44
color_rgb_t color
RGB value.
Definition color.h:45
uint8_t alpha
alpha value [0 - 255]
Definition color.h:46