admin管理员组

文章数量:1130349

I used the RGB function from Windows unit to generate random colors in VCL, and also used the TextOut to write text in bitmap.

Now I need to write that in FireMonkey for Android

Uses windows;

R:=Random(256);
G:=Random(256);
B:=Random(256);
C:=RGB(R, G, B);

label1.Font.Color:=c; 

Is there an equivalent for these in FireMonkey?

I used the RGB function from Windows unit to generate random colors in VCL, and also used the TextOut to write text in bitmap.

Now I need to write that in FireMonkey for Android

Uses windows;

R:=Random(256);
G:=Random(256);
B:=Random(256);
C:=RGB(R, G, B);

label1.Font.Color:=c; 

Is there an equivalent for these in FireMonkey?

Share Improve this question edited Dec 18, 2024 at 6:38 Abdul Aziz Barkat 21.7k7 gold badges54 silver badges83 bronze badges asked Dec 17, 2024 at 12:37 Mohsen DarrehshiriMohsen Darrehshiri 291 silver badge1 bronze badge 1
  • As the RGB function in WinApi.Windows.pas is provided as inline source, you may just copy that to a suitable location and change the result type to TColorRef. – Uwe Raabe Commented Dec 17, 2024 at 12:51
Add a comment  | 

2 Answers 2

Reset to default 6

Firemonkey uses TAlphaColor colours. Use TAlphaColorRec from the System.UITypes unit to set the individual RGBA values of a colour, eg:

var C: TAlphaColorRec;
begin
  C.A:=255;  //Unless you also want random transparency!
  C.R:=Random(256);
  C.G:=Random(256);
  C.B:=Random(256);

  Label1.Font.Color:=C.Color;
end;

By the way, this won't actually change the font colour unless you also do the following:

Label1.StyledSettings:=Label1.StyledSettings-[TStyledSetting.FontColor];

When you change the font colour from the Object Inspector, it does that step for you. Without that step, you're telling Delphi to use the colour set in the style.

You might be looking for the function MakeColor in System.UIConsts. Its defined like this:

function MakeColor(R, G, B: Byte; A: Byte = $FF): TAlphaColor;

So you can generate the color directly like this:

label1.Font.Color := MakeColor(Random(256),Random(256),Random(256));

Or to follow your example:

C: TAlphaColor;
begin
  R:=Random(256);
  G:=Random(256);
  B:=Random(256);
  C:=MakeColor(R, G, B);
  label1.Font.Color:=c; 
end;

I used the RGB function from Windows unit to generate random colors in VCL, and also used the TextOut to write text in bitmap.

Now I need to write that in FireMonkey for Android

Uses windows;

R:=Random(256);
G:=Random(256);
B:=Random(256);
C:=RGB(R, G, B);

label1.Font.Color:=c; 

Is there an equivalent for these in FireMonkey?

I used the RGB function from Windows unit to generate random colors in VCL, and also used the TextOut to write text in bitmap.

Now I need to write that in FireMonkey for Android

Uses windows;

R:=Random(256);
G:=Random(256);
B:=Random(256);
C:=RGB(R, G, B);

label1.Font.Color:=c; 

Is there an equivalent for these in FireMonkey?

Share Improve this question edited Dec 18, 2024 at 6:38 Abdul Aziz Barkat 21.7k7 gold badges54 silver badges83 bronze badges asked Dec 17, 2024 at 12:37 Mohsen DarrehshiriMohsen Darrehshiri 291 silver badge1 bronze badge 1
  • As the RGB function in WinApi.Windows.pas is provided as inline source, you may just copy that to a suitable location and change the result type to TColorRef. – Uwe Raabe Commented Dec 17, 2024 at 12:51
Add a comment  | 

2 Answers 2

Reset to default 6

Firemonkey uses TAlphaColor colours. Use TAlphaColorRec from the System.UITypes unit to set the individual RGBA values of a colour, eg:

var C: TAlphaColorRec;
begin
  C.A:=255;  //Unless you also want random transparency!
  C.R:=Random(256);
  C.G:=Random(256);
  C.B:=Random(256);

  Label1.Font.Color:=C.Color;
end;

By the way, this won't actually change the font colour unless you also do the following:

Label1.StyledSettings:=Label1.StyledSettings-[TStyledSetting.FontColor];

When you change the font colour from the Object Inspector, it does that step for you. Without that step, you're telling Delphi to use the colour set in the style.

You might be looking for the function MakeColor in System.UIConsts. Its defined like this:

function MakeColor(R, G, B: Byte; A: Byte = $FF): TAlphaColor;

So you can generate the color directly like this:

label1.Font.Color := MakeColor(Random(256),Random(256),Random(256));

Or to follow your example:

C: TAlphaColor;
begin
  R:=Random(256);
  G:=Random(256);
  B:=Random(256);
  C:=MakeColor(R, G, B);
  label1.Font.Color:=c; 
end;

本文标签: