«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» Help

How to get the system's colour palette

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

« Return to contents »