Loading...
Searching...
No Matches
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
21
22#include <stdint.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
31typedef struct {
32 uint8_t r;
33 uint8_t g;
34 uint8_t b;
36
40typedef struct {
41 uint8_t r;
42 uint8_t g;
43 uint8_t b;
44 uint8_t w;
46
47
51typedef struct {
53 uint8_t alpha;
55
59typedef struct {
60 float h;
61 float s;
62 float v;
64
72
80
89void color_hex2rgb(const uint32_t hex, color_rgb_t *rgb);
90
99void color_rgb2hex(const color_rgb_t *rgb, uint32_t *hex);
100
110void color_str2rgb(const char *str, color_rgb_t *color);
111
120void color_rgb2str(const color_rgb_t *rgb, char *str);
121
130static inline void color_rgb_invert(const color_rgb_t *rgb, color_rgb_t *inv_rgb)
131{
132 inv_rgb->r = rgb->r ^ 0xFF;
133 inv_rgb->g = rgb->g ^ 0xFF;
134 inv_rgb->b = rgb->b ^ 0xFF;
135}
136
147static inline void color_rgb_shift(const color_rgb_t *rgb, color_rgb_t *out, int8_t shift)
148{
149 if (shift > 0) {
150 out->r = rgb->r << shift;
151 out->g = rgb->g << shift;
152 out->b = rgb->b << shift;
153 } else {
154 out->r = rgb->r >> -shift;
155 out->g = rgb->g >> -shift;
156 out->b = rgb->b >> -shift;
157 }
158}
159
169static inline void color_rgb_set_brightness(const color_rgb_t *rgb, color_rgb_t *out, uint8_t level)
170{
171 out->r = ((unsigned)rgb->r * level + 128) >> 8;
172 out->g = ((unsigned)rgb->g * level + 128) >> 8;
173 out->b = ((unsigned)rgb->b * level + 128) >> 8;
174}
175
188
189#ifdef __cplusplus
190}
191#endif
192
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:169
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:130
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:147
Data-structure for holding HSV colors.
Definition color.h:59
float v
value [0.0 - 1.0]
Definition color.h:62
float s
saturation value [0.0 - 1.0]
Definition color.h:61
float h
hue value [0.0 - 360.0]
Definition color.h:60
Data-structure describing an 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:51
color_rgb_t color
RGB value.
Definition color.h:52
uint8_t alpha
alpha value [0 - 255]
Definition color.h:53
Data-structure describing an RGBW color.
Definition color.h:40
uint8_t g
green value [0 - 255]
Definition color.h:42
uint8_t r
red value [0 - 255]
Definition color.h:41
uint8_t b
blue value [0 - 255]
Definition color.h:43
uint8_t w
white value [0 - 255]
Definition color.h:44