/* file : BKG.as version : 1.0 Created by Cousot Stephane on Tue Apr 8 2003. (cc) Creative Commons. *** FADE BACKGROUND COLOR . drawBKG(): To draw a color shape call the drawBKG() method. The drawBKG(hex, rect) method takes two arguments : hex as color - the hexadecimal color value (ex. red is 0xFF0000, blue is 0x0000FF, and so on) rect as array - defines a rectangle whose sides are specified by left, top, right, and bottom ( [left, top, right, bottom] ) . hexToRGB(): To convert a hexadecimal color value to a RGB color value call hexToRGB() method. the hexToRGB(hex) method takes one argument and return an array : hex as color - the hexadecimal color value (ex. red is 0xFF0000, blue is 0x0000FF, and so on) . redraw(): To set this movie clip in full stage mode call the redraw() method. . fadeBKG(): To start to fade to a color call the fadeBKG() method. The fadeBKG(p, c, t, hex) method takes four arguments : p as integer - period between two colors in millisecond (the first period in a tweening mode) c as float or integer - tweening coefficient (1 for a linear fading, more than 1 for a increase tweening, les decrease tweening) t as integer - threshold of tolerance between two colors ( a number between 1 and 255 ) hex as color - the hexadecimal color value (ex. red is 0xFF0000, blue is 0x0000FF, and so on) */ var fade = false; var tRefresh, tPeriod, tCoef; var BKGcol = new Color(this); var threshold, fromRGB, toRGB; /* ---------------------------- functions ---------------------------- */ function drawBKG(hex, rect) { fromRGB = hexToRGB(hex); with (this) { beginFill(hex, 100); moveTo(rect[0], rect[1]); lineTo(rect[2], rect[1]); lineTo(rect[2], rect[3]); lineTo(rect[0], rect[3]); endFill(); } } function hexToRGB(hex) { hex = hex.toString(16); if (hex==0) { hex="000000" } r = parseInt("0x"+ hex.substring(0, 2)); g = parseInt("0x"+ hex.substring(2, 4)); b = parseInt("0x"+ hex.substring(4, 6)); return new Array(r, g, b); } function redraw() { with(this) { _x = 0; _y = 0; _width = Stage.width; _height = Stage.height; } } function fadeBKG(p, c, t, hex) { tPeriod = p; tCoef = c; tRefresh = getTimer() + tPeriod; threshold = t; toRGB = hexToRGB(hex); fade = true; } /* ------------------------------ events ------------------------------ */ this.onEnterFrame = function() { if (fade && (tRefresh <= getTimer())) { for(i=0; i<3; i++) { num = Math.round(Math.abs(toRGB[i]-fromRGB[i]) /2); prop = Math.min(threshold, num); if (fromRGB[i] > toRGB[i]) { prop = - prop; } fromRGB[i] = Math.max(Math.min(fromRGB[i] + prop, 255), 0); } rgb = ( fromRGB[0] << 16 | fromRGB[1] << 8 | fromRGB[2]); BKGcol.setRGB(rgb); if (fromRGB[0] == toRGB[0] && fromRGB[1] == toRGB[1] && fromRGB[2] == toRGB[2]) { fade = false; } else { tPeriod = Math.floor(tPeriod / tCoef); tRefresh = getTimer() + tPeriod; } } }