AS3中的随机十六进制颜色生成器

时间:2014-06-05 02:53:12

标签: actionscript-3 random flashdevelop

我想创造能够随机使用" (伪随机)颜色生成并提出此代码旨在创建所有颜色和任何颜色。

我对编程非常陌生,想看看是否有人在S.O.有任何评论或批评,代码工作得很好。唯一的问题是颜色偶尔太相似,难以区分它们。

我知道这可能是一种非常强力的编码方式,但却是我的想法。

十六进制发生器

public class colorGenerator 
{
    public var color:int;
    private var randomnumber:Number;
    private var first:String = "";

    public function colorGenerator():void
    {
        var colorstring:String = "0x";
        var transfer:String = "0x";

        for ( var i:uint = 0; i < 6; i++)
        {
            randomhex();

            colorstring += first;   
        }

        transfer = colorstring;
        color = int(transfer);
    }

    public function randomhex():void
    {
        randomnumber = Math.random();


        if ( -1 < randomnumber < ((.99 / 16) * 1))
            first = "0";

        else if ( ((.99/16)*1) < randomnumber < ((.99/16)*2))
            first = "1";

        else if ( ((.99/16)*2)< randomnumber < ((.99/16)*3))
            first = "2";

        else if ( ((.99/16)*3)< randomnumber < ((.99/16)*4))
            first = "3";

        else if ( ((.99/16)*4)< randomnumber < ((.99/16)*5))
            first = "4";

        else if ( ((.99/16)*5)< randomnumber < ((.99/16)*6))
            first = "5";

        else if ( ((.99/16)*6)< randomnumber < ((.99/16)*7))
            first = "6";

        else if ( ((.99/16)*7)< randomnumber < ((.99/16)*8))
            first = "7";

        else if ( ((.99/16)*8)< randomnumber < ((.99/16)*9))
            first = "8";

        else if ( ((.99/16)*9)< randomnumber < ((.99/16)*10))
            first = "9";

        else if ( ((.99/16)*10)< randomnumber < ((.99/16)*11))
            first = "A";

        else if ( ((.99/16)*11)< randomnumber < ((.99/16)*12))
            first = "B";

        else if ( ((.99/16)*12)< randomnumber < ((.99/16)*13))
            first = "C";

        else if ( ((.99/16)*13)< randomnumber < ((.99/16)*14))
            first = "D";

        else if ( ((.99/16)*14)< randomnumber < ((.99/16)*15))
            first = "E";

        else if ( ((.99/16)*15)< randomnumber < 2)
            first = "F";
        }
    }

然后我只将十六进制值分配给另一个类中的变量

var acolor:colorGenerator = new colorGenerator;
var COLOR:uint = acolor.color

感谢您的任何评论!

2 个答案:

答案 0 :(得分:8)

这应该也可以。

Math.random() * 0xFFFFFF;

答案 1 :(得分:1)

未经测试,但这应该更多&#34;随机&#34;:

var red : int = Math.floor(Math.random()*255);
var green : int = Math.floor(Math.random()*255);
var blue : int = Math.floor(Math.random()*255);
var color : int = red << 16 | green << 8 | blue;