【问题标题】:get average color from bmp从 bmp 获取平均颜色
【发布时间】:2011-07-09 11:32:32
【问题描述】:

我正在为第二个屏幕开发一个任务栏(类似于 displayfusion)。

但是,我很难从图标中获得正确的平均颜色。例如谷歌浏览器/当我将它悬停在主任务栏上时,它的背景变成黄色。使用我的代码,它变成橙色/红色。

这就是现在的样子:

如何获得正确的主色/平均色?

我使用这段代码来计算平均颜色:

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);
}

【问题讨论】:

标签: c# rgb


【解决方案1】:

平均颜色不一定是最常用的颜色。我建议计算饱和度超过某个阈值的像素的 HUE,并使用数组创建图像的直方图。 (某个色调值使用了多少次)。

然后平滑直方图(计算两个邻居的局部平均值),然后得到这个平滑直方图取最大值的地方。

您可以通过以下方式获取 HSL 值:

Color.GetHue
Color.GetSaturation
Color.GetBrightness

【讨论】: