Algorithm challenge: Generate color scheme from an image
Solution 1:
To find the primary X colors, screencap the app. Run a color histogram on the image. The top X colors in the histogram are the theme. Edit: if gradients are used, you'll want to pick distinct "peaks" of colors; that is, you may have a whole bunch of colors right around "orange" if orange is one of the main colors used in the gradients. Effectively, just enforce a certain amount of distance between your colors chosen from the histogram.
Tweaking the color scheme can best be done in HSV space; convert your colors to HSV space, and if the users want it to be "Brighter", increase the Value, if they want it to be more "Colorful", increase the Saturation, etc.
Determining the text color can best be done by characterizing areas of high variability (high frequency in Fourier space). Within those areas, you should have either: two colors, text and background, in which case your text is the lesser-used color; or you'll have several colors, text and background image colors, in which case the text color is the most common color.
Solution 2:
You can take a look at:
https://github.com/dcollien/Dreamcoat
which does this in CoffeeScript (literate coffee, so it's well documented)
Demo here: http://dcollien.github.io/Dreamcoat/test.html
It has both a colour quantization approach, and a KMeans approach which are combined.