How can i draw transparent text?

Just starting out? Need help? Post your questions and find answers here.
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

How can i draw transparent text?

Post by Wolfram »

I would expect that the text in this example is light gray but it is black.
How can I draw the text transparent?

Code: Select all

Global Window_0, canvas


Procedure OpenWindow_0(x = 0, y = 0, width = 440, height = 220)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  canvas = CanvasGadget(#PB_Any, 20, 40, 400, 160)
  
  If StartDrawing(CanvasOutput(canvas))
    
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    
    FrontColor(RGBA(0,0,0, 200))
    BackColor(RGBA(255,255,255, 255))
    
    DrawText(20, 20, "Hello Wold")
    Box(120, 20, 60, 60, RGBA(0,0,0, 128))
    
    StopDrawing()
  EndIf

EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
	Case #PB_Menu_Quit
          ProcedureReturn #False
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
        Case canvas
          If EventType() = #PB_EventType_LeftClick
            
          EndIf
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False
macOS Catalina 10.15.7
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: How can i draw transparent text?

Post by Paul »

The help file says...
The CanvasGadget() does not have an alpha channel so the #PB_2DDrawing_AlphaChannel modes of the DrawingMode() function will have no effect and the #PB_2DDrawing_AllChannels mode will be equivalent to #PB_2DDrawing_Default
Image Image
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: How can i draw transparent text?

Post by Wolfram »

How can I draw a text so that I can see 50% of the background? In case of a white background a black text will be 50% gray.
If you like you can do it on an image instead of a canvas.

by the way: the background color of the text does what I expect.
macOS Catalina 10.15.7
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: How can i draw transparent text?

Post by Paul »

This is what your example looks like here under Win7, Win8 & Win10 compiled with PB5.62 x64 & x86
(and I had to comment out the #PB_Menu parts since your code snippet is incomplete)

Image

If I comment out DrawingMode(#PB_2DDrawing_AlphaChannel) then I get

Image

This tells me either the Help file is incorrect stating that "DrawingMode() function will have no effect " or there is a bug ;)
Image Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2085
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How can i draw transparent text?

Post by STARGÅTE »

Code: Select all

    
    DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
    
    FrontColor(RGBA(0,0,0, 128))
    BackColor(RGBA(255,255,255, 255))
    
    DrawText(20, 20, "Hello Wold")
    Box(120, 20, 60, 60, RGBA(0,0,0, 128))
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: How can i draw transparent text?

Post by Paul »

You're right Stargate, that works... so just wrong use of DrawingMode() in this situation.
(assuming he only wanted to blend his text with the background instead of just modifying the alpha channel... in which case the Help file would be correct) ;)

Code: Select all

If OpenWindow(#PB_Any,0,0,440,220,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  canvas = CanvasGadget(#PB_Any, 20, 40, 400, 160)  
  If StartDrawing(CanvasOutput(canvas))    
    DrawingMode(#PB_2DDrawing_Default)
    Box(140,0,200,100,$ff0000)  
  
    DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)    
    FrontColor(RGBA(0,0,0, 128))
    BackColor(RGBA(255,255,255, 255))    
    DrawText(20, 20, "Hello World")
    Box(120, 20, 60, 60, RGBA(0,0,0, 128))
    
    StopDrawing()
  EndIf
  
  Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Image
Image Image
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: How can i draw transparent text?

Post by Wolfram »

Thanks for you help, but here on OSX the text is always black.

Must be a Bug.

Tested on PB 5.42x64 and PB 5.61 x64
macOS Catalina 10.15.7
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How can i draw transparent text?

Post by netmaestro »

On OSX a workaround might involve affecting the components to an image and then setting that image to the canvas.
BERESHEIT
Post Reply