How to set a custom TAlphaColor programmatically?

家住魔仙堡 提交于 2019-12-13 07:09:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!