Nice work! I had a similar idea earlier. I wanted to create a palette like yours but based on given input image instead. You can find my initial code (Python, uses PIL) here. It just blurs the image, quantizes it and outputs the found colors in a grid of colors.

Feel free to use the code if you want. It's still missing a smart way to sort the colors (some ideas) and I'm sure there are nicer ways to generate palette than the way I used. I expect a good algorithm would take image content in count better (ie. detect shapes and make sure colors of even smaller shapes are used well) but simple blur + quantize might do for now.