Reputation: 7005
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.
Upvotes: 1
Views: 1017
Reputation: 2703
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.
Upvotes: 0
Reputation: 7005
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];
}
Upvotes: 1
Reputation: 59553
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>
Upvotes: 1
Reputation: 26
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/
Upvotes: 0