From 6d5704307fbc9b9a978a3cb45f9800be5d4f123b Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 16 Nov 2019 16:12:41 +0100 Subject: [PATCH] sys/color: add color_rgb_set_brightness() Add a function to set the brightness level of a RGB value from 0-255. --- sys/include/color.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sys/include/color.h b/sys/include/color.h index 591e905d7a..63a204bfe6 100644 --- a/sys/include/color.h +++ b/sys/include/color.h @@ -150,6 +150,22 @@ static inline void color_rgb_shift(const color_rgb_t *rgb, color_rgb_t *out, int } } +/** + * @brief Change the brightness of a RGB color by multiplying it with a set factor. + * + * @pre ((rgb != NULL) && (out != NULL)) + * + * @param[in] rgb Input rgb color, that should be multiplied. Must be NOT NULL + * @param[out] out Output rgb color. Must be NOT NULL + * @param[in] level New brightness level. 255 = Full Brightness, 0 = Off. + */ +static inline void color_rgb_set_brightness(const color_rgb_t *rgb, color_rgb_t *out, uint8_t level) +{ + out->r = ((unsigned)rgb->r * level + 128) >> 8; + out->g = ((unsigned)rgb->g * level + 128) >> 8; + out->b = ((unsigned)rgb->b * level + 128) >> 8; +} + /** * @brief Calculate the complementary color of a given rgb color. *