The code below will set the forecolor or font color of a control, such as a
label, to white or black, depending on the background
color.
System.Drawing.Color foreColor =
System.Drawing.Color.Black;
// all colors coming in are in Hex
format. No Known colors but we'll check anyway.
int r;
int
g;
int b;
if (backColor.IsKnownColor)
{
r = backColor.R;
g = backColor.G;
b = backColor.B;
}
else
{
r =
int.Parse(backColor.ToString().Substring(8, 2),
System.Globalization.NumberStyles.HexNumber);
g =
int.Parse(backColor.ToString().Substring(10, 2),
System.Globalization.NumberStyles.HexNumber);
b =
int.Parse(backColor.ToString().Substring(12, 2),
System.Globalization.NumberStyles.HexNumber);
}
int
changeToWhite = 0;
if (r <= 128)
{
changeToWhite ++;
}
if (g <= 128)
{
changeToWhite ++;
}
if (b <=
128)
{
changeToWhite ++;
}
if
(changeToWhite > 2)
{
foreColor =
System.Drawing.Color.White;
}