生成独特的颜色 [英] Generating Unique Colors

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

问题描述

我刚刚写了一个小类,可以生成完全独特的颜色(或者仍然是计划)。我在网上找到了原始的索引移位代码段,只是想知道它是否可以得到改进,或者使用它是否会引起任何问题。

I just wrote a small class that generates perfectly unique colors(or that is the plan anyway). I found the original index shifting snippet online, and was just wondering if it could be improved in anyway, or if any problems could arise from using it.

public class UniqueColorGenerator
    {
        private int _colorIndex;
        public UniqueColorGenerator()
        {

        }

        public Color Next()
        {
            _colorIndex++;
            byte red = ( byte )( _colorIndex & 0x000000FF );
            byte green = ( byte )( ( _colorIndex & 0x0000FF00 ) >> 08 );
            byte blue = ( byte )( ( _colorIndex & 0x00FF0000 ) >> 16 );
            return Color.FromArgb( red , green , blue );
        }
    }

我将其用于OpenGL中的几何拾取 。它也很漂亮。这是一张16384个四边形的图片,它们有自己独特的颜色...或者我想我应该说红色和黑色的独特阴影。

I am using this for "geometry picking" in OpenGL. It works beautifully, too. Here is a picture of 16,384 quads with their own unique color...or I guess I should say "unique shade of red and black".

当我第一次运行它时,我发誓他们是明显的重复项。但是他们不是。采摘不仅做到了完美无瑕,而且我还跑遍了颜色,没有遇到任何碰撞。不过,我仍然想知道,生成器的当前生成方式是否会出现任何问题-除了达到整数最大值,这种情况也永远不会发生。

When I first ran it, I swore that their was obvious duplicates. But their isn't. Not only did the picking work flawlessly, but I also ran through the Colors and didn't get a single collision. I am still wondering, though, could any problems arise from how the generator is currently built—besides hitting integer max, which will also never happen.

伙计们知道我可以如何对颜色进行更多的随机化,所以我可以得到更多的颜色,而不是得到不同的红色阴影?

And also, do you guys know how I could randomize the colors more, so instead of getting different shades of red, I could recieve more colors along the spectrum?

推荐答案

我只想发布自己的小东西。我不会接受这个答案,但是我只是想使用我已经收到的建议来展示一个简单的模型。主要来自Codereview: Codereview链接

I just wanted to post my own little something. I won't be accepting this as the answer, but I just wanted to show a simple mockup using the suggestions that I had already recieved. Mainly from Codereview: Codereview Link

public class UniqueColorGenerator {
        private int _colorIndex;
        private readonly int _offsetIncrement;

        public UniqueColorGenerator() {
            this._offsetIncrement = 1;
        }

        public UniqueColorGenerator( uint offsetIncrement ) {
            this._offsetIncrement = ( int )offsetIncrement;
        }

        public Color Next() {
            return Color.FromArgb( _colorIndex += _offsetIncrement );
        }
    }

该类与我的原始示例完全一样,但要简单得多,甚至可以增加一些独特性。您在构造函数中添加的数字越高,最终模式就越疯狂。这是一个简单的示例,说明如果将 2834783输入到构造函数中会发生什么情况:

That class does the exact same thing as my original example, but is a lot simpler, and even adds room for a bit of uniqueness. The higher number that you add in the constructor, the more crazy the final pattern becomes; here is a quick example of what happens if you feed "2834783" into the constructor:

是的,所有颜色仍然是唯一的。我可以从此新代码中看到的唯一问题是,如果用户输入高得离谱的数字,就会出现此问题。

And yes, all of the colors are still unique. The only issue that I can see from this new code is an issue that would arise if the user put in a ridiculously high number.

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

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