ColorToRGB (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code converts the color of the current form, Form1, to a Windows RGB value.

int NumPaletteEntries = 20; TPaletteEntry FPaletteEntries[20];

Code

__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
  GetPaletteEntries((HPALETTE__*)GetStockObject(DEFAULT_PALETTE), 0, NumPaletteEntries, FPaletteEntries);
  Form1->Color = clMoneyGreen;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int Result = 0;
  int I = 0;
  TColor RealColor = Graphics::ColorToRGB(Form1->Color);
  while (I < NumPaletteEntries)
  {
//  with FPaletteEntries[I] do
	TColor paletteColor =
	RGB(
	  FPaletteEntries[I].peRed,
	  FPaletteEntries[I].peGreen,
	  FPaletteEntries[I].peBlue);
	if (RealColor == paletteColor)
	{
	  Label1->Caption = IntToStr(I);
	  RedEdit->Text = IntToStr(FPaletteEntries[I].peRed);
	  GreenEdit->Text = IntToStr(FPaletteEntries[I].peGreen);
	  BlueEdit->Text = IntToStr(FPaletteEntries[I].peBlue);
	  break;
	}
	I++;
  };
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Form1->Color =
	(StrToInt(RedEdit->Text) << 16) +
	(StrToInt(GreenEdit->Text) << 8) +
	(StrToInt(BlueEdit->Text));
}

Uses