# Random color code

**URL:** <https://forum.kirupa.com/t/random-color-code/8027>\
**Category:** Uncategorized\
**Created:** [November 12, 2002, 7:50pm UTC](https://forum.kirupa.com/t/random-color-code/8027 "2002-11-12T19:50:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shuga](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@shuga](https://forum.kirupa.com/u/shuga)\
**Post date:** [November 12, 2002, 7:50pm UTC](https://forum.kirupa.com/t/random-color-code/8027/1 "2002-11-12T19:50:40Z")

</div>

hey … pom, beta … someone;

shouldn’t this code

```auto

onClipEvent(enterFrame){
	this.setRGB(Math.random(16777215/11));
}

```

apply a random color to the movie clip it’s attached to when it enters the frame?

i’m trying to combine things i’ve learned from several tutorials to get an effect and i’ve almost got it =)

thanks in advance for the help

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 8:08pm UTC](https://forum.kirupa.com/t/random-color-code/8027/2 "2002-11-12T20:08:52Z")

</div>

onClipEvent( **enterFrame** ) {}  
executes the actions every frame. Like if your fps is 20 then the actions will be executed 20 times per second.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 8:21pm UTC](https://forum.kirupa.com/t/random-color-code/8027/3 "2002-11-12T20:21:41Z")

</div>

hey shuga!

u cant setRGB simply like this, it should look like:

```php
Color = new Color(Movie);
Color.setRGB(16777215);

```

i dont think math.random will work with that one, cuz it will create decimals!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 8:34pm UTC](https://forum.kirupa.com/t/random-color-code/8027/4 "2002-11-12T20:34:24Z")

</div>

I put this as a variable definition in the frame’s code

```auto

Color = new Color(image);
Color.setRGB(Math.random(16777215));

```

then i attached this code to the symbol itself

```auto

onClipEvent(enterFrame){
this.Color
}

```

and now it turns them all black … which means the color set is working but it’s not choosing a new random color each time.

and about it being executed 20 times a second. I have stops on each frame. the movie chooses one of 32 frames to randomly stop on but I want each frame to have a different color clip on it so I want it to set the color randomly each time it enters the frame

=)

help

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 8:38pm UTC](https://forum.kirupa.com/t/random-color-code/8027/5 "2002-11-12T20:38:14Z")

</div>

its Showing black cuz its a **DECIMAL**

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 8:39pm UTC](https://forum.kirupa.com/t/random-color-code/8027/6 "2002-11-12T20:39:52Z")

</div>

enterFrame has nothing to do with the timeline or frames.  
It just executes the actions as fast as the frames per second  
the timeline may not play but it still executes them so fast.  
and try making the setRGB action like this:

```auto
**myColor** = new Color(<movielip>);
**myColor**.setRGB( **0x** +(**Math.random()*(16777215/11)**));

```

but **0x** in front of the color value and I think you need to change the Color too cuz a function is named that way too. But maybe you don’t need to change it… experiment 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 8:46pm UTC](https://forum.kirupa.com/t/random-color-code/8027/7 "2002-11-12T20:46:58Z")

</div>

I believe setRGB is expecting hex.

Are you looking for one of 16 million colors? Just wondering?

you could also do this…

R = random number between 0 and 255

G = random number between 0 and 255

B = random number between 0 and 255

then use this formula

```auto

mycolor.setRGB(R<<16 | g<<8 | b);

```

try it out!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 8:55pm UTC](https://forum.kirupa.com/t/random-color-code/8027/8 "2002-11-12T20:55:33Z")

</div>

i’ll try it but i don’t understand why you did

R\<\<16 | g\<\<8 | b

explain what this does please =)

also, i think i’ve seen this somewhere before but i tried it and i couldnt make it work although i’m sure it’s my syntax. if it’s expecting hex why couldn’t i do random(x\*ffffff) ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 8:59pm UTC](https://forum.kirupa.com/t/random-color-code/8027/9 "2002-11-12T20:59:00Z")

</div>

ho yeah! setRGB is like 0xFFFFFF or 0x000000  
0xRRGGBB

400 POSTS  
 ![](http://www.kirupa.com/forums/smileys_files/banana.gif) ![](http://www.kirupa.com/forums/smileys_files/redblob.gif) ![](http://www.kirupa.com/forums/smileys_files/apple.gif) ![](http://www.kirupa.com/forums/smileys_files/FRO.gif)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 9:27pm UTC](https://forum.kirupa.com/t/random-color-code/8027/10 "2002-11-12T21:27:07Z")

</div>

happily you can!

myColor.setRGB(Math.random()\*0xffffff);

will work just dandy.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 9:33pm UTC](https://forum.kirupa.com/t/random-color-code/8027/11 "2002-11-12T21:33:53Z")

</div>

genius!!

that’s the code i was looking for.

thank you so much!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:06pm UTC](https://forum.kirupa.com/t/random-color-code/8027/12 "2002-11-12T22:06:27Z")

</div>

wow! You learn something new every day, especially from Supra.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:19pm UTC](https://forum.kirupa.com/t/random-color-code/8027/13 "2002-11-12T22:19:04Z")

</div>

i learned that from this board!

from a post by ily i think…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:30pm UTC](https://forum.kirupa.com/t/random-color-code/8027/14 "2002-11-12T22:30:01Z")

</div>

Ilyas put it up on Kirupas site under AS Tricks 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:30pm UTC](https://forum.kirupa.com/t/random-color-code/8027/15 "2002-11-12T22:30:11Z")

</div>

here’s the file if anyone wants to see how it’s done =)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:31pm UTC](https://forum.kirupa.com/t/random-color-code/8027/16 "2002-11-12T22:31:09Z")

</div>

Oh yeah… this also produce a very nice effect…

```auto
onClipEvent (enterFrame) {
	red = Math.sin(ra += .02)*127+128;
	green = Math.sin(rb += .05)*127+128;
	blue = Math.sin(rc += .09)*127+128;
	myCol = red << 16 | green << 8 | blue;
	myColored = new Color(this);
	myColored.setRGB(myCol);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:32pm UTC](https://forum.kirupa.com/t/random-color-code/8027/17 "2002-11-12T22:32:18Z")

</div>

good job, like it ;)!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:34pm UTC](https://forum.kirupa.com/t/random-color-code/8027/18 "2002-11-12T22:34:30Z")

</div>

neat.

Shuga, just a comment, if you don’t mind. Even Repeat visitors to your site will not notice that your rocks are random. I really like the concept, maybe you should change the rocks at randon intervals, or have them show up periodically…dunno… Again, nice concept.

Hope you don’t mind the humble suggestion.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:45pm UTC](https://forum.kirupa.com/t/random-color-code/8027/19 "2002-11-12T22:45:11Z")

</div>

thanks, i’ll try that.

so you don’t think the regenerate button in the bottom right will make a difference?

thanks beta, i’ll try that too =)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2002, 10:47pm UTC](https://forum.kirupa.com/t/random-color-code/8027/20 "2002-11-12T22:47:35Z")

</div>

No problem, I hope you enjoy that code 🙂

I believe I got it from Ilyas… 🙂

I made a correction to the code.

I accidently had onClipEvent (onEnterFrame)…lol, I am so used to using onEnterFrame = function() now… so I just removed the on and made the E an e.

[Next page](https://forum.kirupa.com/t/random-color-code/8027.md?page=2)
