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
9#pragma once
10
24
25#include <stdint.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
34typedef struct {
35 uint8_t r;
36 uint8_t g;
37 uint8_t b;
39
43typedef struct {
44 uint8_t r;
45 uint8_t g;
46 uint8_t b;
47 uint8_t w;
49
50
54typedef struct {
56 uint8_t alpha;
58
62typedef struct {
63 float h;
64 float s;
65 float v;
67
75
83
92void color_hex2rgb(const uint32_t hex, color_rgb_t *rgb);
93
102void color_rgb2hex(const color_rgb_t *rgb, uint32_t *hex);
103
113void color_str2rgb(const char *str, color_rgb_t *color);
114
123void color_rgb2str(const color_rgb_t *rgb, char *str);
124
133static inline void color_rgb_invert(const color_rgb_t *rgb, color_rgb_t *inv_rgb)
134{
135 inv_rgb->r = rgb->r ^ 0xFF;
136 inv_rgb->g = rgb->g ^ 0xFF;
137 inv_rgb->b = rgb->b ^ 0xFF;
138}
139
150static inline void color_rgb_shift(const color_rgb_t *rgb, color_rgb_t *out, int8_t shift)
151{
152 if (shift > 0) {
153 out->r = rgb->r << shift;
154 out->g = rgb->g << shift;
155 out->b = rgb->b << shift;
156 } else {
157 out->r = rgb->r >> -shift;
158 out->g = rgb->g >> -shift;
159 out->b = rgb->b >> -shift;
160 }
161}
162
172static inline void color_rgb_set_brightness(const color_rgb_t *rgb, color_rgb_t *out, uint8_t level)
173{
174 out->r = ((unsigned)rgb->r * level + 128) >> 8;
175 out->g = ((unsigned)rgb->g * level + 128) >> 8;
176 out->b = ((unsigned)rgb->b * level + 128) >> 8;
177}
178
191
192#ifdef __cplusplus
193}
194#endif
195
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:172
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:133
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:150
Data-structure for holding HSV colors.
Definition color.h:62
float v
value [0.0 - 1.0]
Definition color.h:65
float s
saturation value [0.0 - 1.0]
Definition color.h:64
float h
hue value [0.0 - 360.0]
Definition color.h:63
Data-structure describing an 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:54
color_rgb_t color
RGB value.
Definition color.h:55
uint8_t alpha
alpha value [0 - 255]
Definition color.h:56
Data-structure describing an RGBW color.
Definition color.h:43
uint8_t g
green value [0 - 255]
Definition color.h:45
uint8_t r
red value [0 - 255]
Definition color.h:44
uint8_t b
blue value [0 - 255]
Definition color.h:46
uint8_t w
white value [0 - 255]
Definition color.h:47