Search code examples
delphifiremonkey

Access to a write-only property in Delphi


I have a difficulty to access the colours used in gradients, for example for a TRectangle in firemonkey. The gradient properties (color and color1) are defined write-only, but it would be necessary to read their value.

What is the correct way to access the read values of these properties?

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects;

type
  TForm1 = class(TForm)
    Rectangle: TRectangle;
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
var
  ColorGradient1: TAlphaColor;
begin

  // Error [dcc32 Error]: E2130 Cannot read a write-only property
  ColorGradient1 := Rectangle.Fill.Gradient.Color1;

end;

end.

enter image description here


Solution

  • Use the TGradient.Points property. Per the TGradient documentation:

    A TGradient instance is used for creating a gradient pattern. A gradient defaults to having two points, and the colors of these two points are exposed as Color and Color1. But it can also have more than two colors, which requires modifying the TGradientPoints through the Points property. The gradient can start and end at any given point, and can be either linear (by default) or radial.

    The Points property is a collection of TGradientPoint objects, and the TGradientPoint.Color property is read/write. Internally, the TGradient.Color and TGradient.Color1 properties simply write to the Points[0].Color and Points[1].Color properties, respectively.