简体   繁体   English

为什么使用十六进制颜色值时,TPanel的颜色属性显示错误的颜色?

[英]Why does TPanel's color property display the wrong color when using a hex color value?

I am using Delphi 2010 and if I create a new VCL application, drop a TPanel on the form and set its "color" property to "clInactiveCaptionText" it shows the correct color. 我正在使用Delphi 2010,如果我创建一个新的VCL应用程序,则将TPanel放到窗体上并将其“ color”属性设置为“ clInactiveCaptionText”,它将显示正确的颜色。

Correct color: 正确的颜色:

在此处输入图片说明

However, if I enter the hex value for this color ($00434E54 --- R 67,G 78,B 84) it shows up incorrectly. 但是,如果我输入此颜色的十六进制值($ 00434E54 --- R 67,G 78,B 84),它将显示不正确。 I should note that the result is the same whether I enable runtime themes or not. 我应该注意,无论是否启用运行时主题,结果都是相同的。

Wrong color: 颜色错误:

在此处输入图片说明

Any idea on why it won't correctly show this color when specifying its hex value? 指定为什么在指定其十六进制值时不能正确显示此颜色的想法?

RGB color values are actually specified as BGR. RGB颜色值实际上指定为BGR。

So if you want: 因此,如果您想要:

  • red you need to specify $000000FF 红色,您需要指定$ 000000FF
  • green you need to specify $0000FF00 绿色,您需要指定$ 0000FF00
  • blue you need to specify $00FF0000 蓝色,您需要指定$ 00FF0000

As others have indicated, the RGB values are stored internally as BGR (ie TColor value, or what Windows calls a COLORREF ), that's why when you specify a custom color code you obtain a different color. 正如其他人指出的那样,RGB值在内部存储为BGR (即TColor值,或Windows称为COLORREF ),这就是为什么当您指定自定义颜色代码时会获得不同的颜色。

To maintain your sanity when specifying colors in RGB form you can use the RGB() function from the Windows unit; 为了以RGB形式指定颜色时保持理智,可以使用Windows单元中的RGB()函数。 this accepts parameters in the "natural"/intuitive RGB order (as byte values) and yields an appropriate TColor / COLORREF value: 这将接受“自然” /直观RGB顺序的参数(作为字节值),并产生适当的TColor / COLORREF值:

  MyPanel.Color := RGB(67, 78, 84);

or if hex is easier: 或者如果十六进制更容易:

  MyPanel.Color := RGB($43, $4E, $54);

这是因为您要以RGB格式设置TColor值,而必须使用BGR。

Actually Tcolor is RGBA color format and also $FF000000 is the alpha channel so: 实际上Tcolor是RGBA颜色格式,并且$FF000000是Alpha通道,因此:

  • To get red chanel you can get $000000FF 要获得红色香奈儿,您可以获得$ 000000FF
  • To get green chanel you can get $0000FF00 要获得绿色香奈儿,您可以获得$ 0000FF00
  • To get bluechanel you can get $00FF0000 要获得bluechanel,您可以获得$ 00FF0000
  • To get alpha chanel chanel you can get $FF000000 要获得Alpha Chanel香奈儿,您可以获得$ FF000000

And easily you can convert the tcolor value to rgb by : 可以轻松地将tcolor值转换为rgb:

IntToHex(ColorPanel1.Color,1) 

this also work in cross platform FMX delphi apps. 这也适用于跨平台FMX delphi应用程序。

function HexToColor(sColor : string) : TColor;
begin
   Result :=
     RGB(
       StrToInt('$'+Copy(sColor, 1, 2)),
       StrToInt('$'+Copy(sColor, 3, 2)),
       StrToInt('$'+Copy(sColor, 5, 2))
     ) ;
end;

With this simple function you can make easier: 使用此简单功能,您可以更轻松:

Panel1.Color := HexToColor ('16a086');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM