Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random background color 2

Status
Not open for further replies.

firegambler

Technical User
Sep 25, 2002
455
AT
hi out there

i'd like so set the rectangle i put on the stage to be my background to random-color when the film is loaded.

the problems are the following:

i didn't manage to initiate an operation when the film loads (via onclipevent(load) )

i don't know how to transfer a math.random()-number into a hexadecimal value (and how to put a function producing such a value into the setRGB()-function in a way that it is accepted by setRGB)

thanks


Firegambler
 
Try this as a way of converting to RGB values:

r=255;//red value
g=255;//green value
b=0;//blue value
//convert to hexadecimal
rgb = (r << 16 | g << 8 | b);
trace(rgb.toString(16));
 
Here's the whole thing - turn your background fill into a movieclip and attach this code:

onClipEvent (load) {
//colour values
r = Math.round(Math.random()*255);
g = Math.round(Math.random()*255);
b = Math.round(Math.random()*255);
//
//convert to hexadecimal
rgb = (r << 16 | g << 8 | b);
rgb = '0x'+rgb;
//
myColour = new Color(this);
myColour.setRGB(rgb);
}
 
hi wangbar (and all the others who know how to do that),

the whole thing works really fine.

just one thing: in flash's action script reference those things like << are explained in a more, how should i say, opaque way.

would you mind explaining me how that converting works?
(i mean &quot;by hand&quot; i know how to put decimal to hexadecimal values as well as putting decimal to binary code but that script really ripped my mind)

thanks firegambler
 
Basically you're shifting a binary representation of the number.

First of all replace 'rgb = '0x'+rgb;' with the earlier script's '(rgb.toString(16));' because it gives a true hex value (I was experimenting and the second version seemed to work but on further investigation the old method is better).

Since RGB values are essentially three numbers giving individual values from 0 to 255 combining the three into one big base ten number to convert it to hex would require multiplying the red value by one amount, the green by another and leaving the blue alone then adding them together and converting to base 16.

Bitwise shifting (<<) is a quick way around this: every shift to the left doubles the number. So double R 16 times (multiply by 65536), double G 8 times (multiply by 256) and then you use the OR '|' operator to add them together (binary addition is a bit different than you expect). The result of this operation when converted to hex is your colour.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top