简体   繁体   English

C# - 如何按彩虹顺序订购 colors 列表?

[英]C# - How do I order a list of colors in the order of a rainbow?

From a list of images, I calculated the average color using a method similar to this .从图像列表中,我使用与此类似的方法计算了平均颜色。 I now have a list of System.Drawing.Color , but I'm not sure how to sort them in a way that it looks like a rainbow.我现在有一个System.Drawing.Color列表,但我不确定如何以看起来像彩虹的方式对它们进行排序。

Here is something simple I have tried (slightly changed for example purposes):这是我尝试过的简单方法(出于示例目的略有更改):

var colorList = new List<Color>
{
    Color.Red,
    Color.Purple,
    Color.Black,
    Color.Blue,
    Color.Green,
    Color.LightGreen,
    Color.LightSkyBlue,
    Color.Yellow
};

var orderedColorList =
    colorList.OrderBy(o => (o.R * 3 + o.G * 2 + o.B * 1));

This does not seem to create a rainbow but more of an effect from black to white.这似乎并没有创造出彩虹,而是更多的从黑到白的效果。

How would I be able to sort them in a way that results in a rainbow?我如何能够以产生彩虹的方式对它们进行排序?

If you order by the Hue first, and then RGB , you should get a rainbow ordering:如果您Hue排序,然后按RGB排序,您应该得到彩虹排序:

var orderedColorList = colorList
    .OrderBy(color => color.GetHue())
    .ThenBy(o => o.R * 3 + o.G * 2 + o.B * 1);

Using a mixed-up collection of rainbow colors, this appears to do the trick:使用彩虹 colors 的混合集合,这似乎可以解决问题:

var colorList = new List<Color>
{
    Color.LightSkyBlue,
    Color.Red,
    Color.Yellow,
    Color.Purple,
    Color.Orange,
    Color.Blue,
    Color.Green
};

var orderedColorList = colorList
    .OrderBy(color => color.GetHue())
    .ThenBy(o => o.R * 3 + o.G * 2 + o.B * 1);

foreach (var color in orderedColorList)
{
    Console.WriteLine(color.Name);
}

Output Output

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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