How to create pastel colors programmatically in C#?

后端 未结 2 1323
盖世英雄少女心
盖世英雄少女心 2021-02-01 22:51

To generate them equally spaced out based on the number of colors wanted. Something that looks like this if 8 is given for the count specified:

List         


        
2条回答
  •  逝去的感伤
    2021-02-01 23:29

    You can also take a look at Rich Newman's HSLColor class. He has a series of blog posts on it, starting with this one.

    Using this code I was able to generate a series of colors evenly spaced around the color wheel, but you'll have to add additional logic if you want to include shades of grey.

    private void button1_Click(object sender, EventArgs e) 
    { 
        listView1.Items.Clear(); 
    
        int step = 240 / 8; 
    
        for (int i = 0; i < 240; i += step) 
        { 
            HSLColor color = new HSLColor((double)i, 240, 160); 
            listView1.Items.Add(i.ToString()).BackColor = color; 
        }                
    }
    

    enter image description here

提交回复
热议问题