Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Color.interpolateColor - create color transition

This is a discussion on Color.interpolateColor - create color transition within the Tutorials forums, part of the Flash English category; Hello! This Flash tutorial explains how to apply color transitions to a MovieClip using the static method interpolateColor of 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
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 29-11-07, 06:36
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Color.interpolateColor - create color transition

Hello!


This Flash tutorial explains how to apply color transitions to a MovieClip using the static method interpolateColor of the Color class in Actionscript 3.0.
As an example, we could want to change the background color based on the chosen section. Or we could want to create buttons which on chosen mouse event make a color transition.


Let us see how to do it"

I create a FLA and I save it as "main.fla".
With the following code, I draw and create a MovieClip of a square shape and on click on stage, I perform a color transition of the MovieClip itself.
Code:
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Strong;
import fl.motion.Color;
import flash.events.Event;
import flash.geom.ColorTransform;

var start_color:uint=0xCCFF00;
var final_color:uint=0x003399;
var clip_mc:MovieClip=new MovieClip();
clip_mc.graphics.beginFill(start_color,1);
clip_mc.graphics.drawRect(100,100,100,100);
clip_mc.graphics.endFill();
addChild(clip_mc);

 
var colorInfo:ColorTransform=clip_mc.transform.colorTransform;
function initTween(event:MouseEvent):void
{
	var my_tween:Tween=new Tween(clip_mc,'alpha',Strong.easeOut,0,1,1,true);
	my_tween.addEventListener(TweenEvent.MOTION_CHANGE,tweenToFinal);
}
function tweenToFinal(event:TweenEvent):void
{
	colorInfo.color=Color.interpolateColor(start_color,final_color,event.position);
	clip_mc.transform.colorTransform=colorInfo;
}
stage.addEventListener(MouseEvent.CLICK,initTween);
The result (click on stage):






Lee us analyse the code

I import the needed classes:
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Strong;
import fl.motion.Color;
import flash.events.Event;
import flash.geom.ColorTransform;

I create two uint type variables containing respectively the initial and final color
var start_color:uint=0xCCFF00;
var final_color:uint=0x003399;
I create a new MovieClip and into it, I draw a colored square based on the value of start_color
var clip_mc:MovieClip=new MovieClip();
clip_mc.graphics.beginFill(start_color,1);
clip_mc.graphics.drawRect(100,100,100,100);
clip_mc.graphics.endFill();
I add clip_mc to the stage
addChild(clip_mc);

I create a ColorTransform type variable and I assign to it the colorTransform from the transform property of clip_mc
var colorInfo:ColorTransform=clip_mc.transform.colorTr ansform;

I create a function that will be called when the mouse is clicked
function initTween(event:MouseEvent):void
{
I create a new Tween and I pass to it the parameters that I want
var my_tween:Tween=new Tween(clip_mc,'alpha',Strong.easeOut,0,1,1,true);
I add a listener to my_tween to the event MOTION_CHANGE which will call the function tweenTofinal
my_tween.addEventListener(TweenEvent.MOTION_CHANGE ,tweenToFinal);
}

function tweenToFinal(event:TweenEvent):void
{
I assign to the property color of the variable colorInfo (of ColorTransform type), the value obtained calling the static method interpolateColor of the color class, passing to it a initial color, a final color and the value at the moment of the tween (event.position) which will go progressively from 0 to 1.
colorInfo.color=Color.interpolateColor(start_color ,final_color,event.position);
I assign the color to clip_mc still using the colorTransform of its transform property
clip_mc.transform.colorTransform=colorInfo;
}
I add the listener to the stage to the event CLICK
stage.addEventListener(MouseEvent.CLICK,initTween) ;

See you soon!

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

  #2 (permalink)  
Old 13-05-08, 15:00
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
re: Color.interpolateColor - create color transition

Anyway, my suggestion is to use caurina Tweener:

Caurina Tweener by Zeh Fernando - tutorial 1
__________________

 


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 !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 27-10-08, 15:19
Junior Member
 
Join Date: Oct 2008
Posts: 1
Rep Power: 0
Ella7 is on a distinguished road
Re: Color.interpolateColor - create color transition

Hello Flep, thank you for the tutorial.

I have though one question:

I noticed that although there is an alpha tween in place that eventually trigger the color blend, the alpha doesn't change, it stays 1 all the time. Why is that?

Does the interpolateColor method set the alpha automatically to 1? I guess this must be because otherwise it makes no sense...

Thank you,
Ella
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
Color.interpolateColor - creare transizioni di colore Flep Articoli e tutorials 10 26-12-08 15:20
Actionscript 3 Tween color change for text field problem samohtwerdna advanced Actionscript 3.0 0 02-10-08 02:47
Trying to change the color of Dynamic text Dakart Flash English 0 18-07-08 20:49
Change Text color alexyz4 Flash English 4 31-10-07 08:28
change color to movieclip billyboo Flash English 7 25-10-07 09:22


All times are GMT. The time now is 12:06.

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