Bob Baudewyns 3 Posted November 30, 2022 (edited) I'm looking to define a gradient for TRectangle stroke. I can do that using the editor: 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 November 30, 2022 by Bob Baudewyns Share this post Link to post
XylemFlow 8 Posted November 30, 2022 What platform are you running it on and which implementation of TCanvas? There are many issues with GPU canvas. Have you read this? https://stackoverflow.com/questions/53561919/fmx-multi-point-gradients Share this post Link to post
Bob Baudewyns 3 Posted November 30, 2022 Yep. My code is based on this but written in a different way. Do you think that's the reason? This is FMX. I'm targeting all platforms Share this post Link to post
programmerdelphi2k 237 Posted November 30, 2022 (edited) 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; Edited November 30, 2022 by programmerdelphi2k Share this post Link to post
programmerdelphi2k 237 Posted November 30, 2022 (edited) the "Bug: FMX.Types.GlobalUseGPUCanvas" was fixed on RAD 11.2! now, we have "TCameraComponent bug! 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 November 30, 2022 by programmerdelphi2k Share this post Link to post
Bob Baudewyns 3 Posted November 30, 2022 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