The user of the ASP.NET web app I'm building can select colors for use on some of the elements (e.g. buttons/titles) to facilitate some degree of personalisation.
The problem is that by default the text on those layers is black...what I'm trying to do is to evaluate the HEX value chosen by the user through a picker, and switch between black and white text programmatically - this can be in JavaScript, or in code behind.
The crux of the problem is that I'm just not sure how to evaluate the HEX to make the decision whether the proximity of the chosen color to black is too close to use black text.
Any ideas?
The methods to do this are built into .Net now:
var hexcolor = "#FA3CD0";
var color = System.Drawing.ColorTranslator.FromHtml(hexcolor);
var brightness = color.GetBrightness();
if (brightness > .5)
{
// color is light
}
else
{
// color is dark
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With