Monday, 2 January 2017

c# - Converting Hex Color Code to Color Name (string)

I want to convert a hex color code to the suitable string color name... with the following code I was able to get the hex code of the "most used" color in a photo:



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

}


So I want for a hex code like: #3A322B to appear something like "dark brown"

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...