« Back to blog

Random Hexadecimal Colors in ActionScript 3.0 Code and Some Resulting Patterns

So far in this project, I have some of the simple shapes down.. circles, hexagons, stars and dodecagons (though they're still not lining up) and each shape has a unique color transparency, line thickness and line color.  Overlapping each other, they make some pretty cool, though still very simple, patterns.  However, all the colors had been entered in manually for each shape in hexadecimal: 0x000000 for black through all combinations of red (the first two values after the 'x'), green (the second two) and blue (third) up to  0xFFFFFF for white. 

Instead of going through and manually changing the colors through the code, I wanted to have them randomly chosen and plugged in every time the program ran.  This is usually pretty easy for a normal value (the value = Math.random()*(the upper limit - the lower limit)+the lower limit), but in this case, I needed to convert to hexadecimal.  Here is what I did:

public function getColorValue():String
        {
            var a:String = randomColorValue();
            var b:String = randomColorValue();
            var c:String = randomColorValue();
            var d:String = randomColorValue();
            var e:String = randomColorValue();
            var f:String = randomColorValue();
            
            var colorValue:String = "0x"+a+b+c+d+e+f;
            trace(colorValue);
            return colorValue;
            
        }
public function randomColorValue():String
        {
            var getNumber = Math.floor(Math.random()*(15-1))+1;
            if (getNumber == 10) { getNumber = "A"; }
            if (getNumber == 11) { getNumber = "B"; }
            if (getNumber == 12) { getNumber = "C"; }
            if (getNumber == 13) { getNumber = "D"; }
            if (getNumber == 14) { getNumber = "E"; }
            if (getNumber == 15) { getNumber = "F"; }
            
            return getNumber;
        }

Each value of the hexadecimal color code is given a string variable that is randomly generated from the "randomColorValue" function.  It remains a string so that it can both be a number, 0-9, or a letter (A-F), and that at the end of the function, the strings can be added together as one.

The last thing that needed to be done was at the point that the variable colorValue was used.  It is still a string here, but the hexadecimal actually reads as an integer (strange, but convenient), so the string has to be turned into a number value when it is used, so:

var fillColorOne:uint = int ( getColorValue() );
var fillColorTwo:uint = int ( getColorValue() );

The return from function getColorValue is converted as it fills the variables for color.  Probably not as cool as I think it is, but I am happy with the result.  All the examples are based on the same layout, but the variation in color and line thickness really changes a lot.

       
Click here to download:
Random_Hexadecimal_Colors_in_A.zip (463 KB)