Reputation: 1734
I need most used 30 color list from a image... I get it but there is alot of color tone's and i can't get main colors. for example picture has red color but i can't get red color.
What can i do?
Dictionary<string,int> d = new Dictionary<string, int>();
Bitmap bmp = (Bitmap)Bitmap.FromFile("c:\\test.jpg");
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color c = bmp.GetPixel(x, y);
var hexColor = c.R.ToString("X") + c.G.ToString("X") + c.B.ToString("X");
if (d.ContainsKey(hexColor))
{
d[hexColor] = ++d[hexColor];
}
else
{
d.Add(hexColor, 1);
}
}
}
var items = (from pair in d
orderby pair.Value descending
select pair);
System.IO.StreamWriter file =
new System.IO.StreamWriter("c:\\colors.html",false);
foreach (var v in items)
{
file.WriteLine("<div style='width:50px;height:50px;float:left;margin-right:10px;background-color:#" + v.Key + "'> </div>");
}
file.Close();
Upvotes: 0
Views: 585
Reputation: 481
RGB is composed of three primary colors: Red, Green, Blue. Each of them has a value in the range 0-255. Colors RGB (0,255,0), RGB (0,254,0), RGB (0,253,0), RGB (2,254,2) look the same, but in fact they are different. Perhaps you should add the tolerance range of colors.
Upvotes: 2