Exemple #1
0
static void send_update(size_t pos, char *buf)
{
    uint32_t hex_rgb = 0x0;
    color_rgb2hex(&rgb, &hex_rgb);

    pos += sprintf(&buf[pos], "{\"n\":\"a:rgb\", \"u\":\"rgb[#hex]\", \"v\":\"#%"PRIx32"\"}]",
                   hex_rgb);

    send_coap_post((uint8_t *)buf, pos);
}
Exemple #2
0
static void test_rgb2str__success(void)
{
    char color_str[7] = { 0 };
    const color_rgb_t rgb = { .r = 0x0A, .g = 0xB1, .b = 0x3C };

    color_rgb2str(&rgb, color_str);

    TEST_ASSERT_EQUAL_STRING("0AB13C", (char *) color_str);
}

static void test_hex2rgb__success(void)
{
    const uint32_t hex = 0x8Fa1b9;
    color_rgb_t rgb;

    color_hex2rgb(hex, &rgb);
    TEST_ASSERT_EQUAL_INT(0x8F, rgb.r);
    TEST_ASSERT_EQUAL_INT(0xA1, rgb.g);
    TEST_ASSERT_EQUAL_INT(0xB9, rgb.b);
}

static void test_rgb2hex__success(void)
{
    uint32_t hex = 0x0;
    const color_rgb_t rgb = { .r = 0x0A, .g = 0xB1, .b = 0x3C };

    color_rgb2hex(&rgb, &hex);

    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) {
        new_TestFixture(test_str2rgb_upper_case__success),
        new_TestFixture(test_str2rgb_lower_case__success),
        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);

    return (Test *)&color_tests;
}

void tests_color(void)
{
    TESTS_RUN(tests_color_tests());
}