Look at ReduceColors for details.
function ImReduceColors(var Image: TImageData; MaxColors: LongInt): Boolean; cdecl;
ImagingExport (at line 119)