scottm scottm - 1 year ago 138
C# Question

How can I generate a palette of prominent colors from an image?

I'm trying to figure out how to sample all of the pixels in an image and generate a palette of colors from it, something like this or this. I have no idea where to even begin. Can anyone point me in the right direction?

__EDIT: __

This is what I've ended up with so far:

I used this Pixelate function to get large block sections like joe_coolish suggested. It's working perfectly and giving me a pretty good sample of colors to work with (this is from the windows sample jelly fish picture):

Now, if someone could help me get the 5 most distinct colors (darkest blue, lightest blue, orange, gray and peach(?)), I would love you forever. I really don't understand how to average or add colors together. I also can't figure out how to tell if a color is similar programatically, there are some many numbers and variables in you explanations that I get lost trying to figure out what's doing what to whom.

Answer Source

The answers involving the code show you how to get the full palette. If you want to get the average colors like in the websites you posted, this is how I would do it.

Source Image:


First, I would average the colors by applying a lowpass filter (something like a Gaussian Blur)

enter image description here

That way you are limiting the total palette. From there I would divide the screen into N blocks (N being the total number of pixels you want in your palette)

enter image description here

From there, target each block and iterate over each pixel, and get the average pixel for that block and add it to your palette index. The result is something like this:

enter image description here

That way your palette is limited and you get the average colors from the different regions. You can do all of that in code, and if you'd like some help with that, let me know and I'll post some. This is just the High Level "what I would do".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download