Skip to content

Commit

Permalink
Merge pull request #5084 from brummer-simon/devel-color
Browse files Browse the repository at this point in the history
RGB color inversion and complementary color calculation added
  • Loading branch information
miri64 committed Mar 16, 2016
2 parents ad40c20 + 1af9612 commit 2305b9d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
18 changes: 18 additions & 0 deletions sys/color/color.c
Expand Up @@ -15,6 +15,7 @@
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Cenk Gündoğan <mail@cgundogan.de>
* @author Simon Brummer <brummer.simon@googlemail.com>
*
* @}
*/
Expand Down Expand Up @@ -175,3 +176,20 @@ void color_rgb2str(const color_rgb_t *rgb, char* str)
tmp = rgb->b & 0x0F;
str[5] = (tmp > 9) ? ('A' - 10 + tmp) : ('0' + tmp);
}

void color_rgb_complementary(const color_rgb_t *rgb, color_rgb_t *comp_rgb)
{
uint8_t max = 0;
uint8_t min = 0;
uint16_t val = 0;

max = (rgb->r > rgb->g) ? rgb->r : rgb->g;
max = (rgb->b > max) ? rgb->b : max;
min = (rgb->r < rgb->g) ? rgb->r : rgb->g;
min = (rgb->b < min) ? rgb->b : min;
val = max + min;

comp_rgb->r = val - rgb->r;
comp_rgb->g = val - rgb->g;
comp_rgb->b = val - rgb->b;
}
29 changes: 29 additions & 0 deletions sys/include/color.h
Expand Up @@ -17,6 +17,7 @@
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Cenk Gündoğan <mail@cgundogan.de>
* @author Simon Brummer <brummer.simon@googlemail.com>
*/

#ifndef __COLOR_H
Expand Down Expand Up @@ -104,6 +105,34 @@ void color_str2rgb(const char *str, color_rgb_t *color);
*/
void color_rgb2str(const color_rgb_t *rgb, char *str);

/**
* @brief Invert a given rgb color
*
* @pre ((rgb != NULL) && (inv_rgb != NULL))
*
* @param[in] rgb Input rgb color, that should be converted. Must be NOT NULL
* @param[out] inv_rgb Output rgb color, result of the conversion. Must be NOT NULL
*/
static inline void color_rgb_invert(const color_rgb_t *rgb, color_rgb_t *inv_rgb)
{
inv_rgb->r = rgb->r ^ 0xFF;
inv_rgb->g = rgb->g ^ 0xFF;
inv_rgb->b = rgb->b ^ 0xFF;
}

/**
* @brief Calculate the complementary color of a given rgb color.
*
* @note Complementary color calculation according to adobe illustator calculations.
* See https://helpx.adobe.com/illustrator/using/adjusting-colors.html
*
* @pre ((rgb != NULL) && (comp_rgb != NULL))
*
* @param[in] rgb Input rgb color. Must be NOT NULL
* @param[out] comp_rgb Output rgb color, result of the complementary color calculation. Must be NOT NULL
*/
void color_rgb_complementary(const color_rgb_t *rgb, color_rgb_t *comp_rgb);

#ifdef __cplusplus
}
#endif
Expand Down
30 changes: 30 additions & 0 deletions tests/unittests/tests-color/tests-color.c
Expand Up @@ -73,6 +73,34 @@ static void test_rgb2hex__success(void)
TEST_ASSERT_EQUAL_INT(0x000AB13C, hex);
}

static void test_rgb_invert__success(void)
{
const color_rgb_t col = {.r = 100, .g = 128, .b = 0};
const color_rgb_t res = {.r = 155, .g = 127, .b = 255};
color_rgb_t tmp;

color_rgb_invert(&col, &tmp);

TEST_ASSERT_EQUAL_INT(res.r, tmp.r);
TEST_ASSERT_EQUAL_INT(res.g, tmp.g);
TEST_ASSERT_EQUAL_INT(res.b, tmp.b);
}


static void test_rgb_complementary__success(void)
{
/* See example: https://helpx.adobe.com/illustrator/using/adjusting-colors.html */
const color_rgb_t col = {.r = 102, .g = 153, .b = 51};
const color_rgb_t res = {.r = 102, .g = 51, .b = 153};
color_rgb_t tmp;

color_rgb_complementary(&col, &tmp);

TEST_ASSERT_EQUAL_INT(res.r, tmp.r);
TEST_ASSERT_EQUAL_INT(res.g, tmp.g);
TEST_ASSERT_EQUAL_INT(res.b, tmp.b);
}

Test *tests_color_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
Expand All @@ -81,6 +109,8 @@ Test *tests_color_tests(void)
new_TestFixture(test_hex2rgb__success),
new_TestFixture(test_rgb2hex__success),
new_TestFixture(test_rgb2str__success),
new_TestFixture(test_rgb_invert__success),
new_TestFixture(test_rgb_complementary__success),
};

EMB_UNIT_TESTCALLER(color_tests, NULL, NULL, fixtures);
Expand Down

0 comments on commit 2305b9d

Please sign in to comment.