title: Applying a Median Cut color reduction algorithm's output palette to the source image
body: <p>So I'm beginning to look into "simple" color reduction to apply to images. I've spent the last day researching how this works, and managed to find what seems to be a decent algorithm to experiment with here: <a href="http://en.literateprograms.org/Median_cut_algorithm_%28C_Plus_Plus%29" rel="nofollow">Median cut algorithm</a></p>
  
  <p>The output here is a palette of n colors. I have yet to ensure this algorithm actually works, but I'm assuming it does. What I would like to do is take that output and apply it to the image that the palette was generated from.</p>
  
  <p>I can't say I'm well versed in color compression formats and all the deep knowledge when it comes to images, but I'm wondering how I can apply the palette without having to start with an image format that comes with an indexed palette (i.e. GIF). </p>
  
  <p>I'm thinking that for each pixel, I calculate the difference between the color of the current pixel and each color in the palette, and replace that pixel with the palette color of the least difference. Would this be a viable way to approach? </p>
  
  <p>Note - I have looked into various libraries (ImageMagick), but these seem slightly overkill; the most I'll be doing with image manipulation will be reducing the color palette - nothing more complex than this. That's why I'm thinking implementing this algorithm would be the simplest approach for my needs.</p>