I doubt naive gif encoder will do the job.
in http://en.wikipedia.org/wiki/Graphics_Interchange_Format
it mentions:
"The algorithms used to select the palette and to perform the dithering vary widely in output quality. Additionally, dithering significantly reduces the image's compressibility and thus works contrary to GIF's main purpose."
So to goal is to get the best color palette that satisfies
1. The resulting image quality is close to original image,
2. The compressed image has to be smaller than original
3. The color quantization can be automated without too much human supervision.
I think the general color quantization algorithms may solve the point 1 and 3 but not necessary point 2.
Never mind, I am an idiot sometime.