Drawing a transparent text with a framed font

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Drawing a transparent text with a framed font

      Hello to everyone,

      I'm trying to draw some transparent text on the background that filled with a vertical gradient. I'd like to have a text with transparent and gray framed characters, so the background could be seen through it. But instead of transparency I've got black characters.

      Here is some code from the simulator (also in attach):

      C Source Code: main.c

      1. #include "GUI.h"
      2. #include "DIALOG.h"
      3. #define RECOMMENDED_MEMORY (1024L * 5)
      4. GUI_RECT pRect;
      5. void MainTask(void) {
      6. GUI_Init();
      7. WM_MULTIBUF_Enable(1);
      8. if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
      9. GUI_ErrorOut("Not enough memory available.");
      10. return;
      11. }
      12. // Fill the screen with a gray color
      13. GUI_SetBkColor(GUI_GRAY);
      14. GUI_Clear();
      15. // Set the recrangle area for text in the middle of the screen (320 * 240)
      16. pRect.x0 = 120;
      17. pRect.y0 = 110;
      18. pRect.x1 = 200;
      19. pRect.y1 = 130;
      20. // Fill the text area with the red -> blue vertical gradient
      21. GUI_DrawGradientV(pRect.x0, pRect.y0, pRect.x1, pRect.y1, GUI_RED, GUI_BLUE);
      22. // Set the framed font
      23. GUI_SetFont(GUI_FONT_20F_ASCII);
      24. // Set the frame color of the characters
      25. GUI_SetBkColor(GUI_GRAY);
      26. // Set the transparency of the characters
      27. GUI_SetColor(GUI_TRANSPARENT);
      28. // Enable alpha channel
      29. GUI_EnableAlpha(1);
      30. // Draw text in the middle of the text area
      31. GUI_DispStringInRect("EmWin", &pRect, GUI_TA_HCENTER | GUI_TA_VCENTER);
      32. // Disable alpha channel
      33. GUI_EnableAlpha(0);
      34. while(1)
      35. {
      36. }
      37. }
      Display All
      Here's what I got:



      Further if I call GUI_SetColor(GUI_TRANSPARENT) after GUI_EnableAlpha(1) then the text become completely unvisible including frame:


      Where is my mistake?

      Thanks for your time,

      Alex.
      Files
      • sources.zip

        (7.56 kB, downloaded 701 times, last: )
    • Hi,

      This is a perfect example for a usecase of memory devices.

      Try the code below, I guess that is what you want. The problem is that emWin will write the transparent pixels (the string) to the background which leads to fully transparent pixel in the frame buffer, it does not mix the color. Due to this the result is a black pixel.
      With the memory device the pixels will also set to fully transparent, but when drawing the memory device it does not touch the background when setting fully transparent pixels.

      C Source Code

      1. #include "GUI.h"
      2. #include "DIALOG.h"
      3. void MainTask(void) {
      4. GUI_RECT pRect;
      5. GUI_HMEM hMem;
      6. GUI_Init();
      7. WM_MULTIBUF_Enable(1);
      8. // Fill the screen with a gray color
      9. GUI_SetBkColor(GUI_GRAY);
      10. GUI_Clear();
      11. // Set the recrangle area for text in the middle of the screen (320 * 240)
      12. pRect.x0 = 120;
      13. pRect.y0 = 110;
      14. pRect.x1 = 200;
      15. pRect.y1 = 130;
      16. //
      17. // Draw the background
      18. //
      19. GUI_DrawGradientV(pRect.x0, pRect.y0, pRect.x1, pRect.y1, GUI_RED, GUI_BLUE);
      20. //
      21. // Create a memory device
      22. //
      23. hMem = GUI_MEMDEV_CreateFixed32(pRect.x0, pRect.y0, pRect.x1, pRect.y1);
      24. GUI_MEMDEV_Select(hMem); // Select the device
      25. GUI_DrawGradientV(pRect.x0, pRect.y0, pRect.x1, pRect.y1, GUI_RED, GUI_BLUE); // Draw the same gradient
      26. GUI_SetFont(GUI_FONT_20F_ASCII); // Set the font
      27. GUI_SetBkColor(GUI_GRAY); // Set a bk color (frame)
      28. GUI_SetColor(GUI_TRANSPARENT); // Set fg color
      29. GUI_DispStringInRect("EmWin", &pRect, GUI_TA_HCENTER | GUI_TA_VCENTER); // Draw the string
      30. GUI_MEMDEV_Select(0); // De-select device
      31. GUI_MEMDEV_Write(hMem); // Write the device were it was created
      32. GUI_MEMDEV_Delete(hMem);
      33. while(1) {
      34. GUI_Delay(100);
      35. }
      36. }
      Display All
      You might also check the API description for GUI_MEMDEV_PunchOutDevice(). This might be also a solution.

      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Hello, Sven,

      Yes, indeed I thought so that its about memory devices. I tested GUI_MEMDEV_PunchOutDevice() and in case of text it might give interesting effects, for example, gradient on the frame...

      Thank you for your example.