[FMX] 将颜色字符串值转换为 TAlphaColor

Delphi 自带一个 StringToAlphaColor ,我重新实现了一个自己的版本,定义为 ParseColor,区别就不说了,自己看。

function ParseColor(const S: String; ADefColor: TAlphaColor): TAlphaColor;
var
  p: PWideChar;
  V: Integer absolute Result;
  ARGB: TAlphaColorRec absolute Result;
begin
  if Length(S) > 0 then
  begin
    p := Pointer(S);
    if ((p^ >= 'a') and (p^ <= 'z')) or ((p^ >= 'A') and (p^ <= 'Z')) then
    // #AARRGGBB
    begin
      if not IdentToAlphaColor('cla' + S, V) then
        Result := ADefColor;
    end
    else if (p^ = '#') or (p^ = 'x') or (p^ = '$') or (p^ = 'H') then
    begin
      if Length(S) = 7 then // #RRGGBB?
      begin
        Inc(p);
        if TryStrToInt('$'+p, V) then
          ARGB.A := 255
        else
          Result := ADefColor;
      end
      else if Length(S) = 9 then // AARRGGBB
      begin
        Inc(p);
        if not TryStrToInt('$'+p, V) then
          Result := ADefColor;
      end
      else
        Result := ADefColor;
    end
    else
      Result := ADefColor;
  end
  else
    Result := ADefColor;
end;

 

分享到: