delphicolorsdelphi-xe7systemcolors

How to get RGB values for a system color?


I've always used GetRValue, GetGValue and GetBValue functions (From Winapi.Windows unit) for extracting the RGB values for a TColor.

Unfortunately, the same approach does not seem to be good for system colors like clWindow, clBtnFace and so on.

For example:

var
  MyColor : TColor;
begin
  MyColor := clBtnFace;

  ShowMessage(
    'R = ' + IntToStr(GetRValue(MyColor)) + sLineBreak +
    'G = ' + IntToStr(GetGValue(MyColor)) + sLineBreak +
    'B = ' + IntToStr(GetBValue(MyColor))
  );
end;

It produces the following output:

R = 15

G = 0

B = 0

Which should looks like this:

Picture of color RGB(15,0,0)

On my system, I see the following color instead:

Picture of color clBtnFace rendered on my system


Solution

  • Using Get(R|G|B)Value() will work just fine with system colors, you just need to convert them to RGB first. Use the ColorToRGB() function for that:

    Converts a TColor value into an RGB representation of the color.

    For example:

    var
      MyColor: TColor;
      RGB: Longint;
    begin
      MyColor := ...; // any valid TColor value, whether RGB or system constant...
      RGB := ColorToRGB(MyColor);
    
      ShowMessage(
        'R = ' + IntToStr(GetRValue(RGB)) + sLineBreak +
        'G = ' + IntToStr(GetGValue(RGB)) + sLineBreak +
        'B = ' + IntToStr(GetBValue(RGB))
      );
    end;