对Unity
Colors
的一维数组进行排序的有效方法是什么(它们是RG?我有一个921,600 Color
的数组(1280 x 720像素图片)对于这种类型的基数排序还是你知道其他任何有效的方法来排序那么多颜色吗?
通过排序我的意思是说我有一些颜色,(123,17,2),(5,16,16),(5,2,150)等。为了排序这些我按R排序然后G然后B(有可能是一个名字,但我不知道)所以排序的列表将是(5,2,150),(5,16,16),(123,17,2)。
答案 0 :(得分:1)
我没有看到任何其他方式,然后蛮力。最好的方法是使用评论部分中所述的Array.Sort<T>
。
创建此排序方法:
private int SortColors(Color a, Color b)
{
if (a.r < b.r)
return 1;
else if (a.r > b.r)
return -1;
else
{
if (a.g < b.g)
return 1;
else if (a.g > b.g)
return -1;
else
{
if (a.b < b.b)
return 1;
else if (a.b > b.b)
return -1;
}
}
return 0;
}
然后就这样使用它:
Color[] colors = new Color[] { new Color (5, 2, 150), new Color (5, 16, 16), new Color (123, 17, 2) };
Array.Sort<Color> (colors, SortColors);
答案 1 :(得分:0)
如果你只是想找到最聪明的&#34;颜色。你不需要对它们进行排序。你可以通过它们循环找到最亮的。
这是非常非常伪代码,但你明白了。
Color brightest = new Color(0, 0, 0);
for (int i = 0; i < count; i++)
{
Color current = list[i];
if (current > brightest) // whatever your comparing means
{
brightest = current;
}
}