Machado
Machado

Reputation: 14489

How to set a custom TAlphaColor programmatically?

That's basically how I attribute colors programatically in Delphi

label.FontColor      := TAlphaColors.Yellow;

What if I want a custom color like #FF1C90EF?

How can I set it programatically?

Upvotes: 0

Views: 15068

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596256

Similar to TColor in VCL, TAlphaColor is just an integer (well, a Cardinal anyway), so you can type-cast your hex value directly:

label.FontColor := TAlphaColor($FF1C90EF);

This behavior is documented on Embarcadero's DocWiki:

System.UITypes.TAlphaColor

There are three ways to set a color:

  • Using the predefined constants from System.UIConsts:

    Color := claGreen; //Delphi
    Color = TAlphaColor(claGreen); // C++
    
  • Using the predefined constants from TAlphaColorRec:

    Color := TAlphaColorRec.Green; //Delphi
    Color = TAlphaColor(TAlphaColorRec::Green); // C++
    
  • Using the 4-byte hexadecimal number representation:

    Color := $FF008000;  // Delphi
    Color = TAlphaColor(0xFF008000); // C++
    

You can also use the TAlphaColorRec record to assign the individual components:

var
  rec: TAlphaColorRec;
begin
  rec.A := $FF;
  rec.R := $1C;
  rec.G := $90;
  rec.B := $EF;
  label.FontColor := rec.Color;
end;

Upvotes: 8

Morgan Thrapp
Morgan Thrapp

Reputation: 9986

It looks like you can pass the color to a new instance of TAlphaColor

Eg, TAlphaColor($FF1C90EF).

Having said that, you can also just set the .FontColor property directly without creating a new instance of TAlphaColor.

Upvotes: 5

Related Questions