Look at MapImageToPalette for details.
function ImMapImageToPalette(var Image: TImageData; Pal: PPalette32; Entries: LongInt): Boolean; cdecl;
ImagingExport (at line 124)