Page 1 of 1

False colors using LineXY()

Posted: Wed Sep 18, 2013 1:26 pm
by bal
Hi All,

At last I have managed to recover my forum account.

Here is small bug I found some months ago while wanted to draw vertical lines with LineXY(...RGBA()) using DrawingMode(#PB_2DDrawing_AlphaClip).

Code: Select all

;
; A bug when drawing a _vertical_ alpha line with LineXY(x1,y1,x2,y2,RGBA()) using DrawingMode(#PB_2DDrawing_AlphaClip)
;
; try with a low alpha value (default), and play with the X-slider (top right):
;  - the rotated line will appear with "Alpha 255" when it is exactly vertical,
;  - the line will have a false color,
;  - changing the _Alpha_ value changes the _Color_ of the line
; (seems like mixed r-g-b-a values somewhere in the "background")
;
; discovered in PB 5.11, Windows 7 Starter (x86) - English
; still reproducible in PB 5.20 LTS, Windows 7 Starter (x86) - English
;
; Top row: Normal Drawing (no bugs, just to compare the two drawing modes)
; Bottom row: Alpha Drawing (the strange one)

Enumeration ; Gadgets
  #canvas
  
  #trkR
  #trkG
  #trkB
  #trkAlpha
  #trkX
  #trkY
  
  #txtR
  #txtG
  #txtB
  #txtAlpha
  #txtX
  #txtY
EndEnumeration
Global r,g,b,a,x,y

Declare TestDrawing()

If OpenWindow(0, #PB_Any, #PB_Any, 600, 480, "Main Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  CanvasGadget(#canvas, 20, 60, 520, 300)
  TextGadget(#txtAlpha,     20,  0, 200, 20, "Alpha:", #PB_Text_Center)
  TrackBarGadget(#trkAlpha, 20, 25, 200, 20, 0, 255)
  SetGadgetState(#trkAlpha, 30)
  
  TextGadget(#txtX,     250,  0, 300, 20, "X:", #PB_Text_Center)
  TrackBarGadget(#trkX, 250, 25, 300, 20, 0, 100) : SetGadgetState(#trkX, 50)
  
  TextGadget(#txtY,     550, 40, 40, 20, "Y:", #PB_Text_Center)
  TrackBarGadget(#trkY, 550, 60, 40, 300, 0, 100, #PB_TrackBar_Vertical) : SetGadgetState(#trkY, 100)
  
  TextGadget(#txtR, 320, 380, 100, 20, "R:")
  TextGadget(#txtG, 320, 410, 100, 20, "G:")
  TextGadget(#txtB, 320, 440, 100, 20, "B:")
  TrackBarGadget(#trkR, 20, 380, 300, 20, 0, 255) : SetGadgetState(#trkR, 255)
  TrackBarGadget(#trkG, 20, 410, 300, 20, 0, 255)
  TrackBarGadget(#trkB, 20, 440, 300, 20, 0, 255)
  
  Repeat
    EventID = WaitWindowEvent()

    r = GetGadgetState(#trkR)
    g = GetGadgetState(#trkG)
    b = GetGadgetState(#trkB)
    a = GetGadgetState(#trkAlpha)
    x = GetGadgetState(#trkX)
    y = GetGadgetState(#trkY)
    
    SetGadgetText(#txtR, "R: " + Str(r))
    SetGadgetText(#txtG, "G: " + Str(g))
    SetGadgetText(#txtB, "B: " + Str(b))   
    SetGadgetText(#txtAlpha, "Alpha: " + Str(a))
    SetGadgetText(#txtX, "X: " + Str(x))
    SetGadgetText(#txtY, "Y: " + Str(y))
    
    TestDrawing()
  Until EventID = #PB_Event_CloseWindow
  
EndIf ; OpenWindow
End

Procedure TestDrawing()  
  StartDrawing(CanvasOutput(#canvas))
    DrawingMode(#PB_2DDrawing_Default)
    Box(0, 0, GadgetWidth(#canvas), GadgetHeight(#canvas), RGB(255,255,255))
  
    DrawText( 10, 10, "#PB_2DDrawing_Default", RGB(r,g,b))
    LineXY( 10, 30,  10, 130, RGB(r,g,b))
    LineXY(100, 30, 101, 130, RGB(r,g,b))
    LineXY(300+x, 130-y , 400-x, 30+y, RGB(r,g,b))
           
    DrawingMode(#PB_2DDrawing_AlphaClip)
    DrawText( 10, 160, "#PB_2DDrawing_AlphaClip", RGBA(r,g,b,a))
    LineXY( 10, 180,  10, 280, RGBA(r,g,b,a))
    LineXY(100, 180, 101, 280, RGBA(r,g,b,a))
    LineXY(300+x, 280-y, 400-x, 180+y, RGBA(r,g,b,a))
  StopDrawing()
EndProcedure
Regards,

Re: False colors using LineXY()

Posted: Wed Sep 18, 2013 4:48 pm
by netmaestro
#PB_2DDrawing_AlphaClip affects the behavior of the alpha channel in the drawing output. You are drawing to CanvasOutput() which has no alpha channel and so the AlphaClip drawing mode is not appropriate for it. If you use #PB_2DDrawing_AlphaClip on a 32bit drawing output and you get changing colors, then there's something to worry about.

Re: False colors using LineXY()

Posted: Wed Sep 18, 2013 5:06 pm
by bal
thanks, good to know that :)

the strange thing is that the code does what I want (drawings with changeable transparency) except when I draw exact vertical lines with LineXY() as you can see in the example code.

Re: False colors using LineXY()

Posted: Wed Sep 18, 2013 8:08 pm
by Demivec
I can confirm the bug for Window XP (x86).

Even though the CanvasOutput() has no alpha channel the Help file says:
PB Manual wrote:PB_2DDrawing_AlphaClip
The drawing operations will be alpha-blended onto the background like with the #PB_2DDrawing_AlphaBlend mode, with the addition that the alpha channel of the drawing output acts as a mask. This means that areas of the output that are transparent before the blending will also remain transparent afterwards. If the drawing output has no alpha channel then this mode acts just like the #PB_2DDrawing_AlphaBlend mode.
This means using the AlphaClip mode with the CanvasGadget should produce the same results as using the AlphaBlend mode. Using AlphaBlend on the CanvasGadget does not show the bug in drawing vertical lines and all works as expected.


If you were in need of using AlphaClip for the CanvasGadget you can draw to a 32-bit image first and then assign this image to the CanvasGadget with SetGadgetAttribute().

One more tip, in your example code you can replace this line:

Code: Select all

Box(0, 0, GadgetWidth(#canvas), GadgetHeight(#canvas), RGB(255,255,255))
with this one:

Code: Select all

Box(0, 0, OutputWidth(), OutputHeight(), RGB(255,255,255))
The OutputWidth() and OutputHeight() will use the size of the drawing area in the CanvasGadget. This is often better than using GadgetWidth() and GadgetHeight() because those may include the CanvasGadget's border if it has one.

Re: False colors using LineXY()

Posted: Wed Sep 18, 2013 10:30 pm
by bal
Thanks Demivec, #PB_2DDrawing_AlphaBlend really works as expected.
Alpha modes are still a bit tricky for me so I stopped experimenting when I got the result I wanted.
And thanks for the other tip as well. Though I can not see the difference in the two outputs yet it is good to keep these tricks in mind ;)

Re: False colors using LineXY()

Posted: Fri Feb 20, 2015 9:12 pm
by bal
This has been moved to Coding Questions though no question was asked but a bug was reported which is still there in PB 5.31. See the included example in the opening post.