Page 1 of 1

Using Alphablend

Posted: Sat Jun 01, 2024 8:13 pm
by matalog
I must be thinking about DrawingMode(#PB_2DDrawing_AlphaBlend) incorrectly, I'll explain why I think this.

I expected this program to draw a dark box that gradually got lighter.

Code: Select all

#width=1000
#height=1000
ALP=1
no=0
OpenWindow(0,100,0,#width,#height,"Window")
CreateImage(0,#width,#height)
ImageGadget(0,0,0,#width,#height,ImageID(0))
Repeat
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_AlphaBlend)

Box(100,100,800,800,RGBA(255,255,255,ALP))
no=no+1
StopDrawing()
SetGadgetState(0,ImageID(0))
Delay(40)

Debug no

    Event = WindowEvent()
  If Event = #PB_Event_CloseWindow 
    End
    EndIf
Until no=512
Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow 
It draws nothing visible to the image.

Do I have to make it 32 bit first?

I expected the same program with ALP=2 to draw a box that gets brighter and eventually white, but it does not, it stays middle-grey-ish.

Is there a drawing mode that will give me what I expect? Is it that I need to have the image in 32 bit mode? I can't get an image into 32 bit mode, because when I add for example: CreateImage(0,#width,#height,32) - Then the image is reported as not initialised.

Re: Using Alphablend

Posted: Sat Jun 01, 2024 8:37 pm
by netmaestro

Code: Select all

#width=1000
#height=1000
alp=1

OpenWindow(0,100,0,#width,#height,"Window")
CreateImage(0,#width,#height)
ImageGadget(0,0,0,#width,#height,ImageID(0))

SetGadgetState(0,ImageID(0))
AddWindowTimer(0,1,40)

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Timer
      alp+1
      If alp>=255
        RemoveWindowTimer(0,1)
      EndIf
      StartDrawing(ImageOutput(0))
        DrawingMode(#PB_2DDrawing_AlphaBlend)
        Box(100,100,800,800,RGBA(alp,alp,alp,alp))
      StopDrawing()
      SetGadgetState(0, ImageID(0))
  EndSelect
  
Until Event = #PB_Event_CloseWindow 

Re: Using Alphablend

Posted: Sat Jun 01, 2024 8:41 pm
by matalog
Thanks for that similar example, but it doesn't seem to allow me to blend colours together, it seems to be setting them categorically.

Is there a way I can have colours mix together getting brighter with each mix? Similar to how my program works with ALP=2, but with it continuing to full white?

Re: Using Alphablend

Posted: Sat Jun 01, 2024 8:44 pm
by Seymour Clufley

Code: Select all

#width=1000
#height=1000
OpenWindow(0,100,0,#width,#height,"Window")
CreateImage(0,#width,#height,32)
ImageGadget(0,0,0,#width,#height,0)

For no = 0 To 510
  Debug no
  
  StartDrawing(ImageOutput(0))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  lum.d = Int(no/2)
  Box(100,100,800,800,RGBA(lum,lum,lum,255))
  StopDrawing()
  SetGadgetState(0,ImageID(0))
  
  Delay(40)
  Event = WindowEvent()
  If Event=#PB_Event_CloseWindow 
    End
  EndIf
Next no
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

Re: Using Alphablend

Posted: Sat Jun 01, 2024 8:56 pm
by matalog
I will be having RGB values decided by another part of a program, I want to be able to control the Alpha channel of the image, which I assumed I could use like a semi-transparent application of the set RGB colours, the amount of transparency set by the ALPHA number.

I do not want to adjust the RGB values I will use, only the Alpha channel value to blend previously drawn colours.

Re: Using Alphablend

Posted: Sat Jun 01, 2024 9:42 pm
by matalog
Am I mistaken that Alphablend Drawingmode() can be used to draw on top of previous colours to add them together?

Re: Using Alphablend

Posted: Sun Jun 02, 2024 5:25 am
by #NULL
The reason why your original code doesn't work, is because with a low alpha like 1 the blending will result in the color that is already there, so no change occurs, also not in the next cycles (It's stored as integer, not as floating point). With value 2 (or even with ALP.d=1.5 which probably gets rounded to integer 2 for RGBA() anyway) you break the cycle to get increasing values.

other things to consider:
- I would specify the image depth ,32 just to be sure (seems to work without though).
- depending on the blending mode internally used, the color/alpha you start out with might be relevant. You can do a box first to set it.
- You can check with Point()and Hex() how your values turned out.
- Better to process all events per 'frame', not just one event, especially on Linux.

Code: Select all

#width=1000
#height=1000
ALP.d=2
no=0
OpenWindow(0,100,0,#width,#height,"Window")
CreateImage(0,#width,#height,32)
ImageGadget(0,0,0,#width,#height,ImageID(0))

StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(0,0,OutputWidth(),OutputHeight(),RGBA(255,255,255,50))
StopDrawing()
SetGadgetState(0,ImageID(0))

Repeat
  StartDrawing(ImageOutput(0))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  
  p1=Point(100,100)
  Box(100,100,800,800,RGBA(255,255,255,ALP))
  p2=Point(100,100)
  no=no+1
  StopDrawing()
  SetGadgetState(0,ImageID(0))
;   Delay(1)
  
  Debug "" + no + " : " + Hex(p1) + " -> " + Hex(p2)
  
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow 
      End
    EndIf
  Until Not Event
Until no=512

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow 

Re: Using Alphablend

Posted: Sun Jun 02, 2024 6:54 am
by Seymour Clufley
I would also suggest using a canvasgadget for this rather than an imagegadget.