Delphi TColor和16進制顏色的相互轉換測試
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;