Mirror

How to get the system's colour palette (Views: 100)


Problem/Question/Abstract:

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?

Answer:

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;

<< Back to main page