My below Codes gives me error:”Index was outside the bounds of the array.” My Algorithms create Colorset arrays that’s arrays dimention ’16’,
But i need Second one ‘colorSetLegend’ that’s dimensions:32 if you look below Bold codes that returns me error.
Color[] colorSetLegend = new Color[32];
Color[] colorSet = { Color.Red, Color.Blue, Color.Green, Color.Yellow };
Color end = Color.White;
colorSet = ColorMaker.GenerateColor(colorSet, end);
for (int i = 0; i < colorSet.Length; )
{
for (int j = 0; j < colorSetLegend.Length; )
{
colorSetLegend[j] = colorSet[i];
colorSetLegend[j++] = Color.Black;
i++;
}
}
My Color generator below:
public class ColorMaker
{
public static Color[] GenerateColor(Color[] baseColorSet, Color end)
{
Color[] colorSet = new Color[16];
int j = 0;
foreach (Color start in baseColorSet)
{
for (int i = 0; i < 15; i += 4)
{
int r = Interpolate(start.R, end.R, 15, i),
g = Interpolate(start.G, end.G, 15, i),
b = Interpolate(start.B, end.B, 15, i);
colorSet[j] = Color.FromArgb(r, g, b);
j++;
}
}
return colorSet;
}
static int Interpolate(int start, int end, int steps, int count)
{
float s = start, e = end, final = s + (((e - s) / steps) * count);
return (int)final;
}
}
You’re incrementing i in your inner loop. I suspect you meant to do it in your outer loop – otherwise during one iteration of your outer loop, you’re incrementing
imany times, until you exceed the bounds of the array.Alternatively, you could write your
forloops the same way everyone else does:Having said that, the code’s a bit pointless given that the first line inside the loop sets
colorSetLegend[j]and the second line sets the same element again. Furthermore, on the next iteration of the outer loop you’ll be overwriting all the values incolorSetLegendall over again. What are you trying to accomplish?Marc made a good-looking guess at your aim here (although he’s now deleted his answer!)
Here’s his guess at working code for what you want:
A few things to learn from this, if he’s right:
forloop, I get nervous