Files
CodeSnippets/SetForegroundColorOnBgColor.cs

10 lines
347 B
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Taken From: https://stackoverflow.com/questions/2241447/make-foregroundcolor-black-or-white-depending-on-background
var foreColor = (PerceivedBrightness(backColor) > 130 ? Color.Black : Color.White);
private int PerceivedBrightness(Color c) {
return (int)Math.Sqrt(
c.R * c.R * .299 +
c.G * c.G * .587 +
c.B * c.B * .114);
}