TColor与RGB分量的关系及转换函数(巧用$来把16进制转10进制)

帮助文档中对Tcolor的说明如下:

If you specify TColor as a specific 4-byte hexadecimal number instead of using the constants defined in the Graphics unit, the low three bytes represent RGB color intensities for blue, green, and red, respectively. The value $00FF0000 (Delphi) or 0x00FF0000 (C++) represents full-intensity, pure blue, $0000FF00 (Delphi) or 0x0000FF00 (C++) is pure green, and $000000FF (Delphi) or 0x000000FF (C++) is pure red. $00000000 (Delphi) or 0x00000000 (C++) is black and $00FFFFFF (Delphi) or 0x00FFFFFF (C++) is white.

TColor 值是以十六进制进行存储的,低三位分别表示红、绿、蓝三色,就像例子中所说$000000FF红,$0000FF00绿,$00FF0000蓝。

知道其结构组成后由Tcolor转到RGB的分量就非常简单了。

这里用到字符串处理函数,十六进制转十进制函数等(这个转化比较巧妙!)……

function getRGBText(AColor:TColor):string;
var
sHexRGBResult:string;//$00FF8080为固定格式
sR,sG,sB:string;
begin
  FmtStr(sHexRGBResult, '%s%0.8x', [HexDisplayPrefix, Integer(AColor)]);
 //使用StrToInt函数可以实现十六进制换十进制,具体代码是:StrToInt('$' + 'FF');
 //这时值等于255。也就是在'FF'前加上一个'$'
  sR:=IntToStr(StrToInt('$'+RightStr(sHexRGBResult,2)));
  Delete(sHexRGBResult,8,2);//$00FF80变成这样
  sG:=IntToStr(StrToInt('$'+RightStr(sHexRGBResult,2)));
  Delete(sHexRGBResult,6,2);//$00FF变成这样
  sB:=IntToStr(StrToInt('$'+RightStr(sHexRGBResult,2)));
  Result:='RGB('+sR+','+sG+','+sB+')';
end;
原文地址:https://www.cnblogs.com/delphi7456/p/1869528.html