diff options
author | inmarket <andrewh@inmarket.com.au> | 2013-08-01 14:52:28 +1000 |
---|---|---|
committer | inmarket <andrewh@inmarket.com.au> | 2013-08-01 14:52:28 +1000 |
commit | 9d2b0b667b9bfb2d8b6969ade0ff0bd8d646d7d7 (patch) | |
tree | 08ee00d6ad2de75976131b2e24053484ff473403 | |
parent | 2d27673f0f45a1b96aa7b9d8de61807fadb020bc (diff) | |
download | uGFX-9d2b0b667b9bfb2d8b6969ade0ff0bd8d646d7d7.tar.gz uGFX-9d2b0b667b9bfb2d8b6969ade0ff0bd8d646d7d7.tar.bz2 uGFX-9d2b0b667b9bfb2d8b6969ade0ff0bd8d646d7d7.zip |
Add color blending to GDISP
-rw-r--r-- | include/gdisp/gdisp.h | 13 | ||||
-rw-r--r-- | src/gdisp/gdisp.c | 21 |
2 files changed, 34 insertions, 0 deletions
diff --git a/include/gdisp/gdisp.h b/include/gdisp/gdisp.h index ac741d3b..ca1844fd 100644 --- a/include/gdisp/gdisp.h +++ b/include/gdisp/gdisp.h @@ -800,6 +800,19 @@ void gdispDrawBox(coord_t x, coord_t y, coord_t cx, coord_t cy, color_t color); void gdispFillRoundedBox(coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t radius, color_t color); #endif + +/** + * @brief Blend 2 colors according to the alpha + * @return The combined color + * + * @param[in] fg The foreground color + * @param[in] bg The background color + * @param[in] alpha The alpha value (0-255). 0 is all background, 255 is all foreground. + * + * @api + */ +color_t gdispBlendColor(color_t fg, color_t bg, uint8_t alpha); + /* Support routine for packed pixel formats */ #if !defined(gdispPackPixels) || defined(__DOXYGEN__) /** diff --git a/src/gdisp/gdisp.c b/src/gdisp/gdisp.c index 4c005079..b59b571d 100644 --- a/src/gdisp/gdisp.c +++ b/src/gdisp/gdisp.c @@ -990,6 +990,27 @@ void gdispDrawBox(coord_t x, coord_t y, coord_t cx, coord_t cy, color_t color) { } #endif +color_t gdispBlendColor(color_t fg, color_t bg, uint8_t alpha) +{ + uint16_t fg_ratio = alpha + 1; + uint16_t bg_ratio = 256 - alpha; + uint16_t r, g, b; + + r = RED_OF(fg) * fg_ratio; + g = GREEN_OF(fg) * fg_ratio; + b = BLUE_OF(fg) * fg_ratio; + + r += RED_OF(bg) * bg_ratio; + g += GREEN_OF(bg) * bg_ratio; + b += BLUE_OF(bg) * bg_ratio; + + r /= 256; + g /= 256; + b /= 256; + + return RGB2COLOR(r, g, b); +} + #if (!defined(gdispPackPixels) && !defined(GDISP_PIXELFORMAT_CUSTOM)) void gdispPackPixels(pixel_t *buf, coord_t cx, coord_t x, coord_t y, color_t color) { /* No mutex required as we only read static data */ |