# Problem getting a random gradient fill to work

**URL:** <https://forum.kirupa.com/t/problem-getting-a-random-gradient-fill-to-work/34851>\
**Category:** Uncategorized\
**Created:** [November 7, 2003, 2:26am UTC](https://forum.kirupa.com/t/problem-getting-a-random-gradient-fill-to-work/34851 "2003-11-07T02:26:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CandyMann\_69](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@CandyMann\_69](https://forum.kirupa.com/u/CandyMann_69)\
**Post date:** [November 7, 2003, 2:26am UTC](https://forum.kirupa.com/t/problem-getting-a-random-gradient-fill-to-work/34851/1 "2003-11-07T02:26:23Z")

</div>

Any ideas?

This works with having normal gradient fill with color:  
[AS]  
color\_red = 0xFF0000;  
color\_blue = 0x0000FF;  
\_root.gear\_x\_width = Math.floor(Math.random()_60)+10;  
\_root.gear\_y\_width = Math.floor(Math.random()20+\_root.gear\_x\_width;  
\_root.gear\_num\_spokes = Math.floor(Math.random()5)+6;  
\_root.gear\_inside = Math.floor(Math.random()\_root.gear\_x\_width/8)+\_root.gear\_x\_width/6;  
\_root.gear\_outside = Math.floor(Math.random()\_root.gear\_x\_width/10)+\_root.gear\_x\_width/10;  
//  
with (\_root[“gear”+1+i]) {  
lineStyle(1, color\_red, 100);  
colors = [color\_blue, color\_red];  
alphas = [100, 100];  
ratios = [0, 255];  
matrix = {a:0, b:\_root.gear\_y\_width_1.5, c:0, d:\_root.gear\_x\_width\*1.5, e:0, f:0, g:0, h:0, i:1};  
beginGradientFill(“radial”, colors, alphas, ratios, matrix);  
draw\_gear(0, 0, gear\_x\_width, gear\_y\_width, gear\_num\_spokes, gear\_inside, gear\_outside);  
endFill();  
}  
[/AS]

This code just gives me black inside, but the fill color on the outside line is random.

```auto

_root.gear_x_width = Math.floor(Math.random()*60)+10;
_root.gear_y_width = Math.floor(Math.random()*20)+_root.gear_x_width;
_root.gear_num_spokes = Math.floor(Math.random()*5)+6;
_root.gear_inside = Math.floor(Math.random()*_root.gear_x_width/8)+_root.gear_x_width/6;
_root.gear_outside = Math.floor(Math.random()*_root.gear_x_width/10)+_root.gear_x_width/10;
//
with (_root["gear"+1+i]) {
	fill_color_a = "0x"+(Math.floor(Math.random()*255)+1).toString(16) +
(Math.floor(Math.random()*255)+1).toString(16) +
(Math.floor(Math.random()*255)+1).toString(16);
	fill_color_b = "0x"+(Math.floor(Math.random()*255)+1).toString(16) +
(Math.floor(Math.random()*255)+1).toString(16) +
(Math.floor(Math.random()*255)+1).toString(16);
	lineStyle(1, fill_color_a, 100);
	colors = [fill_color_b, fill_color_a];
	alphas = [100, 100];
	ratios = [0, 255];
	matrix = {a:0, b:_root.gear_y_width*1.5, c:0, d:_root.gear_x_width*1.5, e:0, f:0, g:0, h:0, i:1};
	beginGradientFill("radial", colors, alphas, ratios, matrix);
	draw_gear(0, 0, gear_x_width, gear_y_width, gear_num_spokes, gear_inside, gear_outside);
	endFill();
}

```

---

<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 7, 2003, 11:58pm UTC](https://forum.kirupa.com/t/problem-getting-a-random-gradient-fill-to-work/34851/2 "2003-11-07T23:58:18Z")

</div>

Any one? Why does the random color var not work, but a color var works?

---

<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 8, 2003, 12:21am UTC](https://forum.kirupa.com/t/problem-getting-a-random-gradient-fill-to-work/34851/3 "2003-11-08T00:21:29Z")

</div>

Although the code seems to be fine… try this:

```auto
fill_color_a = Math.floor(Math.random()*255)+1 << 16 | Math.floor(Math.random()*255)+1 << 8 | Math.floor(Math.random()*255)+1;
fill_color_b = Math.floor(Math.random()*255)+1 << 16 | Math.floor(Math.random()*255)+1 << 8 | Math.floor(Math.random()*255)+1;

```

And it would be easier for us if you could provide your FLA. 🙂

---

<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 11, 2003, 3:21am UTC](https://forum.kirupa.com/t/problem-getting-a-random-gradient-fill-to-work/34851/4 "2003-11-11T03:21:09Z")

</div>

Wonderful!!! That fixed it!

So the \<\< 16 moves the binary over 16 so the next group of numbers does not over write it? Then the | is or, so kind of simular to the add (+)?

Probably not the best definition 😕

Thanks again!

---

<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 11, 2003, 3:25am UTC](https://forum.kirupa.com/t/problem-getting-a-random-gradient-fill-to-work/34851/5 "2003-11-11T03:25:08Z")

</div>

Here is the draw gear code. If you want still want the flash file, I’m sure I can upload it. 🙂

[AS]  
// Draw Gear Funciton  
MovieClip.prototype.draw\_gear = function(x, y, x\_width, y\_width, num\_spokes, inside, outside) {  
// Keep the number of spokes greater then 4  
if (num\_spokes\<4) {  
num\_spokes = 4;  
}  
// Set the add angle by the number of spokes  
angle\_add = 360/Math.round(num\_spokes)/2;  
// Control point time number  
a = Math.tan(angle\_add/4_Math.PI/180);  
// Set outside gear width  
outside\_width\_x = x\_width/2+outside;  
outside\_width\_y = y\_width/2+outside;  
// Set start point  
this.moveTo(x+x\_width/2, y);  
// Step threw the angle to draw outside spokes  
for (angle=angle\_add; angle\<=360; angle += angle\_add) {  
x\_start\_point = x+x\_width/2_Math.cos(angle_Math.PI/180);  
y\_start\_point = y+y\_width/2_Math.sin(angle_Math.PI/180);  
x\_control\_point = x\_start\_point+x\_width/2_a_Math.cos((angle-90)Math.PI/180);  
y\_control\_point = y\_start\_point+y\_width/2a_Math.sin((angle-90)_Math.PI/180);  
// Add a curve  
this.curveTo(x\_control\_point, y\_control\_point, x\_start\_point, y\_start\_point);  
// Add a spoke  
x\_start\_point = x+outside\_width\_x_Math.cos((angle+angle\_add/4)_Math.PI/180);  
y\_start\_point = y+outside\_width\_y_Math.sin((angle+angle\_add/4)_Math.PI/180);  
this.lineTo(x\_start\_point, y\_start\_point);  
x\_start\_point = x+outside\_width\_x_Math.cos((angle+angle\_add_3/4)Math.PI/180);  
y\_start\_point = y+outside\_width\_yMath.sin((angle+angle\_add_3/4)_Math.PI/180);  
this.lineTo(x\_start\_point, y\_start\_point);  
x\_start\_point = x+x\_width/2_Math.cos((angle+angle\_add)_Math.PI/180);  
y\_start\_point = y+y\_width/2_Math.sin((angle+angle\_add)_Math.PI/180);  
this.lineTo(x\_start\_point, y\_start\_point);  
// Add to the angle  
angle += angle\_add;  
}  
//  
// Control point time number  
a = Math.tan(22.5_Math.PI/180);  
// Set inside gear width  
inside\_width\_x = x\_width/2-inside;  
inside\_width\_y = y\_width/2-inside;  
// Set start point  
this.moveTo(x+inside\_width\_x, y);  
// Step threw the angle to draw inside  
for (angle=45; angle\<=360; angle += 45) {  
x\_start\_point = x+inside\_width\_x_Math.cos(angle_Math.PI/180);  
y\_start\_point = y+inside\_width\_y_Math.sin(angle_Math.PI/180);  
x\_control\_point = x\_start\_point+inside\_width\_x_a_Math.cos((angle-90)_Math.PI/180);  
y\_control\_point = y\_start\_point+inside\_width\_y_a\*Math.sin((angle-90)\*Math.PI/180);  
// Add a curve  
this.curveTo(x\_control\_point, y\_control\_point, x\_start\_point, y\_start\_point);  
}  
//  
};  
//  
[/AS]

---

<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, 2003, 12:45am UTC](https://forum.kirupa.com/t/problem-getting-a-random-gradient-fill-to-work/34851/6 "2003-11-12T00:45:01Z")

</div>

I’ve seen a really good explanation about the bitwise stuff, let me find the link for you. Meantime, look up the bitwise OR and bitwise left shift operators in the AS Dictionary (if you haven’t done so already). 😉

And you’re welcome. :beam:
