11

I am developing a taskbar for the 2nd screen(something like displayfusion).

However, I'm having difficulty at getting the right average color from the icon. For example Google Chrome/ When I hover it on the main taskbar it backgrounds turns yellow. With my code it turns orange/red.

This is what it looks now:

enter image description here

How can I get the right dominant/average color?

I use this code to calculate the average color:

public static Color getDominantColor(Bitmap bmp)
{
     //Used for tally
     int r = 0;
     int g = 0;
     int b = 0;

     int total = 0;

     for (int x = 0; x < bmp.Width; x++)
     {
          for (int y = 0; y < bmp.Height; y++)
          {
               Color clr = bmp.GetPixel(x, y);    
               r += clr.R;
               g += clr.G;
               b += clr.B;    
               total++;
          }
     }

     //Calculate average
     r /= total;
     g /= total;
     b /= total;

     return Color.FromArgb(r, g, b);
}
p.campbell
  • 98,673
  • 67
  • 256
  • 322
Jelle Vergeer
  • 1,018
  • 1
  • 17
  • 35
  • Here is a similar question: http://stackoverflow.com/questions/5823854/how-can-i-generate-a-palette-of-prominent-colors-from-an-image/5824104#5824104 – Gary Kindel Jul 19 '11 at 15:01

1 Answers1

13

The average color is not neccessarily the color most used. I recommend calculating the HUE of pixels which have saturation over a certain threshold, and use an array to create a histogram of the image. (How many times a certain hue value was used).

Then smooth the histogram (calculate local average values with both neighbours), then get the place where this smoothed histogram takes the maximal value.

You can get HSL values with:

Color.GetHue
Color.GetSaturation
Color.GetBrightness
vbence
  • 20,084
  • 9
  • 69
  • 118