I need to generate a color palette of the top 5 dominant colors in an image. I'd like to replicate the results of [Embedly's extract tool](http://embed.ly/docs/explore/extract?url=https%3A%2F%2Fmedia.onfabrik.com%2Fimages%2Fco1%2Fa79b6f098d7e2a63.jpg%3Fmaxwidth%3D600&maxwidth=500) or [Color Thief](http://lokeshdhakar.com/projects/color-thief/).

The following command gives me slightly different results:

```
convert testimage.jpg  -format %c -colorspace LAB -colors 5 histogram:info:- | sort -n -r
    157154: ( 19, 28, 35) #131C23 srgb(19,28,35)
     16164: ( 27, 51, 77) #1B334D srgb(27,51,77)
     15725: ( 79, 88, 84) #4F5854 srgb(79,88,84)
      8608: ( 44, 77,103) #2C4D67 srgb(44,77,103)
      5149: ( 84,126,150) #547E96 srgb(84,126,150)

```

I'm a bit unsure if I should quantize to 5 colors as I've found that doing so doesn't work so well with simple graphics (for example the Google logo). Is it better to use a larger color palette and then just select the top *n* colors?

This leads me on to my next question regarding the quantization algorithm used. Looking at the results of Embedly Extract, the output colors are not necessarily the most *frequent* but appear to be the clusters that are the most *different* from each other.

For example suppose I have a very dark image (black/browns) with a small detail in bright red. How would I ensure that ImageMagick includes the red? (apologies if this sounds dumb, color theory is all new to me!).

Below is the image I've been using for testing:

![enter image description here](https://i.stack.imgur.com/tsfVt.jpg)