Delphi: Decimal color to RGB and back

Converting a decimal color value (24-bit) to RGB in Delphi is easy using bitshifts, the reverse is true as well. Here’s how it’s done:

procedure ColorToRGB(const Color: Integer; out R, G, B: Byte);
  R := Color and $FF;
  G := (Color shr 8) and $FF;
  B := (Color shr 16) and $FF;

function RGBToColor(const R, G, B: Byte): Integer;
  Result := R or (G shl 8) or (B shl 16);

Take in mind that Delphi TColor is a 24-bit integer decimal color representation.

One thought on “Delphi: Decimal color to RGB and back

  1. Harald say:

    You can use the function Vcl.Graphics.ColorToRGB.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.