A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] choose random color

  1. #1
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588

    resolved [RESOLVED] choose random color

    I'm using the following code to randomly choose a color problem is the same color is being chosen too close togeather. does anybody know how to write this so that the colors are extremely randomly chosen? I know this is vauge but be creative

    PHP Code:
    var userCol =0x35E5ED
    var ranCol Math.floor(Math.random()*10-Math.random()*3);
                
    trace(ranCol);
                switch (
    ranCol) {
                    case -
    :
                        
    userCol =0x35E5ED;
                        break;


                    case 
    :
                        
    userCol =0x35E5ED;
                        break;

                    case 
    :
                        
    userCol =0x020C0C;
                        break;

                    case 
    :
                        
    userCol =0xFB4411;
                        break;

                    case 
    :
                        
    userCol =0x12F728;
                        break;

                    case 
    :
                        
    userCol =0x12D8F7;
                        break;

                    case 
    :
                        
    userCol =0x2709FD;
                        break;

                    case 
    :
                        
    userCol =0xEE00FF;
                        break;

                    case 
    :
                        
    userCol =0xCD23CB;
                        break;

                    case 
    :
                        
    userCol =0xED0E29;
                        break;

                    case 
    :
                        
    userCol =0x4F4546;
                        break;

                    case 
    10 :
                        
    userCol =0x454616;
                        break;

                } 
    ~calmchess~

  2. #2
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    Based on the look of it, you want to randomly choose between a set number of colors?

    Math.random() is really the only function, uh, hmm..

    Nothing much really comes to mind. You can do some array creation, popping, etc to make sure no dupes come up and further flip the coin, but you're really stuck with computer "random" which really isn't.

    Oh, and FYI this gives ranges below -1. Not sure if you're set up for that.
    I (Love | Hate) Flash.
    ----
    Save a version back so others may help you!

  3. #3
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    you could push each color into an array, then remove each one as it's served. that way you'll reduce the chance of getting the same color twice in a row...

    Code:
    var colors:Array = [0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF];
    var holder:Array = [];
    
    holder.push(colors.splice(Math.round(Math.random()*(colors.length-1)), 1));
    var userCol:uint = holder[holder.length-1];
    trace(userCol);
    
    if(!colors.length){
    	colors = holder;
    	holder = [];
    }
    Otherwise, if you want a truly random color, then just generate some RGB values...

    Code:
    var r:uint = Math.floor(Math.random()*0xff);
    var g:uint = Math.floor(Math.random()*0xff);
    var b:uint = Math.floor(Math.random()*0xff);
    var color:uint = r << 16 | g << 8 | b;
    
    trace(color);
    Last edited by Ralgoth; 12-30-2009 at 09:12 AM. Reason: forgot a bracket
    Search first, asked questions later.

  4. #4
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    thanks ralgoth i think i'll take your suggestion and create some random rgb colors didn't think of doing it that way.
    ~calmchess~

  5. #5
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    Fabulous code Fabulous I say
    ~calmchess~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center