Home  |  About  | Last |  Submit  |  Contact
AllQuests.com



Previous Question:  Sippenquest: St?dtegr?ndung  Vorschl?ge und IdeenNext Question:  sliding navigation  Flash MX
Question Changing Colors Using ActionScript ( kirupaForum Flash MX )
Updated: 2008-05-19 08:34:44 (16)
Changing Colors Using ActionScript

Hi There,
Looking at the Changing Colors Using ActionScript tut. on the website, can any one tell how to tween the changing colours

Tanks,
J..

Answers: Changing Colors Using ActionScript ( kirupaForum Flash MX )
Changing Colors Using ActionScript

well, to tween you'd need to fade through each of the RGB components of the color... something like this:
ActionScript Code:
var startColor = {r:50,g:100,b:250};
var endColor = {r:250,g:0,b:120};
var t = 0;
var oColor = new Color(myMovieClip);
this.onEnterFrame = function() {
  t += 0.1; // Change this value for a faster/slower fade..
  if (t < 1) {
    fadeColor(oColor,startColor,endColor,t); // Fade to new value
  } else { // Fade is over
    fadeColor(oColor,startColor,endColor,1); // Set to end color
    delete this.onEnterFrame; // Delete frame loop
  }
}
function fadeColor(oColor,startColor,endColor,t) {
  // oColor is the color object to do the fade on
  // t is the value of the fade in the range of 0..1
  var newR = (endColor.r - startColor.r)*t + startColor.r;
  var newG = (endColor.g - startColor.g)*t + startColor.g;
  var newB = (endColor.b - startColor.b)*t + startColor.b;
  oColor.setRGB( newR << 16 | newG << 8 | newB );
}

brainy

Changing Colors Using ActionScript

ActionScript Code:
// prototype
MovieClip.prototype.fadeColor = function(cto, ease) {
    var myColor = new Color(this), cColor = myColor.getTransform();
    this.onEnterFrame = function() {
        for (var c in cColor) {
            cColor[c] += (cto[c]-cColor[c])/ease;
        }
        myColor.setTransform(cColor);
    }
}
// usage
myMovieClip.fadeColor({rb:204, gb:0, bb:0, ab:255}, 10);

parameters

cto: color transform object.
ra is the percentage for the red component (-100 to 100).
rb is the offset for the red component (-255 to 255).
ga is the percentage for the green component (-100 to 100).
gb is the offset for the green component (-255 to 255).
ba is the percentage for the blue component (-100 to 100).
bb is the offset for the blue component (-255 to 255).
aa is the percentage for alpha (-100 to 100).
ab is the offset for alpha (-255 to 255).

ease: ease (there's no need for explanation).


Make your choice.

kode

Changing Colors Using ActionScript

Thank-you both,

I ended up using brainys code which worked straight away.

wicked !!!!!

portugeezer

Changing Colors Using ActionScript

*dances around* woop, my code worked straight away, and i typed it up in the quick reply box... makes you feel like you know your flash

brainy

Changing Colors Using ActionScript

good code brainy (and kax of course )

i just dont get what this does:
Quote:
oColor.setRGB( newR << 16 | newG << 8 | newB );

mlk

Changing Colors Using ActionScript

it combines the 3 components into one 24bit color value... it shifts the red by 16 bits to the left, and or's it with the green shifted 8 bits to the left and or's it with the blue. yup yup. i actually did a pretty extensive explanation on that at the bit-101 forums so you might wanna look it up there.

brainy

Changing Colors Using ActionScript

ok i looked at brainy code and i understood thats what i want to do

but where do i put everything??

PHP Code:
wellto tween you'd need to fade through each of the RGB components of the color... something like this:

ActionScript:--------------------------------------------------------------------------------var startColor = {r:50,g:100,b:250};
var endColor = {r:250,g:0,b:120};
var t = 0;
var oColor = new Color(myMovieClip);
this.onEnterFrame = function() {
        t += 0.1; // Change this value for a faster/slower fade..
        if (t < 1) {
                fadeColor(oColor,startColor,endColor,t); // Fade to new value
        } else { // Fade is over
                fadeColor(oColor,startColor,endColor,1); // Set to end color
                delete this.onEnterFrame; // Delete frame loop
        }
}
function fadeColor(oColor,startColor,endColor,t) {
        // oColor is the color object to do the fade on
        // t is the value of the fade in the range of 0..1
        var newR = (endColor.r - startColor.r)*t + startColor.r;
        var newG = (endColor.g - startColor.g)*t + startColor.g;
        var newB = (endColor.b - startColor.b)*t + startColor.b;
        oColor.setRGB( newR << 16 | newG << 8 | newB );


pares101

Changing Colors Using ActionScript

can someone reply to the last question.. i'm also interested i don't know how to use this code
how can this code work with the tutorial from the site?

http://www.kirupa.com/developer/actionscript/color.htm

Thanks in advance

alapimba

Changing Colors Using ActionScript

so doesn any1 know where to put the code ???

komarik

Changing Colors Using ActionScript

Sure, see the example!

Sirenetta1

Changing Colors Using ActionScript

I have to step in and recommend kode's code over brainy's. Look at the following example:
[swf=http://www.kirupa.com/forum/attachment.php?attachmentid=12605&stc=1] width=400 height=170[/swf]
You'll notice that kode's code has preserved the picture and tinted it green while Brainy's can only wash the entire picture out with green. Weigh these two options and then decide.

Lindquist

Changing Colors Using ActionScript

definately go with Kodes version, just used it ~cheers Kode!

phuture

Changing Colors Using ActionScript

Quote:
Originally Posted by phuture
definately go with Kodes version, just used it ~cheers Kode!
Time warp, phuture?

Barn

Changing Colors Using ActionScript

I have a question about this... how would you acomplish a 100% white to normal (and then to 0 alpha if wanted) effect through AS? Similar to this tutorial: http://www.kirupa.com/developer/mx2004/shine.htm but without the swipe?

sAi

Changing Colors Using ActionScript

Kode's version is close to what I need, because as stated keeps the image and change mostly the tint. But I need to loop between given or random colors. So, anybody knows how to:

- Add an array of colors to cycle through
- or set the fadeColor randomly

Thanks in advance

Omaha

Changing Colors Using ActionScript

bump!

sAi

Previous Question:  Sippenquest: St?dtegr?ndung  Codemasters forum  Vorschl?ge und IdeenNext Question:  sliding navigation  kirupaForum  Flash MX

- Source: Changing Colors Using ActionScript kirupaForum Flash MX
- Previous Question: Sippenquest: St?dtegr?ndung Codemasters forum Vorschl?ge und Ideen
- Next Question: sliding navigation kirupaForum Flash MX