В гугле много вариантов, но они инвертируют цвета, а мне нужно было на основе цвета фона (в ячейке таблицы) подобрать максимально читаемый цвет текста. Методом подбора вышло следующее:
Color SmartColorInvert(Color c)
{
float b = c.GetBrightness();
if (b < 0.4f)
return Color.White;
else if (b > 0.75f)
return Color.Black;
else if (Cm(c.R) && Cm(c.G) && Cm(c.B)) // If grey
return b > 0.6f ? Color.Black : Color.White;
float h = c.GetHue();
if (h > 340 || (h >= 0 && h < 20)) // Red
return Color.Yellow;
if (h > 100 && h < 140) // Green
return Color.DarkCyan;
if (h > 220 && h < 260) // Blue
return Color.Yellow;
return Color.FromArgb(c.A, 255 ^ c.R, 255 ^ c.G, 255 ^ c.B);
}
bool Cm(byte c)
{
return c > 100 && c < 156;
}