简体   繁体   中英

How to set color array into another color arrays?

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++] = 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 i many times, until you exceed the bounds of the array.

Alternatively, you could write your for loops the same way everyone else does:

for (int i = 0; i < colorSet.Length; i++)
{
    for (int j = 0; j < colorSetLegend.Length; j++)
    {
        colorSetLegend[j] = colorSet[i];
        colorSetLegend[j] = Color.Black;
    }
}

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 in colorSetLegend all 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:

for (int i = 0; i < colorSet.Length; i++)
{
    colorSetLegend[i*2] = colorSet[i];
    colorSetLegend[(i*2)+1] = Color.Black;
}

A few things to learn from this, if he's right:

  • Think about the level of nesting of your loops. Did you really mean to have two loops here?
  • Try to use conventional idioms for looping - whenever I see an empty bit at the end of the start of a for loop, I get nervous
  • Using pre- and post-increment operators in another expression is easy to get wrong.

This will achieve what you are looking for:

int j = 0;
for (int i = 0; i < colorSet.Length; i++)
{
    colorSetLegend[j++] = colorSet[i];
    colorSetLegend[j++] = Color.Black;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM