Generate distinctly different RGB colors in graphs

前端 未结 12 1276
故里飘歌
故里飘歌 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:48

    I needed the same functionality, in a simple form.

    What I needed was to generate as unique as possible colors from an an increasing index value.

    Here is the code, in C# (Any other language implementation should be very similar)

    The mechanism is very simple

    1. A pattern of color_writers get generated from indexA values from 0 to 7.

    2. For indices < 8, those colors are = color_writer[indexA] * 255.

    3. For indices between 8 and 15, those colors are = color_writer[indexA] * 255 + (color_writer[indexA+1]) * 127

    4. For indices between 16 and 23, those colors are = color_writer[indexA] * 255 + (color_writer[indexA+1]) * 127 + (color_writer[indexA+2]) * 63

    And so on:

        private System.Drawing.Color GetRandColor(int index)
        {
            byte red = 0;
            byte green = 0;
            byte blue = 0;
    
            for (int t = 0; t <= index / 8; t++)
            {
                int index_a = (index+t) % 8;
                int index_b = index_a / 2;
    
                //Color writers, take on values of 0 and 1
                int color_red = index_a % 2;
                int color_blue = index_b % 2;
                int color_green = ((index_b + 1) % 3) % 2;
    
                int add = 255 / (t + 1);
    
                red = (byte)(red+color_red * add);
                green = (byte)(green + color_green * add);
                blue = (byte)(blue + color_blue * add);
            }
    
            Color color = Color.FromArgb(red, green, blue);
            return color;
        }
    

    Note: To avoid generating bright and hard to see colors (in this example: yellow on white background) you can modify it with a recursive loop:

        int skip_index = 0;
        private System.Drawing.Color GetRandColor(int index)
        {
            index += skip_index;
            byte red = 0;
            byte green = 0;
            byte blue = 0;
    
            for (int t = 0; t <= index / 8; t++)
            {
                int index_a = (index+t) % 8;
                int index_b = index_a / 2;
    
                //Color writers, take on values of 0 and 1
                int color_red = index_a % 2;
                int color_blue = index_b % 2;
                int color_green = ((index_b + 1) % 3) % 2;
    
                int add = 255 / (t + 1);
    
                red = (byte)(red + color_red * add);
                green = (byte)(green + color_green * add);
                blue = (byte)(blue + color_blue * add);
            }
    
            if(red > 200 && green > 200)
            {
                skip_index++;
                return GetRandColor(index);
            }
    
            Color color = Color.FromArgb(red, green, blue);
            return color;
        }
    

提交回复
热议问题