A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: How do I change the alpha of a tint in AS3?

  1. #1
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508

    How do I change the alpha of a tint in AS3?

    I want to be able to change the alpha % of a tint color programmatically?
    for example, I can go to Properties panel and choose "Tint" under the Color option and then choose a bright red and give it a 50% alpha value...how do I do this in AS3?

    also, I would like to be able to tween from one color to another but if read the documentation correctly, it seems like I have to use the "transform" object to change a DisplayObject's color...I could tween any other property, like "alpha", "height", "width", etc., but how would I tween the color transform?

    thanks in advance!

  2. #2
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    anybody????

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You need to use a colorTransform object and compile the ARGB value yourself. Remember that the Alpha is just another channel (like Red/Green/&c.) so it's value goes from 0 - 255, or 0x00 - 0xFF.

    PHP Code:
    var clr:uint 0xFF0000;  //  rgb
    var alph:int 0xFF;  //  0 - 255

    //  convert to ARGB
    var compositeClr:uint = (alpha << 24) + clr;

    //  add to object's existing colorTransform
    var c:ColorTransform;
    myObject.transform.colorTransform;
    c.color compositeClr;
    myObject.transform.colorTransform c

  4. #4
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    man I don't understand that stuff (the channels I mean)...do you know of any good tutorials for stuff like that? I just don't understand the channels very well for some reason?

    thx for the feedback!

  5. #5
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Here's a function I wrote that changes tint and alpha of that tint. Hope it helps.

    Here's the class file version:

    private function tintColor(mc:Sprite, colorNum:Number, alphaSet:Number):void {
    var cTint:Color = new Color();
    cTint.setTint(colorNum, alphaSet);
    mc.transform.colorTransform = cTint;
    }

    And here's the version as it would be in an FLA:

    function tintColor(mc:Sprite, colorNum:Number, alphaSet:Number):void {
    var cTint:Color = new Color();
    cTint.setTint(colorNum, alphaSet);
    mc.transform.colorTransform = cTint;
    }
    It's called like so: tintColor(mc1, 0x006600, .7);
    Basically, your first argument is your sprite to be tinted, second is the tint color, and third is the alpha value of that tint.

    Let me know if you have any questions.

  6. #6
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Crashcourse in color theory:
    Your image is cut into pixels...the way that each pixel is stored is by 4 values - alpha, red, green, and blue.

    Each of those 4 values ranges from 0 - 255 (which is stored in base 16 or hexadecimal as 0x00 - 0xFF). Each pixel stacks those four values next to eachother like so:

    PHP Code:
    128 == 0x88 == 50transparency
    255 
    == 0xFF == 100red
    000 
    == 0x00 == 0green
    000 
    == 0x00 == 0blue
    --------------------------------
    0x 88 FF 00 00 == 0x88FF0000 
    When you discuss all of the alpha of the image at once (or all the redness, &c.) that's considered the "alpha channel" or "red channel". If you go into photoshop's "channels" panel and turn off all of the channels except Red, you'll see a greyscale representation of how much red is in each pixel (white being 100% red, black being no red).

    Anyhow, sorry if that was condescending, the real goofiness is when you start splitting and combining those numbers:

    So its weird that the color range is 0 - 255...basically it's stored internally as binary so each channel gets itself 8 bits: 00000000 - 11111111 (0 - 255). When all four channels are combined that stacks 4 numbers next to eachother, at 8 bits apiece, which makes a binary number 32-bits long (hence, 32-bit color).

    PHP Code:
    0x88 == 01000000 == 128 // alpha
    0xFF == 11111111 == 255 // red
    0x00 == 00000000 == 00 // green
    0x00 == 00000000 == 00 // blue
    --------------------------------
    01000000 11111111 00000000 00000000
    01000000111111110000000000000000 
    Your traditional hex color (#FF0000 as in CSS/HTML) is just 24-bit, it has no alpha built-in. So if we wan't to use that and add alpha, we need to tack it onto the left side, but you can't just add the values:

    PHP Code:
      111111110000000000000000 // RGB
    +               01000000 // alpha
    ---------------------------
    111111110000000001000000  !=  01000000111111110000000000000000 
    Enter bit-shifting. Basically using the << and >> operaters, you can move the decimal point around on your integer:

    PHP Code:
    //  move the decimal point by 24 spaces (0.0 -- .00)
    01000000 << 24
    01000000000000000000000000000000 
    // now with 24 zeros!


    +       111111110000000000000000  // RGB
    01000000000000000000000000000000  // Alpha + 24 zeros
    ----------------------------------
    01000000111111110000000000000000 
    So that gives you the number you need, albeit you never really see that crazy big string of binary. (Note: when using alpha on colors, you need to store them in a uint...no other number type can hold all those digits!)

    Anyhow, this officially concludes the most nerdy thing I've ever written. If you are clammoring for more, you can read a little more here or google around for color channels and bitshifting.

  7. #7
    Junior Member
    Join Date
    May 2004
    Posts
    1
    neznein9 thanks a bunch that was a really informative post, made my day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center