代码之家  ›  专栏  ›  技术社区  ›  mafu

如何生成这个特定的数字序列?

  •  2
  • mafu  · 技术社区  · 14 年前

    显然,在.net中没有预定义的列表可用。

    有没有办法找回这些“标准”颜色,或者我必须写下 IEnumerable<Color> 我自己?

    编辑:这是RGB值的可能输出。

    集合内的顺序 无所谓(即00 FF 00可能早于00 FF)。

    00 00 00 // start out with 00 and FF components
    00 00 FF
    00 FF 00
    FF 00 00
    FF FF 00
    FF 00 FF
    00 FF FF
    FF FF FF
    00 00 80 // ok, now add 80
    00 80 00
    ...
    80 80 80
    FF 00 80
    FF 80 00
    FF 80 80
    80 FF 00
    80 FF 80
    FF FF 80
    ...
    // now add 40 and B0
    
    4 回复  |  直到 14 年前
        1
  •  8
  •   Oded    14 年前

    Colors 类上有许多预定义的ARGB颜色,就像 Color 结构。这些东西像 Yellow , White Green

    如果需要用户定义的系统颜色,可以使用 SystemColors ActiveBorder , WindowText 等。。。


    框架中没有任何东西可以按ARGB值对颜色进行排序,因为这实际上没有多大意义

    可以使用Linq按颜色的组件对颜色列表进行排序( OrderBy 扩展方法)。

        2
  •  1
  •   Marius Schulz    14 年前

    SystemColors 上课对你来说是件好事。

        3
  •  1
  •   zgorawski    14 年前

        4
  •  1
  •   mafu    14 年前

    这是生成序列的一种相当快速的方法:

    public static IEnumerable<Color> StandardColors ()
    {
        int r = 0;
        int g = 0;
        int b = 0;
        int inc = 0x100;
    
        yield return Color.FromArgb (0, 0, 0);
    
        while (true) {
            if (((r | g | b) & inc) != 0) {
                int outR = r == 0 ? 0 : r - 1;
                int outG = g == 0 ? 0 : g - 1;
                int outB = b == 0 ? 0 : b - 1;
                yield return Color.FromArgb (outR, outG, outB);
            }
    
            r += inc;
            if (r > 256) {
                r = 0;
                g += inc;
    
                if (g > 256) {
                    g = 0;
                    b += inc;
    
                    if (b > 256) {
                        b = 0;
                        inc >>= 1;
    
                        if (inc <= 1) {
                            break;
                        }
                    }
                }
            }
        }
    }
    

    这当然可以改进。例如,应该避免使用单独的outR/G/B变量,并且应该通过2*inc从奇数开始递增(基于 inc )值,以避免必须测试该值是否已在前面生成。

    static void Main (string[] args)
    {
        var colors = StandardColorEnumerator.StandardColors ().Take (15)
            .Concat (StandardColorEnumerator.StandardColors ().Skip (1000).Take (10));
        foreach (var color in colors) {
            Console.WriteLine (color.B + "\t" + color.G + "\t" + color.R);
        }
    
        Console.ReadKey (true);
    }
    

    生成以下输出:

    0       0       0
    0       0       255
    0       255     0
    0       255     255
    255     0       0
    255     0       255
    255     255     0
    255     255     255
    0       0       127
    0       127     0
    0       127     127
    0       127     255
    0       255     127
    127     0       0
    127     0       127
    
    15      47      191
    15      47      207
    15      47      223
    15      47      239
    15      47      255
    15      63      0
    15      63      15
    15      63      31
    15      63      47
    15      63      63