Skip to main content
Associate
October 9, 2023
Question

Draw transparent rectangles

  • October 9, 2023
  • 7 replies
  • 2697 views

Hi there. I'm trying to figure out how to draw/fill rectangle with transparency. I can fill opaque rectangles without issues. 
I am using `stm32h750`, 320x240 ILI9341 LCD which supports RGB565 buffers. Below is the code that kinda works, but it generates weird colors. In that code, I'm using DMA2D_M2M_BLEND_FG with RGB565 color and I'm setting alpha for the rectangle in the foreground layer.  How can I make it work? 


```

hdma2d.Instance = DMA2D;
hdma2d.Init.Mode = DMA2D_M2M_BLEND_FG;
hdma2d.Init.ColorMode = DMA2D_OUTPUT_RGB565;
hdma2d.Init.AlphaInverted = DMA2D_REGULAR_ALPHA;
hdma2d.Init.RedBlueSwap = DMA2D_RB_REGULAR;
hdma2d.Init.OutputOffset = screen_width - rect.GetWidth();
// Foreground
hdma2d.LayerCfg[1].AlphaMode = DMA2D_REPLACE_ALPHA;
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_RGB565;
hdma2d.LayerCfg[1].InputOffset = 0;
hdma2d.LayerCfg[1].InputAlpha = alpha;
hdma2d.LayerCfg[1].RedBlueSwap = DMA2D_RB_REGULAR;
hdma2d.LayerCfg[1].AlphaInverted = DMA2D_REGULAR_ALPHA;

// Background
hdma2d.LayerCfg[0].InputAlpha = 0xff;
hdma2d.LayerCfg[0].AlphaMode = DMA2D_REPLACE_ALPHA;
hdma2d.LayerCfg[0].InputColorMode = DMA2D_INPUT_RGB565;
hdma2d.LayerCfg[0].InputOffset = screen_width - rect.GetWidth();
hdma2d.LayerCfg[0].RedBlueSwap = DMA2D_RB_REGULAR;
hdma2d.LayerCfg[0].AlphaInverted = DMA2D_REGULAR_ALPHA;
HAL_DMA2D_Init(&hdma2d);
HAL_DMA2D_ConfigLayer(&hdma2d, 1);
HAL_DMA2D_ConfigLayer(&hdma2d, 0);

HAL_DMA2D_BlendingStart(&hdma2d,
color,
(uint32_t)(buffer + offset),
(uint32_t)(buffer + offset),
rect.GetWidth(),
rect.GetHeight());
```
This topic has been closed for replies.

7 replies

Osman SOYKURT
ST Technical Moderator
October 10, 2023

Hello @brbrr ,

I have some doubts concerning your configuration. Transparency should be used with ARGB888 color.

OsmanSOYKURT_0-1696944566557.png

Can you check that and tell us if it helps?

ST Software Developer | TouchGFX
brbrrAuthor
Associate
October 10, 2023

Thanks, I did try to use ARGB8888 for BLEND_FG, but unfortunately, I end up with completely messed up colors. For example:
- white (BG) + green@50alpha I end up with blue color
- gray + green@50alpha I'm getting purple 

Below is the updated configuration I am using (note I am converting RGB565 to ARGB8888):

 
uint32_t clr = RGB565toARGB8888(__builtin_bswap16(color), 0);
hdma2d.Instance = DMA2D;
hdma2d.Init.Mode = DMA2D_M2M_BLEND_FG;
hdma2d.Init.ColorMode = DMA2D_OUTPUT_RGB565;
hdma2d.Init.AlphaInverted = DMA2D_REGULAR_ALPHA;
hdma2d.Init.RedBlueSwap = DMA2D_RB_REGULAR;
hdma2d.Init.OutputOffset = screen_width - rect.GetWidth();
// Foreground
hdma2d.LayerCfg[1].AlphaMode = DMA2D_REPLACE_ALPHA;
hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
hdma2d.LayerCfg[1].InputOffset = 0;
hdma2d.LayerCfg[1].InputAlpha = alpha;
hdma2d.LayerCfg[1].RedBlueSwap = DMA2D_RB_REGULAR;
hdma2d.LayerCfg[1].AlphaInverted = DMA2D_REGULAR_ALPHA;
 
 



Osman SOYKURT
ST Technical Moderator
November 3, 2023

Hello @brbrr ,

Did you succeed in resolving your issue?

ST Software Developer | TouchGFX
brbrrAuthor
Associate
November 3, 2023

No, unfortunately.