Add alpha blending support

This commit is contained in:
inmarket 2017-10-02 16:47:18 +10:00
parent fdaf636b5f
commit f9be386e52
2 changed files with 51 additions and 18 deletions

View File

@ -3649,26 +3649,54 @@ void gdispGDrawBox(GDisplay *g, coord_t x, coord_t y, coord_t cx, coord_t cy, co
}
#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;
#if GDISP_PIXELFORMAT == GDISP_PIXELFORMAT_RGB888
// Special alpha hacked version.
// Note: this will still work with real RGB888
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 a, r, g, b;
r = RED_OF(fg) * fg_ratio;
g = GREEN_OF(fg) * fg_ratio;
b = BLUE_OF(fg) * fg_ratio;
a = ALPHA_OF(fg) * fg_ratio;
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;
a += ALPHA_OF(bg) * bg_ratio;
r += RED_OF(bg) * bg_ratio;
g += GREEN_OF(bg) * bg_ratio;
b += BLUE_OF(bg) * bg_ratio;
r >>= 8;
g >>= 8;
b >>= 8;
a >>= 8;
r >>= 8;
g >>= 8;
b >>= 8;
return RGB2COLOR(r, g, b);
}
return ARGB2COLOR(a, r, g, b);
}
#else
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 >>= 8;
g >>= 8;
b >>= 8;
return RGB2COLOR(r, g, b);
}
#endif
color_t gdispContrastColor(color_t color) {
uint16_t r, g, b;

View File

@ -330,9 +330,14 @@ typedef uint16_t colorformat;
// Special hack to allow alpha on RGB888
#if GDISP_PIXELFORMAT == GDISP_PIXELFORMAT_RGB888
#define GFXTRANSPARENT (0xFF000000)
#define COLOR_BITS_A 8
#define COLOR_SHIFT_A 24
#define ALPHA_OF(c) (((c)>>24) ^ 0xFF)
#define EXACT_ALPHA_OF(c) ALPHA_OF((c))
#define AHTML2COLOR(h) ((h) ^ 0xFF000000)
#define ARGB2COLOR(a,r,g,b) ((((COLOR_TYPE)(((a) ^ 0xFF) & 0xFF)) << 24) | RGB2COLOR_R(r) | RGB2COLOR_G(g) | RGB2COLOR_B(b))
#define RGB2COLOR_A(a) (((COLOR_TYPE)(((a) ^ 0xFF) & 0xFF)) << 24)
#define ARGB2COLOR(a,r,g,b) (RGB2COLOR_A(a) | RGB2COLOR_R(r) | RGB2COLOR_G(g) | RGB2COLOR_B(b))
#define GFXTRANSPARENT (0xFF000000)
#endif
//-------------------------