C# 如何将颜色数组设置为另一个颜色数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1001577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:15:50  来源:igfitidea点击:

How to set color array into another color arrays?

c#.netasp.netarrays

提问by Penguen

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.

我下面的代码给了我错误:“索引超出了数组的范围。” 我的算法创建了 Colorset 数组,即数组维度“16”,但我需要第二个“colorSetLegend”,即维度:32,如果您查看下面返回错误的粗体代码。

 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;
    }
}

采纳答案by Jon Skeet

You're incrementing i in your innerloop. I suspect you meant to do it in your outerloop - otherwise during oneiteration of your outer loop, you're incrementing imany times, until you exceed the bounds of the array.

你在你的内部循环中增加 i 。我怀疑您打算在外循环中执行此操作 - 否则在外循环的一次迭代中,您会递增i很多次,直到超出数组的边界。

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

或者,您可以for像其他人一样编写循环:

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 colorSetLegendall over again. What are you trying to accomplish?

话虽如此,鉴于循环内的第一行设置colorSetLegend[j]和第二行再次设置相同的元素,代码有点毫无意义。此外,在外循环的下一次迭代中,您将colorSetLegend再次覆盖所有值。你想达到什么目的?

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 forloop, I get nervous
  • Using pre- and post-increment operators in another expression is easy to get wrong.
  • 考虑循环的嵌套级别。你真的想在这里有两个循环吗?
  • 尝试使用传统习语进行循环 - 每当我在循环开始时看到一个空位时for,我就会感到紧张
  • 在另一个表达式中使用前置和后置递增运算符很容易出错。

回答by Lazarus

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;
}