To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-07-2004, 12:23 PM   #1
flashdiscovery
Member
 
Join Date: Jan 2001
Posts: 83
color blending prototype

I have two color objects that I stored using getTransform().

I set the color of the movieclip to the first color Object. When I click on the movieclip, I want it to slowly blend to the color of the second color object. How do I do that?
flashdiscovery is offline   Reply With Quote
Old 10-07-2004, 03:40 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
I would do this using a single color object, which you modify dynamically.

Let's say you have two r,g,b values:

r1,g1,b1 // original color

r2,g2,b2 // destination color

You would start by setting the color object to r1,g1,b1. Then over time, you would tween it to r2,g2,b2.

During the course of the animation, the value of t (for time) is set to go from 0 to 1.

Then:

r - t*r2 + (1-t)*r1;
g - t*g2 + (1-t)*g1;
b - t*b2 + (1-t)*b1;

This is a basic linear-interpolation forumula. Notice that when t is near zero, the color is very close to r1,g1,b1 but when t is near one, the color is very close to r2,g2,b2.

Here's a script which uses this technique to transform a movieclip from red to green over 4 seconds.

code:


doTween = function()
{
// t will go from 0 to 1 over the time specified by this.dur (duration)
var t = (getTimer() - this.startTime)/this.dur;
if (t > 1) {
t = 1;
delete this.onEnterFrame;
}
var r = t*this.r2 + (1-t)*this.r1;
var g = t*this.g2 + (1-t)*this.g1;
var b = t*this.b2 + (1-t)*this.b1;
// trace(r + ", "+ g + ", " + b);
this.clr.setRGB((r << 16) + (g << 8) + b);
}

MovieClip.prototype.beginColorTween = function(r1,g1,b1,r2,g2,b2,duration)
{
this.r1 = r1;
this.g1 = g1;
this.b1 = b1;
this.r2 = r2;
this.g2 = g2;
this.b2 = b2;
this.dur = duration;
this.startTime = getTimer();
this.clr = new Color(this);
this.onEnterFrame = doTween;
}

// transform movieclip 'mc' from red to green over 4 seconds
mc.beginColorTween (255,0,0, 0,255,0, 4*1000);




For more subtle tints, you can use setTransform instead of setRGB, but the method of interpolation is the same - for each element in the transform array whcih changes, interpolate it using

x = t*x2 + (1-t)*x1;
__________________

Last edited by jbum; 10-07-2004 at 03:43 PM.
jbum is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off

Forum Jump


All times are GMT -4. The time now is 04:10 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.