[英]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: 因此,如果您想要:
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通道,因此:
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.