随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

function TColorToHex(Color: TColor): string;
begin
  Result :=
    IntToHex(GetRValue(Color), 2) +
    IntToHex(GetGValue(Color), 2) +
    IntToHex(GetBValue(Color), 2);
end;

function HexToTColor(sColor: string): TColor;
begin
  Result :=
  RGB(
    StrToInt(#36 + Copy(sColor, 1, 2)),
    StrToInt(#36 + Copy(sColor, 3, 2)),
    StrToInt(#36 + Copy(sColor, 5, 2))
  );
end;

//测试:
procedure TForm1.Button1Click(Sender: TObject);
var
  c: TColor;
  s: string;
begin
  c := clRed;
  s := TColorToHex(c);
  ShowMessage(s); //FF0000 (相当于 HTML 中的 #FF0000)

  s := 'FF0000';
  c := HexToTColor(s);
  ShowMessage(BoolToStr(c=clRed));  //-1
  Self.Color := c;  //窗体将变红色
end;
posted on 2007-12-12 02:43  万一  阅读(...)  评论(...编辑  收藏