How to create pastel colors programmatically in C#?

后端 未结 2 1320
盖世英雄少女心
盖世英雄少女心 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

    0 讨论(0)
  • 2021-02-01 23:50

    You'll find colors easier to work with in these sorts of problems if you use HSV instead of RGB.

    "equally spaced colors" almost always means "equally spaced hues". So, for [0,360) in hue, you just equally space by dividing that range equally.

    Now you have a hue, and you just need to find the "pastel" version of that hue. To me, this means desaturating the color a bit. I'd say to 80% saturated for starters.

    In my tests, I used 100% for value. Then just convert to RGB. Here's what I've been playing with:

    <body>
    <script>
    // taken from http://ariya.blogspot.com/2008/07/converting-between-hsl-and-hsv.html
    function hsv_to_hsl(s, v)
    {
        var ss, ll;
        ll = (2. - s) * v;
        ss = 1. * s * v;
        ss /= (ll <= 1.) ? (ll) : 2. - ll;
        ll /= 2.;
    
        return [ss, ll];
    }
    
    function do_colors(sat, light)
    {
        n = 15;
        document.write(n + " colors at " + sat + "% sat, " + light + "% lightness<br />");
        for(var x = 0; x < n; ++x)
        {
            hue = 360.0 / n * x;
            html_chunk = "<div style='width: 50px; height: 50px; display: inline-block; background: hsl(" + hue + ", " + sat + "%, " + light + "%);'>&nbsp;</div>";
            document.write(html_chunk);
        }
        document.write("<br />");
    }
    
    do_colors(100, 50);
    do_colors(95, 75);
    do_colors(75, 50);
    do_colors(100, 35);
    
    // rudimentary averages from your colors
    sl = hsv_to_hsl(.7, .9);
    s = sl[0] * 100;
    l = sl[1] * 100;
    do_colors(s, l);
    do_colors(75, 60);
    </script>
    </body>
    

    Not C#, I know, but just trying to nail the light & sat down.

    Otherwise, you could look at your sample colors, and see if there is any correlation in the HSV/HSL values, and try to derive an algorithm from that. If you plot S/H and V/H, you'll see a large dip in the graph at the grey color --- it seems to be an outlier. (Third from left on bottom row.) Ignoring that value, S is about at 75% and value is just under 90%. Using those values probably gave the nicest result.

    Link: http://jsfiddle.net/ZHyAQ/

    0 讨论(0)
提交回复
热议问题