title: Generate color palette from image with ImageMagick
body: <p>I need to generate a color palette of the top 5 dominant colors in an image. I'd like to replicate the results of <a href="http://embed.ly/docs/explore/extract?url=https%3A%2F%2Fmedia.onfabrik.com%2Fimages%2Fco1%2Fa79b6f098d7e2a63.jpg%3Fmaxwidth%3D600&amp;maxwidth=500" rel="noreferrer">Embedly's extract tool</a> or <a href="http://lokeshdhakar.com/projects/color-thief/" rel="noreferrer">Color Thief</a>.</p>
  
  <p>The following command gives me slightly different results:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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 <em>n</em> colors?</p>
  
  <p>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 <em>frequent</em> but appear to be the clusters that are the most <em>different</em> from each other.</p>
  
  <p>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!).</p>
  
  <p>Below is the image I've been using for testing:</p>
  
  <p><img src="https://i.stack.imgur.com/tsfVt.jpg" alt="enter image description here"></p>