Programmatically choose high-contrast colors

前端 未结 10 724
野的像风
野的像风 2020-12-14 07:29

This should be a simple question, but I haven\'t been able to find a way to make it work.

Essentially, I have a silly localhost page that I use in my webdevelopment.

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 07:39

    Thanks to @starblue !

    Here is C# code that I use

     public static string GetContrastBlackOrWhiteColorAsHtmlColorCode(Color c)
            {
                System.Drawing.Color color = System.Drawing.ColorTranslator.FromHtml("transparent");
    
                try
                {
                    if (c.R >= 128 && c.G >= 128 && c.B >= 128)
                    {
                        return System.Drawing.ColorTranslator.ToHtml(Color.Black);
                    }
                    else
                    {
                        return System.Drawing.ColorTranslator.ToHtml(Color.White);
                    }
                }
                catch (Exception)
                {
                }
    
                return System.Drawing.ColorTranslator.ToHtml(color);
            }
    

提交回复
热议问题