为每个字符串生成特定的颜色? [英] Generate a specific color for each string?

查看:637
本文介绍了为每个字符串生成特定的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid显示属于产品的订单。

I have a DataGrid that shows orders that belong to product.

我想有一个生成每个产品独有的SolidColorBrush

更新

我需要这些颜色是坚实的和独特的,或至少是以独特的方式排序,是黑色,蓝色,绿色,因为这3种颜色可以互相混淆。

I need these colors to be solid and distinctive, or at least to be ordered in a distinctive way, i.e. there shouldn't be black, blue, green as these 3 colors can be confused with each other. Besides it should be black, red, blue etc.

因此基本上公式必须是:

So basically the formula has to be:


  1. 将所有颜色分组为主色的子组

  2. 按照黑暗顺序排列

  3. 再次订购乒乓交替方式

  4. 我注意到有一个很酷的功能: Color.AreClose ,它可能有用

  1. Group all the colors into subgroups of main colors
  2. Order them by darkness
  3. Order it again the ping-pong alternation way (dark-light dark-light etc.) with the pervious group.
  4. I just noticed there is a cool function: Color.AreClose, it might be useful, I still donno how to implement it.
  5. I am sure you have more ideas.



>

相关:是否有仅包含256种颜色的.NET列表?


推荐答案

有一个粗略的色差公式


色差由以下公式确定:
:(最大值(红色值
1,红色值2) - 最小值(红色值
1,红色值2))+(最大值(绿色
值1,绿色值2)
(绿色值1,绿色值2))+
(最大值(蓝色值1,蓝色值2)
-最小值(蓝色值1,蓝色值2))

Color difference is determined by the following formula: (maximum (Red value 1, Red value 2) - minimum (Red value 1, Red value 2)) + (maximum (Green value 1, Green value 2) - minimum (Green value 1, Green value 2)) + (maximum (Blue value 1, Blue value 2) - minimum (Blue value 1, Blue value 2))

颜色亮度
差异的范围是125.颜色
差异的范围是500.

The rage for color brightness difference is 125. The range for color difference is 500.

还有其他更复杂的

然后,您可以在一个算法中使用该函数来存储集合中的所有可能的颜色,从该集合中选择一种颜色),并将其作为迭代排序的起点。

You might then use that function in an algorithm that would store all the possible colors in a set, choose one color from that set (say black), and use that as a starting point for an iterative sort.

这既不是优化也不是测试,但它可能工作:

This is neither optimized nor tested, but it might work:

static List<Color> SortColors(IEnumerable<Color> colors)
{
    var hashed = new HashSet<Color>(colors);
    var sorted = new List<Color>(hashed.Count);

    //Start with black
    var last = hashed.Single(x => x.Red == 0 && x.Green == 0 && x.Blue == 0);
    hashed.Remove(last);
    sorted.Add(last);

    while (hashed.Any())
    {
        //This could of course be optimized by doing these two steps in a single loop
        var bestDiff = hashed
            .Max(x => Difference(x, last));
        var best = hashed.First(x => Difference(x, last) == bestDiff);

        hashed.Remove(best);
        sorted.Add(best);
        last = best;
    }

    return sorted;
}

static int Difference(Color a, Color b)
{
    return Math.Abs(a.Red - b.Red)
        + Math.Abs(a.Green - b.Green)
        + Math.Abs(a.Blue - b.Blue);
}

这篇关于为每个字符串生成特定的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆