问题
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?
回答1:
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.
回答2:
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;
来源:https://stackoverflow.com/questions/33760430/how-to-set-a-custom-talphacolor-programmatically