From 839955cd052b273c6aab3b45aee930296485c55b Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Wed, 11 Jun 2014 18:13:55 +0200 Subject: [PATCH] sys: added color module --- sys/Makefile | 3 ++ sys/color/Makefile | 2 + sys/color/color.c | 127 ++++++++++++++++++++++++++++++++++++++++++++ sys/include/color.h | 62 +++++++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 sys/color/Makefile create mode 100644 sys/color/color.c create mode 100644 sys/include/color.h diff --git a/sys/Makefile b/sys/Makefile index bbf3f59ca7..4160eaf57d 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -91,5 +91,8 @@ endif ifneq (,$(filter oneway_malloc,$(USEMODULE))) DIRS += oneway-malloc endif +ifneq (,$(filter color,$(USEMODULE))) + DIRS += color +endif include $(RIOTBASE)/Makefile.base diff --git a/sys/color/Makefile b/sys/color/Makefile new file mode 100644 index 0000000000..e47d67770f --- /dev/null +++ b/sys/color/Makefile @@ -0,0 +1,2 @@ +MODULE = color +include $(RIOTBASE)/Makefile.base diff --git a/sys/color/color.c b/sys/color/color.c new file mode 100644 index 0000000000..f00cd2b4bc --- /dev/null +++ b/sys/color/color.c @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser General + * Public License v2.1. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @ingroup sys_color + * @{ + * + * @file + * @brief Implementation of color module + * + * @author Hauke Petersen + * + * @} + */ + +#include "color.h" + + +void color_rgb2hsv(color_rgb_t *rgb, color_hsv_t *hsv) +{ + float rd, gd, bd, delta, rc, gc, bc; + + /* norm RGB colors to the range [0 - 1.0] */ + rd = (float)rgb->r / 255.0f; + gd = (float)rgb->g / 255.0f; + bd = (float)rgb->b / 255.0f; + + /* find value as maximum of the three colors */ + if (rd >= gd) { + hsv->v = (rd >= bd) ? rd : bd; + } + else { + hsv->v = (gd >= bd) ? gd : bd; + } + + /* compute delta from value and minimum of the three RGB colors */ + if (rd <= gd) { + delta = (rd <= bd) ? (hsv->v - rd) : (hsv->v - bd); + } + else { + delta = (gd <= bd) ? (hsv->v - gd) : (hsv->v - bd); + } + + /* find the saturation from value and delta */ + hsv->s = (hsv->v != 0.0f) ? (delta / hsv->v) : 0x0f; + + /* compute hue */ + hsv->h = 0.0f; + if (hsv->s != 0.0) { + rc = (hsv->v - rd) / delta; + gc = (hsv->v - gd) / delta; + bc = (hsv->v - bd) / delta; + + if (rd == hsv->v) { + hsv->h = bc - gc; + } + else if (gd == hsv->v) { + hsv->h = 2.0f + rc - bc; + } + else { + hsv->h = 4.0f + gc - rc; + } + hsv->h *= 60.0f; + if (hsv->h < 0.0f) { + hsv->h += 360.0; + } + } +} + +void color_hsv2rgb(color_hsv_t *hsv, color_rgb_t *rgb) +{ + int i; + float aa, bb, cc, f, h; + + if (hsv->s == 0.0f) { + rgb->r = (uint8_t)(hsv->v * 255.0f); + rgb->g = (uint8_t)(hsv->v * 255.0f); + rgb->b = (uint8_t)(hsv->v * 255.0f); + } + else { + h = (hsv->h == 360.0f) ? 0.0 : hsv->h; + h /= 60.0f; + i = (int)h; + f = h - i; + aa = hsv->v * (1.0f - hsv->s); + bb = hsv->v * (1.0f - (hsv->s * f)); + cc = hsv->v * (1.0f - (hsv->s * (1.0f - f))); + + switch (i) { + case 0: + rgb->r = (uint8_t)(hsv->v * 255.0f); + rgb->g = (uint8_t)(cc * 255.0f); + rgb->b = (uint8_t)(aa * 255.0f); + break; + case 1: + rgb->r = (uint8_t)(bb * 255.0f); + rgb->g = (uint8_t)(hsv->v * 255.0f); + rgb->b = (uint8_t)(aa * 255.0f); + break; + case 2: + rgb->r = (uint8_t)(aa * 255.0f); + rgb->g = (uint8_t)(hsv->v * 255.0f); + rgb->b = (uint8_t)(cc * 255.0f); + break; + case 3: + rgb->r = (uint8_t)(aa * 255.0f); + rgb->g = (uint8_t)(bb * 255.0f); + rgb->b = (uint8_t)(hsv->v * 255.0f); + break; + case 4: + rgb->r = (uint8_t)(cc * 255.0f); + rgb->g = (uint8_t)(aa * 255.0f); + rgb->b = (uint8_t)(hsv->v * 255.0f); + break; + case 5: + rgb->r = (uint8_t)(hsv->v * 255.0f); + rgb->g = (uint8_t)(aa * 255.0f); + rgb->b = (uint8_t)(bb * 255.0f); + break; + } + } +} diff --git a/sys/include/color.h b/sys/include/color.h new file mode 100644 index 0000000000..7773534ad1 --- /dev/null +++ b/sys/include/color.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser General + * Public License v2.1. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @defgroup sys_color Module for handling colors + * @ingroup sys + * @brief The color sys module supports handling RGB and HSV color + * @{ + * + * @file + * @brief Headers for the color handling module + * + * @author Hauke Petersen + */ + +#ifndef __COLOR_H +#define __COLOR_H + +#include + +/** + * @name Data-structure describing a RGB color + */ +typedef struct { + uint8_t r; /**< red value [0 - 255] */ + uint8_t g; /**< green value [0 - 255] */ + uint8_t b; /**< blue value [0 - 255] */ +} color_rgb_t; + +/** + * @name Data-structure for holding HSV colors + */ +typedef struct { + float h; /**< hue value [0.0 - 360.0] */ + float s; /**< saturation value [0.0 - 1.0] */ + float v; /**< value [0.0 - 1.0] */ +} color_hsv_t; + + +/** + * @brief Convert RGB color to HSV color + * + * @param[in] rgb Input color encoded in RGB space + * @param[out] hsv Output color encoded in HSV space + */ +void color_rgb2hsv(color_rgb_t *rgb, color_hsv_t *hsv); + +/** + * @brief Convert HSV color to RGB color + * + * @param[in] hsv Input color encoded in HSV space + * @param[out] rgb Output color encoded in RGB space + */ +void color_hsv2rgb(color_hsv_t *hsv, color_rgb_t *rgb); + +#endif /* __COLOR_H */ +/** @} */