PDA

View Full Version : Color Morph


Voetsjoeba
12-30-2004, 07:40 AM
A very simple prototype to morh a Color object's color into a provided RGB value, again using my own easing formula. This once again proves that it can be used to ease almost anything.


Color.prototype.morphTo = function(R, G, B, A){
aux = _root.createEmptyMovieClip("__colormorph",-999);
aux.ref = this;
aux.onEnterFrame = function(){
curColor = this.ref.getTransform();
curra = curColor.ra;
curga = curColor.ga;
curba = curColor.ba;
curaa = curColor.aa;
this.ref.setTransform({ra:R-(R-curra)/1.1, ga:G-(G-curga)/1.1, ba:B-(B-curba)/1.1, aa:A-(A-curaa)/1.1});
}
}


Example demonstrating this prototype:

width=760 height=420

Source: http://www.voetsjoeba.com/lab/source/colorfade.fla

pom
12-30-2004, 10:18 AM
That's handy, Voets, but it looks to me that:
- having a reference to the _root in a prototype is not really a good idea
- using the same name for your controller clip all the time prevents you from color-morphing 2 clips at the same time
- you never stop the onEnterFrame
Or am I missing something? :)

Voetsjoeba
12-30-2004, 10:36 AM
Well I need to create the movieclips somewhere, and _root is the only movieclip of which I'm sure to exist. And I meant to delete that onEnterFrame, but I just forgot to implement it. You're right though, lemme edit it.

Voetsjoeba
12-30-2004, 11:17 AM
Color.cmid = 0;
Color.prototype.morphTo = function(R, G, B, A) {
if(!_root.__cmc) _root.createEmptyMovieClip("__cmc",-999);
if(this.cmid == undefined){
this.cmid = Color.cmid++;
this.cmaux = _root.__cmc.createEmptyMovieClip("__colormorph"+this.cmid, this.cmid);
}
this.cmaux.ref = this;
this.cmaux.onEnterFrame = function() {
curColor = this.ref.getTransform();
tarR = R-(R-curColor.ra)/1.3;
tarG = G-(G-curColor.ga)/1.3;
tarB = B-(B-curColor.ba)/1.3;
tarA = A-(A-curColor.aa)/1.3;
this.ref.setTransform({ra:tarR, ga:tarG, ba:tarB, aa:tarA});
if(Math.abs(tarR-R) <= 2 && Math.abs(tarG-G) <= 2 && Math.abs(tarB-B) <= 2 && Math.abs(tarA-A) <= 2){
delete this.onEnterFrame;
}
};
};

Better pom ? :)

pom
12-30-2004, 11:23 AM
:thumb:

[Edit] I'm wondering though: your tarA, tarG... variables are not local to the function, so if you run that script on 2 clips, isn't it going to be a problem?Color.cmid = 0;
Color.prototype.morphTo = function(R, G, B, A) {
if(!_root.__cmc) _root.createEmptyMovieClip("__cmc",-999);
if(this.cmid == undefined){
this.cmid = Color.cmid++;
this.cmaux = _root.__cmc.createEmptyMovieClip("__colormorph"+this.cmid, this.cmid);
}
this.cmaux.ref = this;
this.cmaux.onEnterFrame = function() {
curColor = this.ref.getTransform();
var tarR = R-(R-curColor.ra)/1.3;
var tarG = G-(G-curColor.ga)/1.3;
var tarB = B-(B-curColor.ba)/1.3;
var tarA = A-(A-curColor.aa)/1.3;
this.ref.setTransform({ra:tarR, ga:tarG, ba:tarB, aa:tarA});
if(Math.abs(tarR-R) <= 2 && Math.abs(tarG-G) <= 2 && Math.abs(tarB-B) <= 2 && Math.abs(tarA-A) <= 2){
delete this.onEnterFrame;
this.removeMovieClip () ;
}
};
};Can't test right now, so this may be totally unuseful :P

Voetsjoeba
12-30-2004, 02:49 PM
Nothing gets past you unnoticed, does it :P

GW02
12-30-2004, 10:54 PM
Wow, that's nice. May come in handy.