Recent

Author Topic: [SOLVED]Draw Transparent Fill  (Read 10854 times)

lainz

  • Hero Member
  • *****
  • Posts: 4704
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Draw Transparent Fill
« Reply #15 on: April 28, 2019, 01:31:31 am »
It works... but in fact you're using normal canvas to draw, not BGRABitmap, so you don't get antialiasing for example.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Draw Transparent Fill
« Reply #16 on: April 28, 2019, 03:20:46 am »
It works... but in fact you're using normal canvas to draw, not BGRABitmap, so you don't get anti-aliasing for example.

Yeah, I know. At this point it's not a major problem, I'm not recreating PhotoShop...
Its just a scratchpad type app for jotting notes, picture quality isn't needed.

But, if I decide I want the anti-aliasing, then I will have to convert to BGRABitmap


Thanks for bring that up too.  :)
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

TPieter

  • Newbie
  • Posts: 4
Re: [SOLVED]Draw Transparent Fill
« Reply #17 on: January 06, 2025, 10:41:54 pm »
Hi,

This is my first post here.  Thank you all for the helpfull topics and hints.  I am still discovering OOP and Lazarus. 

Regarding the subject of transparancy, I discovered something odd.
The order in wich one changes properties counts:
In order to make drawing on the Form1 transparant, "Canvas.Brush.color:=clNone;" needs to come before  "Canvas.Brush.style:=bsClear;"
I tested this in Lazarus 2.2.6 and 3.0.

Kind regards,



 





wp

  • Hero Member
  • *****
  • Posts: 12773
Re: [SOLVED]Draw Transparent Fill
« Reply #18 on: January 07, 2025, 11:27:34 am »
"make a drawing in Form1 transparent" - what does this mean, what are you drawing?

Can you show the drawing code so that I can see the context? Either post a simple full compilable project, or copy/paste the drawing code into a forum post (please enclose it by [code]...[/code] tags).

TPieter

  • Newbie
  • Posts: 4
Re: [SOLVED]Draw Transparent Fill
« Reply #19 on: January 07, 2025, 11:25:02 pm »
Hi WP,

Making transparent means "not filling the shape":
---------------------------------------------------------------------------
Transparent:

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, ColorBox;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormPaint(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormPaint(Sender: TObject);
begin
  canvas.Brush.color:=clNone;
  canvas.Brush.style:=bsClear;
  canvas.pen.color:=clGreen;
  Canvas.Rectangle(10,10,100,100);
  Canvas.Rectangle(50,50,150,150);
end;

end.
                         
---------------------------------------------------------------------------------
Filled:
Code: [Select]
uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, ColorBox;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormPaint(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormPaint(Sender: TObject);
begin
  canvas.Brush.style:=bsClear;
  Canvas.Brush.color:=clNone;
  canvas.pen.color:=clGreen;
  Canvas.Rectangle(10,10,100,100);
  Canvas.Rectangle(50,50,150,150);
end;

end.
                                             

It is almost the same, just swapped the "canvas.brush.color:=clNone" and the "canvas.brush.style:=bsClear"
For some reason no program works on my raspberry pi to make a screenshot.  I ll continue trying to make a screenshot and then come back.

When Canvas.Brush.color:=clNone comes before Canvas.Brush.style:=bsClear ; the rectangle is not filled, otherwise it is filled and in my example I don't see all the sides of the first rectangle.


TPieter

  • Newbie
  • Posts: 4
Re: [SOLVED]Draw Transparent Fill
« Reply #20 on: January 07, 2025, 11:48:41 pm »
Found a way to make a screenshot on raspberry pi : just press "Print Scrn" button lol

wp

  • Hero Member
  • *****
  • Posts: 12773
Re: [SOLVED]Draw Transparent Fill
« Reply #21 on: January 07, 2025, 11:52:13 pm »
Ok, I see now.

Look at the sources of TBrush.SetColor (in lcl/include/brush.inc of your Lazarus installation) which is called when you set the Canvas.Brush.Color:
Code: Pascal  [Select][+][-]
  1. procedure TBrush.SetColor(const NewColor: TColor; const NewFPColor: TFPColor);
  2. begin
  3.   if (NewColor = Color) and (NewFPColor = FPColor) and (Style <> bsClear) then Exit;
  4.   FreeReference;
  5.   // reset bitmap
  6.   FBitmap := nil;
  7.   FColor := NewColor;
  8.   inherited SetFPColor(NewFPColor);
  9.   if Style = bsClear then
  10.     inherited SetStyle(bsSolid);
  11.   Changed;
  12. end;  
The highlighted lines mean: When the Style is bsClear (transparent) and you change the color (that's what this procedure is supposed to do) then the Style is automatically switched to bsSolid (solid fill). That's quite logical to me: if the Style would remain at bsClear then the new color would not be seen at all.

And in fact, there is no need at all to set any color when you want the Style to be clear. The following code is enough:
Code: Pascal  [Select][+][-]
  1.   canvas.Brush.style:=bsClear;
  2.   canvas.pen.color:=clGreen;
  3.   Canvas.Rectangle(10,10,100,100);
  4.   Canvas.Rectangle(50,50,150,150);
The word "clNone" is a bit misleading: often it means "non-colored color" such as black, but some components do interpret it as "transparent" indeed, and must take care of setting the Brush to bsClear.
« Last Edit: January 08, 2025, 12:25:47 am by wp »

TPieter

  • Newbie
  • Posts: 4
Re: [SOLVED]Draw Transparent Fill
« Reply #22 on: January 08, 2025, 12:00:47 am »
What a super fast reply !
Yes, it is logical not to mention canvas.brush.color at all when I don't want the shape to be filled, I see that now.

It is out of the subject, but I am very gratefull to have fpc/lazarus available.

Tx !

 

TinyPortal © 2005-2018