【发布时间】:2017-04-26 11:49:08
【问题描述】:
我想得到最接近这个列表的颜色:
static List<Color> clist = new List<Color>()
{
Color.Black, Color.Brown, Color.Blue, Color.Green, Color.Red, Color.Yellow,
Color.Gray, Color.Indigo, Color.Orange, Color.Pink, Color.Turquoise, Color.White
};
我得到了最接近的颜色,但我想添加:苍白/深色/鲜艳/浅色,具体取决于颜色的亮度和饱和度:
static string closestColor2(List<Color> colors, Color target)
{
var colorDiffs = colors.Select(n => ColorDiff(n, target)).Min(n => n);
int x = colors.FindIndex(n => ColorDiff(n, target) == colorDiffs);
return colors[x].ToString();
}
static int ColorDiff(Color c1, Color c2)
{
return (int)Math.Sqrt((c1.R - c2.R) * (c1.R - c2.R)
+ (c1.G - c2.G) * (c1.G - c2.G)
+ (c1.B - c2.B) * (c1.B - c2.B));
}
【问题讨论】:
-
您绝对必须计算 HSL 或 HSV 颜色空间中的距离(或者,如果您想获得真正的花哨,CIE 或 YUV 之类的)。如果您想获得有意义的结果,您不能使用 RGB 颜色空间。如果单独的色调还不够,也可以使用其他组件。分配给每个组件的相对权重取决于您。可能想要比其他人更重视亮度/价值。也许接下来是色调,最后是饱和度。
-
还有哪些其他组件?我是色彩操作编程的新手……
-
对于 HSL,组件将是色相、饱和度和亮度。就像 RGB 一样,分量是红色、绿色和蓝色。搜索“color distance c#”,前面有很多关于这个话题的讨论。