PDA

View Full Version : Colortransform.color



malidia
February 18th, 2009, 11:25 AM
OK, so i think i am going crazy again and have spent way too much time on something simple, so please help my sanity.

below is some code that changes the tint of a movie clip on the stage with no problem, but my problem is i want to change a colors tint and store that color for later use to apply to dynamically created sprites, for some reason the code changes my place graphic as expect but when i draw a square using the Colortransform.color property the color always in gray scale???? i am so confused.



package{

import flash.display.*;
import flash.geom.*;

public class ColorTinter extends MovieClip{
public function ColorTinter():void{
var tint = 127.5;

var cT:ColorTransform = new ColorTransform();
cT.color = 0xFF0000;


cT.redOffset += tint;
cT.greenOffset += tint;
cT.blueOffset += tint;

square_mc.transform.colorTransform = cT;

this.graphics.beginFill(cT.color, 1);
this.graphics.drawRect(0, 0, 100, 100);
this.graphics.endFill();

}
}
}
and here is source --> http://troyblank.com/temp/ColorTinter.zip
and here is swf -->http://troyblank.com/temp/ColorTinter.swf (one on right is place square)

senocular
February 18th, 2009, 11:49 AM
The problem is that you're offsetting the already maxed red FF value with another 127.5 putting it beyond its acceptable range. What you end up getting is that value essentially wrapping around and starting from 0 again (and actually, given the way hex color works, you're adding to the alpha). So what you end up with is red going back to 0 then having added the remaining 126.5 of that offset with the other colors going from 0 to 127.5. So really, you're not at FULL gray ;)

This is different from square_mc because you're not setting its color, you're setting its colorTransform. ColorTransform is a complicated structure that goes beyond simple RGB values (as defined in hex) and provides offsets and multipliers. Its these values, not the color hex, that the movie clip uses to define its color when setting the colorTransform.

You could just as well make it gray too if you created another ColorTransform and assigned its color to the cT.color property after the offsets then used the new ColorTransform for the square_mc's. This would only set the base color and not include the offset information meaning the square's color would be based solely on the hex information which is gray.

malidia
February 18th, 2009, 12:02 PM
ahhhh, that totally makes sense, i guess i misunderstood the ColorTransform class, so below is updated code that works using bitwise operators to apply the tint instead, and i get the same results as i would using colotransform, thanks senocular for being so awesome.


package{

import flash.display.*;
import flash.geom.*;

public class ColorTinter extends MovieClip{
public function ColorTinter():void{
var tint = 127.5;

var cT:ColorTransform = new ColorTransform();
cT.color = 0xFF0000;


cT.redOffset += tint;
cT.greenOffset += tint;
cT.blueOffset += tint;

square_mc.transform.colorTransform = cT;

var tintColor:uint = tintRGB(255+tint, 0+tint, 0+tint);


this.graphics.beginFill(tintColor, 1);
this.graphics.drawRect(0, 0, 100, 100);
this.graphics.endFill();

}

function tintRGB(red:Number,green:Number,blue:Number):uint {

var RGB:Number;
if(red>255){red=255;}
if(green>255){green=255;}
if(blue>255){blue=255;}

if(red<0){red=0;}
if(green<0){green=0;}
if(blue<0){blue=0;}

RGB=(red<<16) | (green<<8) | blue;

return RGB;

}
}
}