Jump to content
Bob Baudewyns

Creating Gradient by code

Recommended Posts

I'm looking to define a gradient for TRectangle stroke.

I can do that using the editor:

1354599739_GradientEditor.png.deeb031d5ebdd22c37cf8a98427c8b33.png

Which gives me the following definition:

    Stroke.Kind = Gradient
    Stroke.Gradient.Points = <
      item
        Color = claRed
        Offset = 0.000000000000000000
      end
      item
        Color = claBlue
        Offset = 0.250000000000000000
      end
      item
        Color = claYellow
        Offset = 0.500000000000000000
      end
      item
        Color = claFuchsia
        Offset = 0.750000000000000000
      end
      item
        Color = claLime
        Offset = 1.000000000000000000
      end>
    Stroke.Gradient.StartPosition.Y = 0.500000000000000000
    Stroke.Gradient.StopPosition.X = 1.000000000000000000
    Stroke.Gradient.StopPosition.Y = 0.500000000000000000
    Stroke.Thickness = 5.000000000000000000

I'm trying to reproduce that Gradient definition by code like this:

const
  TGradientColors:  Array[0..4] of TAlphaColor = (claRed, claBlue, claYellow, claFuchsia, claLime);
  TGradientOffsets: Array[0..4] of Single      = (0.0, 0.25, 0.50, 0.75, 1.0);

FGradient:= TGradient.Create;
  for ColorIndex:= 0 to 4 do
    begin
      FGradient.Points.Add;
      FGradient.Points[ColorIndex].Color:=  TGradientColors[ColorIndex];
      FGradient.Points[ColorIndex].Offset:= TGradientOffsets[ColorIndex];
  end;
  CalcPosition(FAngle, FGradient);

But what I get is not at all what I'm expecting:

    PanelsAppearence.Gradient.Points = <
      item
        Color = claRed
        Offset = 0.000000000000000000
      end
      item
        Color = claNull
        Offset = 0.000000000000000000
      end
      item
        Color = claNull
        Offset = 0.000000000000000000
      end
      item
        Color = claNull
        Offset = 0.000000000000000000
      end
      item
        Color = claLime
        Offset = 0.750000000000000000
      end
      item
        Color = claWhite
        Offset = 1.000000000000000000
      end
      item
        Color = claFuchsia
        Offset = 1.000000000000000000
      end>

I've searched everywhere for the proper way to define a gradient by code
Could you help me?

Edited by Bob Baudewyns

Share this post


Link to post

try my sample:

procedure TForm1.Button1Click(Sender: TObject);
var
  MyGradient : TGradient;
  MyGradPoint: TCollectionItem;
begin
  Rectangle2.Fill.Kind := TBrushKind.Gradient;
  //
  MyGradient := Rectangle2.Fill.Gradient.Create;
  // MyGradient.Points.Clear;
  //
  MyGradPoint                        := MyGradient.Points.Add;
  TGradientPOint(MyGradPoint).Color  := TColorRec.Black;
  TGradientPOint(MyGradPoint).Offset := 0.0; // min offset
  //
  MyGradPoint                        := MyGradient.Points.Add;
  TGradientPOint(MyGradPoint).Color  := $FFF68484;
  TGradientPOint(MyGradPoint).Offset := 0.472049683332443200;
  //
  MyGradPoint                        := MyGradient.Points.Add;
  TGradientPOint(MyGradPoint).Color  := $FF3B3BEA;
  TGradientPOint(MyGradPoint).Offset := 1.0; // max offset
  //
  // Rectangle2.Fill.Gradient.StartPosition.X := 0.5;
  // Rectangle2.Fill.Gradient.StartPosition.Y := 1.0;
  // Rectangle2.Fill.Gradient.StopPosition.X  := 0.499999970197677600;
  // Rectangle2.Fill.Gradient.StopPosition.Y  := 0.0;
end;

 

Sem título.png

Edited by programmerdelphi2k

Share this post


Link to post

the "Bug: FMX.Types.GlobalUseGPUCanvas" was fixed on RAD 11.2!

  • now, we have "TCameraComponent bug!  :classic_cheerleader:

 

before, you can just do that: 

  • in "Finalization" section (at first unit of your project, for example), just add this line:
implementation

uses
  FMX.TextLayout.GPU; // {for RAD11.1 bug}

  //add this line in an unit in your project

....
finalization
   TGPUObjectsPool.Uninitialize;

 

Edited by programmerdelphi2k

Share this post


Link to post

Yep, this is doing the job. Thanks

  for ColorIndex:= 0 to 4 do
    begin
      GradientPoint:= FGradient.Points.Add;
      TGradientPoint(GradientPoint).Color:=  TGradientColors[ColorIndex];
      TGradientPoint(GradientPoint).Offset:= TGradientOffsets[ColorIndex];
  end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×