Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

setGradientBG.as

Name: setGradientBG() [MX] adds a two color gradient background to a textField
Author: senocular: www.senocular.com
Date: 1899-12-31T00:28:18.100
Documentation:
TextField SETGRADIENTBG: sets a 2 color fading gradient
background to a textfield.

TextFieldInstance.setGradientBG(hexColor,hexColor [,rotation] [,depth]);
TextFieldInstance.setGradientBG([hexColor, alpha], [hexColor,alpha] [,rotation] [,depth]);


Arguments:
- color1, color2:  color specifications for the colors in the fade. At 0 rotation, color1 is
the top-most color in the fade.  The value used here can be either a hex color or an array
with a hex value and an alpha value in the form of [hex, alpha], where the color will then
also be displayed with the given alpha in the fade.
- rotation: (optional) specified rotation of the gradient in degrees.  Default: 0 degrees.
- depth: (optional) specified depth of the gradient.  The gradient itself is not part of the
textField, rather a movieclip created to be the size and shape of the textfield used as a
holder for the graident fill itself.  This depth specifies the depth of that movieclip, 
created in the same scope of the textField itself.  If not specified, the depth becomes the
depth of the textField - 1.

Returns:
- the movieclip reference of the background clip.  If you move, hide or in some way alter the
textfield, you may want to do the same with the background so running this function will
return a reference to that background as it is, afterall, seperate.

Example:
this.createTextField("my_txt",10,100,100,200,25);
my_txt.text = "GREETINGS"
// create a gradient background from red to a blue with 20% alpha
// set at a 0 rotation (up and down fade)
my_txt.setGradientBG(0xff0000,[0x0000ff,20],0);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
TextField.prototype.getDepth = MovieClip.prototype.getDepth;
TextField.prototype.setGradientBG = function(color1, color2, rotation, depth){
	var bg, a1, a2;
	if (color1 instanceof Array){
		a1 = color1[1];
		color1 = color1[0];
	}else a1 = 100;
	if (color2 instanceof Array){
		a2 = color2[1];
		color2 = color2[0];
	}else a2 = 100;
	rotation = (rotation) ? (rotation+90)*Math.PI/180 : 0;
	if (depth == undefined) depth = this.getDepth()-1;
	if (depth > this.getDepth()) trace("Warning: background gradient created for "+this._name+" set at depth greater than textfield depth, text may be hidden.");
	with (bg = this._parent.createEmptyMovieClip(this._name+"grad_bg",depth)){
		var matrix = {matrixType:"box", x:this._x, y:this._y, w:this._width, h:this._height, r:rotation}; 
		beginGradientFill("linear", [color1,color2], [a1,a2], [0,255], matrix);
		moveto(this._x,this._y);
		lineto(this._x+this._width, this._y);
		lineto(this._x+this._width, this._y+this._height);
		lineto(this._x, this._y+this._height);
		lineto(this._x, this._y);
		endFill();
	}
	return bg;
}