3 Replies Latest reply: Sep 24, 2011 7:33 AM by kglad RSS

    Need help applying the AdjustColor filter to a textfield

    reindeer4 Community Member

      In the Flash CS4 IDE, if you have Dynamic Text, you can apply a filter labeled Adjust Color which lets you change Brightness, Contrast, Saturation, and Hue.

      Can someone explain how to do that through Actionscript 3 instead of the IDE?

       

      I found a couple of old tutorials and followed them but can't get things working.

       

      Not sure if that filter is called ColorMatirx or ColorMatrixFilter or if those refer to something else entirely.

       

      Thanks for any help.

        • 1. Re: Need help applying the AdjustColor filter to a textfield
          kglad CommunityMVP

          there's no easy way to do that with actionscript.  actionscript is mostly centered around the rgb (red,green, blue) color model while that filter deals with the hsb (hue, saturation, brightness) aka hsl (hue, saturation, luminosity)color model.

           

          there are some significant benefits to be realized using hsb/hsl color model but i'm not sure that applies to anything you're doing.  unless you're particularly adept at the hsb/hsl color model, when working with flash, i would usually recommend using the rgb model.  it's easier for most people to use rgb.

           

          but, if you have a good reason, you can go back and forth between hsl and rgb using the following class.  i'm not going to give any significant support so if you don't understand it, i would recommend using rgb.

           

           

          package com.kglad {

             

              public class ColorConvert {

           

                  public function ColorConvert() {

                      // http://www.easyrgb.com/index.php?X=MATH&H=19#text19

                  }

                 

                  public function rgb_to_hsl(n:uint):Array{

                      var varR:Number = n>>16 & 0xff;

                      var varG:Number = n>>8 & 0xff;

                      var varB:Number = n>>0 & 0xff;

                     

                      varR /= 255;

                      varG /= 255;

                      varB /= 255;

                     

                      var min:Number = Math.min(varR,varG);

                      min = Math.min(min,varB);

                      var max:Number = Math.max(varR,varG);

                      max = Math.max(max,varB);

                     

                      var deltaRGB:Number = max-min;

                     

                      var L:Number = (max+min)/2;

                     

                      if(deltaRGB==0){

                          var H:Number = 0;

                          var S:Number = 0;

                      } else {

                          if(L<.5){

                              S = deltaRGB/(max+min);

                          } else {

                              S = deltaRGB/(2-max-min);

                          }

                          var deltaR:Number = ( ( ( max - varR ) / 6 ) + ( deltaRGB / 2 ) ) / deltaRGB;

                             var deltaG:Number = ( ( ( max - varG ) / 6 ) + ( deltaRGB / 2 ) ) / deltaRGB;

                             var deltaB:Number = ( ( ( max - varB ) / 6 ) + ( deltaRGB / 2 ) ) / deltaRGB;

           

                             if(varR == max ){

                              H = deltaB - deltaG;

                          } else if(varG == max ){

                              H = ( 1 / 3 ) + deltaR - deltaB;

                          } else if(varB == max ){

                              H = ( 2 / 3 ) + deltaG - deltaR;

                          }

                             if ( H < 0 ) H += 1;

                             if ( H > 1 ) H -= 1;

                      }

                      // HSL each in [0,1].  standard HSL colorSpace: H*360, S*100, L*100

                      return [H,S,L];

                  }

                  public function hsl_to_rgb(hslA:Array):uint{

                      var H:Number = hslA[0];

                      var S:Number = hslA[1];

                      var L:Number = hslA[2];

                      if ( S == 0 ){                      

                          //HSL from 0 to 1

                             var R:Number = L * 255;

                             var G:Number = L * 255;

                             var B:Number = L * 255;

                      } else {

                             if(L < 0.5 ){

                              var var_2:Number = L * ( 1 + S )

                          } else {

                              var_2 = ( L + S ) - ( S * L )

                          }

                             var var_1:Number = 2 * L - var_2

           

                            R = 255 * h_to_rgb( var_1, var_2, H + ( 1 / 3 ) )

                             G = 255 * h_to_rgb( var_1, var_2, H )

                             B = 255 * h_to_rgb( var_1, var_2, H - ( 1 / 3 ) )

                      }

                      return uint(R)<<16|uint(G)<<8|uint(B);

                  }

                  private function h_to_rgb(v1:Number,v2:Number,vH:Number):Number{

                         if(vH<0){

                          vH += 1;

                      }

                         if(vH>1){

                          vH -= 1;

                      }

                         if(6*vH<1){

                          return v1+(v2-v1)*6*vH;

                      }

                         if(2*vH<1){

                          return v2;

                      }

                         if(3*vH<2){

                          return v1+(v2-v1)*(2/3-vH)*6;

                      }

                         return v1;

                  }

                 

              }

          }

          • 2. Re: Need help applying the AdjustColor filter to a textfield
            reindeer4 Community Member

            Thanks for all the info.

             

            I'm already in up to my head in a number of programs, and I wasn't looking to dive into anything complex.

             

            I just figured it was my lack of understanding in AS3 and relative unfamiliarity with filters as opposed to some missing functionality.

             

            So just to be clear, for dynaically generated text (as opposed to text typed in via the flash cs4 ide), there's no simple, native way to assign a color and then adjust the brightness?

             

            Or would I have a simple option of strictly using HSB instead of RGB for the scripted text?

             

            Thanks for your help.

            • 3. Re: Need help applying the AdjustColor filter to a textfield
              kglad CommunityMVP

               

              So just to be clear, for dynaically generated text (as opposed to text typed in via the flash cs4 ide), there's no simple, native way to assign a color and then adjust the brightness?

              that's incorrect.


              if you can live with rgb color management, you do have a simple way to handle color using actionscript.  you can use the colorTransform property of your textfield's transform:


              var tf:TextField=new TextField();
              tf.text="whatever";
              addChild(tf);
              var ct:ColorTransform=tf.transform.colorTransform;
              ct.color = 0xrrggbb;
              tf.transform.colorTransform=ct;