How to get the system's colour palette #138

Question
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);
type
  TPal = Array [0..255] of TPaletteEntry;
var
  pPal: ^TPal;
  i, numEntries: Integer;
begin
  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^ );
  memo1.clear;
  for i:= 0 to numEntries - 1 do
    with pPal^[i] do
      memo1.lines.add(
        Format('Color %d: R= %d, G= %d, B= %d', [i, pered, pegreen, peblue])
      );
end;
Original resource: The Delphi Pool
Author: Peter Below
Added: 2009/11/06
Last updated: 2009/11/06