Both the savefile icon creator and eyecatcher creator programs use K-means to reduce the total colour count to under 16 or 256 colours, however is almost never chooses K colours and often looks bad. Research for better algorithms. One that came to mind is this:
Keep all the code the same, except instead of calling K-means we find the 16 most common colours in our image (Colour 1 appears 36 times, colour 2 appears 33 times, etc). Then maybe we refine them so the colours blend better (Be cautious of centroids merging and we lose colours)