Evaluate whether a HEX value is dark or light

前端 未结 4 852
难免孤独
难免孤独 2020-12-28 20:03

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.

4条回答
  •  长发绾君心
    2020-12-28 20:57

    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
        }
    

提交回复
热议问题