Fred, please CanvasGadget() and surface with alpha support

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Fred, please CanvasGadget() and surface with alpha support

Post by Caronte3D »

I don't think it's truncate, simply the point over canvasGadget gets 24bits and over ImageGadget gets 32bits
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Fred, please CanvasGadget() and surface with alpha support

Post by Demivec »

@kernadec: Here is a rework of your original alpha test code.

The image shows a variety of alpha values and the image can be recreated by push right mouse button while over canvas.
It uses the method I described in my first post. The method is to simply creating an image to interact with that contains the values you would like to return with RGBA().

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
How are you going to take advantage of the alpha color values in what you are programming?
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: Fred, please CanvasGadget() and surface with alpha support

Post by kernadec »

Hello,
Demivec, thank you for your code which I modified a little to compare it with ImageGadget using the IDE tool: Color Picker with alpha mode, we can clearly see the differences in the displays.
so CanvasGadget() arrived at the beginning of 2012 we are in 2023 since we have processors which will soon have 24 cores and disks full of terra and with gigabytes of memories, I think that after ten years of 24bit CanvasGadget() could now be in RGBA 32bit

@Caronte3D
you are right they are not truncated but in 24bit sorry

Best Regard


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
Last edited by kernadec on Sun Mar 19, 2023 9:21 am, edited 1 time in total.
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: Fred, please CanvasGadget() and surface with alpha support

Post by Mesa »

This code is pretty good for me except for the values...

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

M.
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: Fred, please CanvasGadget() and surface with alpha support

Post by kernadec »

Hi, @mesa
in your previous code the colors displayed in the canvas window are 32 bit colors
whereas in the demivec code the 24bit colors are respected as well as their values ​​
it is just that the display of the alpha value must be blocked at 255
because with "Color Picker" the colors without alpha
are identical to those of this code in the reference color frame
so i just stuck the display alpha at 255
best regard
Post Reply