views:

110

answers:

1

I can't get this to work in my AS1 application. I am using the Color.setTransform method.

Am I correct in thinking the following object creation should result in transforming a colour to white?

var AColorTransform = {ra:100, rb:255, ga:100, gb:255, ba:100, bb:255, aa:100, ab:255};

And this one to black?

AColorTransform = {ra:100, rb:-255, ga:100, gb:-255, ba:100, bb:-255, aa:100, ab:-255};

I read on some websites that calling setRGB or setTransform may not result in actually changing the display colour when the object you're performing the operation on has some kind of dynamic behaviour. Does anyone know more about these situations? And how to change the colour under all circumstances?

Regards.

+1  A: 

Been a long time since I've had to do anything is AS1, but I'll do my best.

The basic code for a color.setTransform() looks like this...

var AColorTransform = {ra:100, rb:255, ga:100, gb:255, ba:100, bb:255, aa:100, ab:255};
var myColor = new Color(mc);
myColor.setTransform(AColorTransform);

...where mc is a MovieClip on the stage somewhere.

Remember that you're asking about transform, which by its nature is intended to transform colors from what they are to something else. If you want to reliably paint in a specific color (such as black or white), you're usually far better off using setRGB, which would look like this:

var myColor = new Color(mc);
//set to black
myColor.setRGB(0x000000);
//or set to white
myColor.setRGB(0xFFFFFF);

These work reliably, though there can be some gotchas. Generally, just remember that the color is attached to the specific MovieClip...so if that MovieClip falls out of scope (ie, it disappears from the timeline) your color will be deleted with it.


Read further only if you want to understand color transform better:

Let's look at the components of that color transform.

    a (multiplier 0 > 100%)   b(offset -255 > 255)
r       ra                       rb

g       ga                       gb

b       ba                       bb

a       aa                       bb

There are four channels (r, g, b, and a). The first three are for red, green and blue, and the last one for alpha (transparency). Each channel has an 'a' component and a 'b' component, thus ra, rb, ga, gb, etc. The 'a' component is a percentage multiplier. That is, it will multiply any existing channel by the percent in that value. The 'b' component is an offset. So 'ra' multiplies the existing red channel. 'rb' offsets it. If your red channel starts as 'FF' (full on red), setting ra:100 will have no effect, since multiplying FF by 100% results in no change. Similarly, if red starts at '00' (no red at all), no value of 'ra' will have any effect, since (if you recall your Shakespeare) twice nothing is still nothing. Things in-between will multiply as you'd expect.

Offsets are added after multiplication. So you can multiply by some value, then offset it:

r (result red color) = (RR * ra%) + rb
g (result green color) = (GG * ga%) + gb
b (result blue color) = (BB * ba%) + bb
a (result alpha) = (AA * aa%) + ab

example: RR = 128 (hex 0x80), ra = 50 (50% or .5), rb = -20

resulting red channel: (128 * .5) + (-20) = 44 (hex 0x2C)

Frankly, this all gets so confusing that I tend to prefer the simple sanity of avoiding transforms altogether and go with the much simpler setRGB().

Wikiup
'These work reliably, though there can be some gotchas.'I hope you see the inconsistency in that sentence :-)Great answer altogether though! I tried using setRGB, but that didn't work so I switched to setTransform which I havent gotten working yet either.I don't think it's in the command but probably in the reference to the movieclip. I think the way I'm trying to do it would work in any other language, but it's not working in AS1...Thanks!
JurgenW