How to get the system's colour palette #138

I would like to automatically select colours for painting. At program start I want to use the 16 colour palette. Should the user need more colours, I would like to switch to the 256 colour palette. How can I do this?

You can retrieve the system palette. Palettes are normally used in 16 and 256 colour mode, but the Highcolor mode also uses a system palette, which defines a range of standard colours. The following example shows how to retrieve the system palette in 256c mode:

procedure TForm1.Button1Click(Sender: TObject);
  TPal = Array [0..255] of TPaletteEntry;
  pPal: ^TPal;
  i, numEntries: Integer;
  pPal := Nil;
  numEntries := GetSystemPaletteEntries( Canvas.handle, 0, 8, pPal^ );
  if numEntries > 256 then
    numEntries := 256;
  pPal := AllocMem( numEntries * Sizeof( TPaletteEntry ));
  GetSystemPaletteEntries( Canvas.Handle, 0, numEntries, pPal^ );
  for i:= 0 to numEntries - 1 do
    with pPal^[i] do
        Format('Color %d: R= %d, G= %d, B= %d', [i, pered, pegreen, peblue])
Original resource: The Delphi Pool
Author: Peter Below
Added: 2009/11/06
Last updated: 2009/11/06