Search code examples
delphi

Extract R, G, B Byte value from TColor


In Delphi, is there a ready-to-use function that can extract R, G, B as byte from a TColor variable?

I find the following one online but it seems to be strange:

  procedure ColorToRGB(iColor: TColor; var R, G, B: Byte);
    function HexToInt(const Value: string): Integer;
    begin
      Result := StrToInt('$' + Value);
    end;
  var
    s: string;
  begin
    s := IntToHex(iColor, 6);
    R := HexToInt(AnsiRightStr(s, 2));
    G := HexToInt(AnsiLeftStr(AnsiRightStr(s, 4), 2));
    B := HexToInt(AnsiLeftStr(s, 2));
  end;

Solution

  • There is a GetRGB() procedure in the Vcl.GraphUtil unit:

    procedure GetRGB(Col: TColor; var R, G, B: Byte);

    uses Vcl.GraphUtil;
    
    procedure ColorToRGB(iColor: TColor; var R, G, B: Byte);
    begin
      GetRGB(iColor, R, G, B);
    end;