Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

flash.geom.ColorTransform

This is a discussion on flash.geom.ColorTransform within the Tutorials forums, part of the Flash English category; Since the ColorTransform class is very enjoyable (and useful)"why don"t we play with it a little. Browsing the ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 12-10-07, 17:10
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
flash.geom.ColorTransform

Since the ColorTransform class is very enjoyable (and useful)"why don"t we play with it a little.
Browsing the web and in particular Flash sites, you"ll have come across some applications using a kind of fade-in/fade-out of the background from one colour to another.
I find it very intelligent as it allows to distinguish the sections of the site by their background colour.
I"ve written a class that shows how to execute this fading, here it is:
Code:
package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.display.Stage;
		
	public class CambiaSfondo extends Sprite 
	{
		private var _sprite:Sprite;
		
		private var rosso1:Number=1;
		private var verde1:Number=0;
		private var blu1:Number=0;
		
		private var rosso2:Number=0;
		private var verde2:Number=.5;
		private var blu2:Number=1;
		
		private var speed:Number=0.1;
		
		private var colors_array:Array;
		
		public function CambiaSfondo() 
		{
			initArray();
			drawSprite();
			addListeners();
		}
		
		public function initArray():void
		{
			colors_array=new Array('light-blue','purple','red','green',
								   'brown','blue','orange','yellow');
		}
		
		public function drawSprite():void
		{
			_sprite=new Sprite();
			_sprite.graphics.beginFill(0xffffff,100);
			_sprite.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			_sprite.graphics.endFill();
			_sprite.buttonMode=true;
			_sprite.useHandCursor=true;
			addChild(_sprite);
		}
		
		public function addListeners():void
		{
			_sprite.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			_sprite.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		public function reCall():void
		{
			_sprite.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public function onEnterFrame(event:Event):void
		{
			var arrR:Number=rosso2;
			var dr:Number=rosso2-rosso1;
			var ar:Number=dr*speed;
			rosso1+=ar;
			verde1+=(verde2-verde1)*speed;
			blu1+=(blu2-blu1)*speed;
			_sprite.transform.colorTransform=
			new ColorTransform(rosso1,verde1,blu1);
			if(Math.abs(dr)<=.01)
			{
				_sprite.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
		}
		
		public function onClick(event:MouseEvent):void 
		{
			var n:Number=Math.floor(Math.random()*colors_array.length);
			setColor(colors_array[n]);
		}
		
		public function setColor(s:String):void
		{
			switch(s)
			{
				case 'randomize':
				rosso2=Math.random();
				verde2=Math.random();
				blu2=Math.random();
				break;
				
				case 'light-blue':
				rosso2=0;
				verde2=.5;
				blu2=1;
				break;
				
				case 'purple':
				rosso2=.8;
				verde2=.3;
				blu2=.85;
				break;
				
				case 'red':
				rosso2=.88;
				verde2=.19;
				blu2=.31;
				break;
				
				case 'green':
				rosso2=.18;
				verde2=.47;
				blu2=.26;
				break;
				
				case 'brown':
				rosso2=.54;
				verde2=.38;
				blu2=.25;
				break;
				
				case 'blue':
				rosso2=.14;
				verde2=.10;
				blu2=.34;
				break;
				
				case 'orange':
				rosso2=.98;
				verde2=.53;
				blu2=.20;
				break;
				
				case 'yellow':
				rosso2=.92;
				verde2=.94;
				blu2=.24;
				break;
			}
			reCall();
		}
	}
}
I"ve also included a case for "randomize", so that we can use random colours.

Stay tuned!
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:52..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
flash.geom.ColorTransform Flep Articoli e tutorials 1 02-11-08 15:05
Actionscript 3 Help with ColorTransform flicker on large text blocks samohtwerdna Actionscript 3.0 newbies 0 21-10-08 16:40
ColorTransform wonīt work with 2 masks madmad Flash English 0 28-05-08 09:00
Oggetto Graphics e ColorTransform superkent Actionscript 3.0 base 3 31-12-07 13:33


All times are GMT. The time now is 17:32.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap