Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create pastel colors programmatically in C#?

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<Color> GeneratePastelColors (int count)

enter image description here

like image 749
Joan Venge Avatar asked Oct 19 '11 04:10

Joan Venge


People also ask

What makes a color a pastel?

Pastels (which are also known as “tints”) are pale tones of colors made by mixing a significant amount of white into the original shade (so, for example, a pastel yellow would be a paler shade of yellow).

What is the code for pastel blue?

Pastel blue is a pale shade of blue. The pastel blue color code is #AEC6CF.


2 Answers

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/

like image 200
Thanatos Avatar answered Oct 07 '22 14:10

Thanatos


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

like image 43
Jeff Ogata Avatar answered Oct 07 '22 15:10

Jeff Ogata