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:
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);
}
The typical approach to averaging RGB colors is to add up all the red, green, and blue values, and divide each by the number of pixels to get the components of the final color.
The average colour is the sum of all pixels divided by the number of pixels. However, this approach may yield a colour different to the most prominent visual color. What you might really want is dominant color rather than average colour.
5) Finding the Average ColorFilter > Blur > Average finds the average of all of the colors in an image (or in a selection) and fills the entire image (or selection) with that color.
Use the average() Function of NumPy to Find the Average Color of Images in Python. In mathematics, we can find the average of a vector by dividing the sum of all the elements in the vector by the total number of elements.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With