Basic Tint Function

Actionscript:
  1. // display object, red (0-255), green, blue, amount (0-1)
  2. function tint(dsp:DisplayObject, r:Number, g:Number, b:Number, amount:Number=1):void {
  3.     if (amount != 1) {
  4.         r *= amount;
  5.         g *= amount;
  6.         b *= amount;
  7.     }
  8.     amount = 1-amount;
  9.     var ct:ColorTransform = transform.colorTransform;
  10.     ct.redOffset = r;
  11.     ct.redMultiplier = amount;
  12.     ct.greenOffset = g;
  13.     ct.greenMultiplier = amount;
  14.     ct.blueOffset = b;
  15.     ct.blueMultiplier = amount;
  16.     dsp.transform.colorTransform = ct;
  17. }
  18.  
  19. // test out the tint function on a circle with a stroke:
  20. var circle:Shape = Shape(addChild(new Shape()));
  21. with (circle.graphics) {
  22.     lineStyle(5, 0x339999);
  23.     beginFill(0x003333);
  24.     drawCircle(0,0,50);
  25.     x = 100;
  26.     y = 100;
  27. }
  28.  
  29. // tint the circle 50% green
  30. tint(circle, 0, 255, 0, .5);

The ColorTransform object is a little confusing with its "redMultiplier, redOffset, greenMultiplier etc..." properties. Once you understand them it's not a big deal, but I still find them a bit cumbersome. So when I just want to tint a clip similar to the way you might tint a clip in the IDE... I use this tint function. Rather than taking a hexidecimal number it takes values for red, green and blue (0-255) - and an amount argument (0-1).

This entry was posted in color and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted August 23, 2011 at 2:37 am | Permalink

    Id say overall that Ive found ColorTransform to be rather useful.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*