Re: Fred, please CanvasGadget() and surface with alpha support
Posted: Fri Mar 17, 2023 10:55 am
I don't think it's truncate, simply the point over canvasGadget gets 24bits and over ImageGadget gets 32bits
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
;Demnstration of RGBA interaction with a 'shadow canvas' by using a working image with alpha.
;Drawing is done to the working alpha image that acts as a 'shadow canvas'.
;Interactions with the actual canvas is used to reference information on the 'shadow canvas' to obtain RGBA info.
#workingAlphaImageNum = 1 ;Also known as the 'shadow canvas'.
#canvasW = 280 ;canvas width and height
#canvasH = 280
Procedure redoRandomAlphaDrawing(imageNum)
If IsImage(imageNum) And StartDrawing(ImageOutput(imageNum))
Box(0, 0, OutputWidth(), OutputHeight(), #White)
DrawingMode(#PB_2DDrawing_AllChannels)
Box(29, 49, 222, 222, RGBA(240, 240, 240, 0)) ;make an area with less alpha to show later blending
Line(30, 50, 150, 150, RGBA(80, 80, 80, 250))
Line(30, 50 + 150, 150, -150, RGBA(80, 80, 80, 250))
Line(100, 120, 150, 150, RGBA(80, 80, 80, 250))
Line(100, 120 + 150, 150, -150, RGBA(80, 80, 80, 250))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(30, 50, 150, 150 ,RGBA(Random(255),Random(105), 0, Random(100,50))) ;different alphas for each box
Box(100, 120, 150, 150 ,RGBA(0, Random(255), Random(105), Random(100,50)))
StopDrawing()
SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(#workingAlphaImageNum))
EndIf
EndProcedure
If OpenWindow(0, 0, 0, #canvasW + 20, #canvasH + 20, "Test Canvas RGBA (alpha in 24bit) v0.2", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
c.i = 0
CanvasGadget(0, 10, 10, #canvasW, #canvasH, #PB_Canvas_Container)
;create working image with alpha (32-bit) ;this is the 'shadow canvas' where real drawing takes place
If CreateImage(#workingAlphaImageNum, #canvasW, #canvasH, 32, #White)
redoRandomAlphaDrawing(#workingAlphaImageNum)
EndIf
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case 0
If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
;get coordinates from canvas interaction
x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
If x >= 0 And x < #canvasW And y >= 0 And y < #canvasH
If StartDrawing(ImageOutput(#workingAlphaImageNum))
DrawingMode(#PB_2DDrawing_AllChannels)
c = Point(x,y) ;get RGBA value from working image with alpha
bpft = Bool(DrawingBufferPixelFormat() & #PB_PixelFormat_32Bits_RGB)
StopDrawing()
;display on canvas the color value of working image through interaction point of canvas
If StartDrawing(CanvasOutput(0))
DrawText(12, 20 - TextHeight("0"), Mid("aabbggrraarrggbb",bpft * 8 + 1, 8), #Black, #White)
DrawText(10, 20, Space(20),#White,#White)
DrawText(10, 20, RSet(Hex(c), 8, "0"), $FFFFFF * Bool($80 > = (Red(c) * 0.21 + Green(c) * 0.72 + Blue(c) * 0.07)), c)
doRedraw = #True ;signal that canvas was dirtied up and no longer a direct copy of the working image
StopDrawing()
EndIf
EndIf
EndIf
ElseIf EventType() = #PB_EventType_RightButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_RightButton)
redoRandomAlphaDrawing(#workingAlphaImageNum)
ElseIf doRedraw
SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(#workingAlphaImageNum))
redraw = #False
EndIf
EndSelect
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
Code: Select all
;Demnstration of RGBA interaction with a 'shadow canvas' by using a working image with alpha.
;Drawing is done to the working alpha image that acts as a 'shadow canvas'.
;Interactions with the actual canvas is used to reference information on the 'shadow canvas' to obtain RGBA info.
#workingAlphaImageNum = 1 ;Also known as the 'shadow canvas'.
#canvasW = 280 ;canvas width and height
#canvasH = 280
Global c1.i = RGBA(Random(255),Random(105), 0, Random(100,50))
Global c2.i = RGBA(0, Random(255), Random(105), Random(100,50))
Global c3.i = 0
Global cs.s = ""
Procedure redoRandomAlphaDrawing(imageNum)
If IsImage(imageNum) And StartDrawing(ImageOutput(imageNum))
Box(0, 0, OutputWidth(), OutputHeight(), #White)
DrawingMode(#PB_2DDrawing_AllChannels)
Box(29, 49, 222, 222, RGBA(240, 240, 240, 0)) ;make an area with less alpha to show later blending
Line(30, 50, 150, 150, RGBA(80, 80, 80, 250))
Line(30, 50 + 150, 150, -150, RGBA(80, 80, 80, 250))
Line(100, 120, 150, 150, RGBA(80, 80, 80, 250))
Line(100, 120 + 150, 150, -150, RGBA(80, 80, 80, 250))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(30, 50, 150, 150 ,c1) ;different alphas for each box
Box(100, 120, 150, 150 ,c2)
StopDrawing()
SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(#workingAlphaImageNum))
EndIf
EndProcedure
If OpenWindow(0, 0, 0, #canvasW + 20, #canvasH + 20, "Test Canvas RGBA (alpha in 24bit) v0.2", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
c.i = 0
CanvasGadget(0, 10, 10, #canvasW, #canvasH, #PB_Canvas_Container)
;create working image with alpha (32-bit) ;this is the 'shadow canvas' where real drawing takes place
If CreateImage(#workingAlphaImageNum, #canvasW, #canvasH, 32, #White)
redoRandomAlphaDrawing(#workingAlphaImageNum)
EndIf
;#########################################################
If OpenWindow(1, WindowX(0) + 330,WindowY(0) , #canvasW + 20,#canvasH + 20, "Test ImageGadget 32bit Alpha", #PB_Window_SystemMenu)
If CreateImage(2,280, 280 ,32,#PB_Image_Transparent)
StartDrawing(ImageOutput(2))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(30, 50, 150, 150 ,c1)
Box(100, 120, 150, 150 ,c2)
DrawingMode(#PB_2DDrawing_AllChannels)
c3 = Point(130,140)
DrawText( 0, 0, Str(Red(c1))+","+Str(Green(c1))+","+Str(Blue(c1))+","+Str(Alpha(c1)),RGBA(255,255,255,255),c1)
DrawText(100, 16, Str(Red(c3))+","+Str(Green(c3))+","+Str(Blue(c3))+","+Str(Alpha(c3)),RGBA(255,255,255,255),c3)
DrawText(195, 32, Str(Red(c2))+","+Str(Green(c2))+","+Str(Blue(c2))+","+Str(Alpha(c2)),RGBA(255,255,255,255),c2)
StopDrawing()
ImageGadget(2, 10, 10, 280, 280, ImageID(2))
EndIf
EndIf
;#########################################################
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case 0
If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
;get coordinates from canvas interaction
x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
If x >= 0 And x < #canvasW And y >= 0 And y < #canvasH
If StartDrawing(ImageOutput(#workingAlphaImageNum))
DrawingMode(#PB_2DDrawing_AllChannels)
c4 = Point(x,y) ;get RGBA value from working image with alpha
bpft = Bool(DrawingBufferPixelFormat() & #PB_PixelFormat_32Bits_RGB)
StopDrawing()
;display on canvas the color value of working image through interaction point of canvas
If StartDrawing(CanvasOutput(0))
DrawText(12, 20 - TextHeight("0"), Mid("aabbggrraarrggbb",bpft * 8 + 1, 8), #Black, #White)
DrawText(10, 20, Space(20),#White,#White)
DrawText(10, 20, Str(Red(c4))+","+Str(Green(c4))+","+Str(Blue(c4))+","+Str(255),RGBA(255,255,255,255),c4)
; DrawText(10, 20, RSet(Hex(c), 8, "0"), $FFFFFF * Bool($80 > = (Red(c) * 0.21 + Green(c) * 0.72 + Blue(c) * 0.07)), c)
doRedraw = #True ;signal that canvas was dirtied up and no longer a direct copy of the working image
StopDrawing()
EndIf
EndIf
EndIf
ElseIf EventType() = #PB_EventType_RightButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_RightButton)
redoRandomAlphaDrawing(#workingAlphaImageNum)
ElseIf doRedraw
SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(#workingAlphaImageNum))
redraw = #False
EndIf
EndSelect
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
Code: Select all
;Demonstration of RGBA interaction with a 'shadow canvas' by using a working image with alpha.
;Drawing is done to the working alpha image that acts as a 'shadow canvas'.
;Interactions with the actual canvas is used to reference information on the 'shadow canvas' to obtain RGBA info.
#workingAlphaImageNum = 1 ;Also known as the 'shadow canvas'.
#canvasW = 280 ;canvas width and height
#canvasH = 280
Global c1.i = RGBA(Random(255),Random(105), 0, Random(100,50))
Global c2.i = RGBA(0, Random(255), Random(105), Random(100,50))
Global c3.i = 0
Global cs.s = ""
Procedure redoRandomAlphaDrawing(imageNum)
If IsImage(imageNum) And StartDrawing(ImageOutput(imageNum))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(0, 0, OutputWidth(), OutputHeight(), RGBA(255, 255, 255, 255))
; Box(29, 49, 222, 222, RGBA(240, 240, 240, 0)) ;make an area with less alpha to show later blending
; Line(30, 50, 150, 150, RGBA(80, 80, 80, 250))
; Line(30, 50 + 150, 150, -150, RGBA(80, 80, 80, 250))
; Line(100, 120, 150, 150, RGBA(80, 80, 80, 250))
; Line(100, 120 + 150, 150, -150, RGBA(80, 80, 80, 250))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(30, 50, 150, 150 ,c1) ;different alphas for each box
Box(100, 120, 150, 150 ,c2)
StopDrawing()
SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(#workingAlphaImageNum))
EndIf
EndProcedure
If OpenWindow(0, 0, 0, #canvasW, #canvasH , "Test Canvas RGBA (alpha in 24bit) v0.2", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
c.i = 0
CanvasGadget(0, 0, 0, #canvasW, #canvasH, #PB_Canvas_Container)
;create working image with alpha (32-bit) ;this is the 'shadow canvas' where real drawing takes place
If CreateImage(#workingAlphaImageNum, #canvasW, #canvasH, 32, #PB_Image_Transparent );#White
redoRandomAlphaDrawing(#workingAlphaImageNum)
EndIf
;#########################################################
If OpenWindow(1, WindowX(0) + 330,WindowY(0) , #canvasW + 20,#canvasH + 20, "Test ImageGadget 32bit Alpha", #PB_Window_SystemMenu)
If CreateImage(2,280, 280 ,32,#PB_Image_Transparent)
StartDrawing(ImageOutput(2))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(30, 50, 150, 150 ,c1)
Box(100, 120, 150, 150 ,c2)
DrawingMode(#PB_2DDrawing_AllChannels)
c3 = Point(130,140)
DrawText( 0, 0, Str(Red(c1))+","+Str(Green(c1))+","+Str(Blue(c1))+","+Str(Alpha(c1)),RGBA(255,255,255,255),c1)
DrawText(100, 16, Str(Red(c3))+","+Str(Green(c3))+","+Str(Blue(c3))+","+Str(Alpha(c3)),RGBA(255,255,255,255),c3)
DrawText(195, 32, Str(Red(c2))+","+Str(Green(c2))+","+Str(Blue(c2))+","+Str(Alpha(c2)),RGBA(255,255,255,255),c2)
StopDrawing()
ImageGadget(2, 10, 10, 280, 280, ImageID(2))
EndIf
EndIf
SetWindowColor(1,#White)
;#########################################################
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case 0
If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
;get coordinates from canvas interaction
x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
If x >= 0 And x < #canvasW And y >= 0 And y < #canvasH
If StartDrawing(ImageOutput(#workingAlphaImageNum))
DrawingMode(#PB_2DDrawing_AllChannels)
c4 = Point(x,y) ;get RGBA value from working image with alpha
bpft = Bool(DrawingBufferPixelFormat() & #PB_PixelFormat_32Bits_RGB)
StopDrawing()
;display on canvas the color value of working image through interaction point of canvas
If StartDrawing(CanvasOutput(0))
DrawText(12, 20 - TextHeight("0"), Mid("aabbggrraarrggbb",bpft * 8 + 1, 8), #Black, #White)
DrawText(10, 20, Space(20),#White,#White)
DrawText(10, 20, Str(Red(c4))+","+Str(Green(c4))+","+Str(Blue(c4))+","+Str(Alpha(c4)),RGBA(255,255,255,255),c4)
; DrawText(10, 20, RSet(Hex(c), 8, "0"), $FFFFFF * Bool($80 > = (Red(c) * 0.21 + Green(c) * 0.72 + Blue(c) * 0.07)), c)
doRedraw = #True ;signal that canvas was dirtied up and no longer a direct copy of the working image
StopDrawing()
EndIf
EndIf
EndIf
ElseIf EventType() = #PB_EventType_RightButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_RightButton)
redoRandomAlphaDrawing(#workingAlphaImageNum)
ElseIf doRedraw
SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(#workingAlphaImageNum))
redraw = #False
EndIf
EndSelect
EndIf
Until Event = #PB_Event_CloseWindow
EndIf