Generate distinctly different RGB colors in graphs

前端 未结 12 1271
故里飘歌
故里飘歌 2020-11-30 16:51

When generating graphs and showing different sets of data it usually a good idea to difference the sets by color. So one line is red and the next is green and so on. The pro

12条回答
  •  囚心锁ツ
    2020-11-30 17:46

    In case someone needs to generate random medium to high dark color for white foreground in C#, here is the code.

    [DllImport("shlwapi.dll")]
    public static extern int ColorHLSToRGB(int H, int L, int S);
    
    public static string GetRandomDarkColor()
    {
        int h = 0, s = 0, l = 0;
        h = (RandomObject.Next(1, 2) % 2 == 0) ? RandomObject.Next(0, 180) : iApp.RandomObject.Next(181, 360);
        s = RandomObject.Next(90, 160);
        l = RandomObject.Next(80, 130);
    
        return System.Drawing.ColorTranslator.FromWin32(ColorHLSToRGB(h, l, s)).ToHex();
    }
    
    private static string ToHex(this System.Drawing.Color c)
    {
        return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
    }
    

    You can replace RandomObject with your own Random class object.

提交回复
热议问题