Using Alphablend

Just starting out? Need help? Post your questions and find answers here.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 304
Joined: Tue Sep 05, 2017 10:07 am

Using Alphablend

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Using Alphablend

Post 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 
BERESHEIT
User avatar
matalog
Enthusiast
Enthusiast
Posts: 304
Joined: Tue Sep 05, 2017 10:07 am

Re: Using Alphablend

Post 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?
Seymour Clufley
Addict
Addict
Posts: 1265
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Using Alphablend

Post 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
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
matalog
Enthusiast
Enthusiast
Posts: 304
Joined: Tue Sep 05, 2017 10:07 am

Re: Using Alphablend

Post 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.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 304
Joined: Tue Sep 05, 2017 10:07 am

Re: Using Alphablend

Post by matalog »

Am I mistaken that Alphablend Drawingmode() can be used to draw on top of previous colours to add them together?
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Using Alphablend

Post 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 
Seymour Clufley
Addict
Addict
Posts: 1265
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Using Alphablend

Post by Seymour Clufley »

I would also suggest using a canvasgadget for this rather than an imagegadget.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Post Reply