1

I want to plot X series lines all in different but "nice" colors. By "nice" I mean bright and different.

This is what I am doing at the moment:

var colors = Enum.GetValues(typeof(KnownColor));

int c = -1;
foreach (var ser in AllSeries)
{
    c++;
    chart.PlotDataSeries( Color.FromKnownColor((KnownColor)colors.GetValue(c % 100 + 1)), );
}

But this gives me lots of similar drab colors, mainly black and grey, because KnownColor has a lot of repeated "system menu" type colors included.

diiN__________
  • 7,393
  • 6
  • 42
  • 69
ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • 4
    Store your nice colors in a `Color[]` – Tim Schmelter Jun 16 '16 at 11:49
  • "nice" colors seem very opinionated. Additionally "different but also bright" seems opinionated and also contradictory. #000000 and #FFFFFF are about as different as you can get, one is definitely bright, one is not. I'm not really sure what you're looking for here... this question sounds like any of of the following: Opinionated, asking for tutorial site, color design, none of which are really programming questions - the type of question that should be posted on SO :O – Kritner Jun 16 '16 at 11:52
  • I am asking if there are Alternatives to KnownColor or ways to "select" certain types from it. – ManInMoon Jun 16 '16 at 11:54
  • 1
    @Kritner I've actually had this exact problem in UI design over the years when developing charting software so there is definitely an aspect of programming here as well as a smattering of "good UX". The underlying question is _how can you select a series related colors with a set of properties algorithmically?_ – D.Shawley Jun 16 '16 at 11:58
  • @D.Shawley makes sense, would UX be any better of a site for such a question though? – Kritner Jun 16 '16 at 11:59
  • 1
    This may be of use as an alternative way to randomize colors: http://stackoverflow.com/a/5805844/4846465 – Ryan Peters Jun 16 '16 at 12:01
  • What is wrong with the defaul series' colors or its [alternatives](https://blogs.msdn.microsoft.com/alexgor/2009/10/06/setting-microsoft-chart-series-colors/)? – TaW Jun 17 '16 at 09:39

4 Answers4

1

One approach is to "navigate the color space". I'm not a C# programmer but the method that I have used in diagramming, albeit manually, is to use the Hue-Saturation-Value colorspace instead of Red-Green-Blue. I manually select a "nice" color that I want to start with. Then divide the value space of the hue by the number of colors that you need and use this value as an increment to the hue value. This should give you evenly distributed colors that look good together since they are effectively the same saturation and brightness.

A Little More Detail

I took a look at the current .NET/C# API and it doesn't look like there is an easy way to create a random color from anything other than RGB values. Here's an example of the ramping that I described in Javascript+HTML. The translation from HSV or HSL space into RGB is language specific but the answers to Creating a C# Color from HSL values should help.

function setColor(id, hue) {
  var elm = document.getElementById(id);
  var saturation = document.getElementById("saturation").value;
  var lightness = document.getElementById("lightness").value;
  elm.style.color = "hsl(" + hue + ", " + saturation + "%, " +
                         lightness + "%)";
}

function setColors() {
    setColor("first", 0 * (360 / 5));
    setColor("second", 1 * (360 / 5));
    setColor("third", 2 * (360 / 5));
    setColor("fourth", 4 * (360 / 5));
}
<p id="first">First Color<p>
<p id="second">Second Color</p>
<p id="third">Third Color</p>
<p id="fourth">Fourth Color</p>

<form>
    Saturation: <input id=saturation value=75><br>
    Lightness: <input id=lightness value=40><br>
    <input type=submit onclick="javascript:setColors()" value=Update>
</form>
Community
  • 1
  • 1
D.Shawley
  • 58,213
  • 10
  • 98
  • 113
1

A suggestion along theses lines was made in the comments by @Tim Schmelter

This is NOT ideal as it does repeat, but at least the colours are distinct.

public Color GetNiceColor(int n)
{
    Color[] NiceColors = new Color[]{Color.Red,Color.Yellow,Color.Blue,Color.Green,Color.Brown,Color.DarkGray,Color.Orange,Color.Magenta,Color.Cyan,Color.Magenta,Color.Lime};
    int selection = n % NiceColors.Count();

    return NiceColors[selection];
}
ManInMoon
  • 6,795
  • 15
  • 70
  • 133
0

I don't think this is possible as that sequence of colours doesn't follow any order. I good trick for getting a nice set of colours I find is to find a colour you like - get its 3 R,G and B values - and... mix the order of the hex code pairs. so...

Colour1=C4 89 FF
Colour2=C4 FF 89
Colour3=FF 89 C4
Colour4=etc.

I don't have any reference to this - as this is just what I do if i need a quick sequence of colours that look ok together.

You could also look at colour ramps to get a nice sequence: eg. http://www.pixelfor.me/crc/F0000032

Or one of the many websites where people vote on sequences of colours that look good together eg. http://colorhunt.co/

Ozmo
  • 26
  • 3
0

Here is the C# version of D.Shawley's answer, with some tips of Nolonar.

using System.Runtime.InteropServices;

enum E_COLOR_TYPE
{
    DEFAULT,
    FIRST,
    SECOND,
    THIRD,
    //Add more item here...
    TOTAL,
}

static class ColorSetter
{
    [DllImport("shlwapi.dll")]
    private static extern int ColorHLSToRGB(int H, int L, int S);

    private static Color[] s_arrColor;

    static ColorSetter()
    {
        const int COLOR_COUNT = (int)E_COLOR_TYPE.TOTAL;
        s_arrColor = new Color[COLOR_COUNT];
        const int HUE_BASE = (240 / COLOR_COUNT);
        const int LIGHTNESS = 120;
        const int SATURATION = 240;
        for (int i = 0; i < s_arrColor.Length; ++i)
            s_arrColor[i] = ColorTranslator.FromWin32((ColorHLSToRGB(i * HUE_BASE, LIGHTNESS, SATURATION)));
    }

    public static Color GetColorType(E_COLOR_TYPE type)
    {
        return s_arrColor[(int)type];
    }
}

Note that if there are too many items in E_COLOR_TYPE, the colors won't be "nice" anymore. So far with this code, the colors are pretty "nice" to me.

123iamking
  • 2,387
  • 4
  • 36
  • 56