我希望将十六进制颜色代码转换为适当的字符串颜色名称……使用以下代码,我能够获取照片中“最常用”的颜色的十六进制代码:
我想要像 #3A322B 这样的十六进制代码翻译成 "深棕色"。
class ColorMath
{
public static string 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;
Color myColor = Color.FromArgb(r, g, b);
string hex = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");
return hex;
}
}
我想要像 #3A322B 这样的十六进制代码翻译成 "深棕色"。
return "dark brown";
;-) 不过,说真的,在彩色照片中像您的算法一样混合所有颜色,总会导致类似棕色的颜色 - “平均”颜色与主导颜色不同。 您可能需要像此算法一样的算法:https://github.com/lokesh/color-thief,该算法使用中位切割算法对颜色进行量化处理。 在此处查看演示:http://lokeshdhakar.com/projects/color-thief - Dirk Vollmar