ColorTransform | RGB, Hex and random colors | Actionscript Color Tutorial

    Overview

    Color can sometimes make or break your design. I’ve put together this flash to show how to set a movieclip to a certain color, I’ve had to do this at runtime and had to go by different values such as a hex number, rgb values and have even wanted to just set a random color, so this example does them all! It’s even nice for translating a Hexadecimal color into RGB color.

    Flash uses a Transform object to control certain properties of movie clips. To set color we need to use a Transform object as well as a ColorTransform object. ColorTransform objets are used to, you guessed it, tell the Transform object what color we want to set our clip to. It was a little unintuitive for me to learn, but now it makes sense, or at least enough sense to use.

    I’ve made a function that does all this for you. You just send it the movieClip reference and a color.

    setColor(myMovieClip, myColor)

    There are functions to convert rgb values to a hex value, and from a hex value to red, blue and green values as well.

    To make a random hexadecimal number Math.random() * 16777216 (the total number of hexadecimal numbers)

    Steps

    1. Imports
      import flash.geom.ColorTransform;
      import flash.geom.Transform;
    2. Make a Transform object
      var myTransform:Transform = new Transform(item);
    3. Make a ColorTransform object
      var myColorTransform:ColorTransform = new ColorTransform();
    4. Set the rgb color of the ColorTransfrorm object
      myColorTransform.rgb = myColor;
    5. Set the colorTransform property of the Transform object to your ColorTransform object
      myTransform.colorTransform = myColorTransform;

    Flash Color App

    Get Adobe Flash player

    Source Actionscript (as2)

    //method to set a specified movieClip(item:movidClip) to a specified color(col:hex value number)
    function setColor(item, col) {
      //make transform object and send the specified movieClip to it
      var myTransform:Transform = new Transform(item);
      //make colorTransform
      var myColorTransform:ColorTransform = new ColorTransform();
      //check color bounds
      if (col > 16777215) col = 16777215;
      else if (col < 0) col = 0;
      //variable to hold the color value
      var myColor:Number = col;
      //set color through color transformation
      myColorTransform.rgb = myColor;
      myTransform.colorTransform = myColorTransform;

      trace("the hex number: 0x" + addZeros(myColorTransform.rgb.toString(16)));
      var rgbObject = hex2rgb(myColor);
      trace("the hex number in rgb format: "+rgbObject.r+", "+rgbObject.g+", "+rgbObject.b);
      trace("the hex number in decimal format: " + myColorTransform.rgb);
      displayColors(myColorTransform.rgb);
    }

    //bitwise conversion of rgb color to a hex value
    function rgb2hex(r, g, b):Number {
        return(r<<16 | g<<8 | b);
    }
    //bitwise conversion of a hex color into rgb values
    function hex2rgb (hex):Object{
        var red = hex>>16;
        var greenBlue = hex-(red<<16)
        var green = greenBlue>>8;
        var blue = greenBlue - (green << 8);
      //trace("r: " + red + " g: " + green + " b: " + blue);
        return({r:red, g:green, b:blue});
    }

    //BUTTONS
    randomColor.onRelease = function() {
      //make random number (within hex number range)
      var theColor = Math.floor(Math.random() * 16777215);
      //set ball color to random color value
      setColor(colorBall.inner, theColor);
    }
    readHexColor.onRelease = function() {
      //convert 6 character input string into hex color format used by actionscript
      var theColor = "0x"+hexColorIn.text;
      //set ball color to hex color value
      setColor(colorBall.inner, theColor);
    }
    readRGBColor.onRelease = function() {
      //convert rgb values into hex value
      var theColor = rgb2hex(redColorIn.text, greenColorIn.text, blueColorIn.text);
      //set ball color to converted hex color value
      setColor(colorBall.inner, theColor);
    }
    readDecColor.onRelease = function() {
      //convert rgb values into hex value
      var theColor = decColorIn.text;
      //set ball color to converted hex color value
      setColor(colorBall.inner, theColor);
    }

    Open Source Download

    color.zip (containing color.fla and color.swf)

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

    14 Comments

    1. Posted November 10, 2008 at 7:44 pm | Permalink

      This is great – need to convert this to as3 though. I assume it will largely be the same?

    2. ainhoitxu
      Posted November 13, 2008 at 7:22 am | Permalink

      I tried this code and it completely colors my movieclip. How do you keep the borders with their original color (in your case white)? In your example only the inside of the circle changes color, not the borders.

      Thanks!

    3. Posted November 13, 2008 at 11:10 am | Permalink

      @Phil – I haven’t messed with this in AS3 as of yet, but I will soon and I’ll be sure to post about it.

      @ainhoitxu – Simple really, in my example I have a movieclip containing two more clips, the inner part of the circle and the outline. Thay are seperate clips, and in the setColor(colorBall.inner, theColor); function I am sendning as an argument the colorBall.inner movieClip. I could also send in the colorBall.outline to change the color of the stroke, or like you have done and send in the whole clip as one. Make sense?

    4. ainhoitxu
      Posted November 13, 2008 at 6:49 pm | Permalink

      It does totally make sense! I am trying to do the same as you, color the inner part of an image with borders. I suppose there is no easier way to do it than having 2 different movieclips, right?

    5. Isaac
      Posted January 18, 2009 at 4:56 am | Permalink

      Is this really the bare minimum of what’s required to program a color change in flash? Basically… I have a movieclip in my library, and I have a command that will call that out and place it on stage, but for each time it’s loaded onto the stage, I want it to randomize its color. I’ve been trying to insert code to do this in the “function onLoad () { }” segment, but I don’t seem to understand how to get the colorTransform code to work on an already existing movie clip. I keep finding examples that involve creating objects from code…

    6. Posted March 18, 2009 at 11:09 pm | Permalink

      Isaac, this is the bare minimum to transform a movieclip with an instance name of “mymc”

      import flash.geom.ColorTransform;
      import flash.geom.Transform;

      var myTransform:Transform = new Transform(mymc);
      var myColorTransform:ColorTransform = new ColorTransform();
      myColorTransform.rgb = 0xFF0000;
      myTransform.colorTransform = myColorTransform;

    7. Posted April 1, 2009 at 8:44 pm | Permalink

      To my knowledge this is the only post that actually shows how to convert hex to RGB. Thank you very much. It works well for me.

    8. Gabriel
      Posted May 13, 2009 at 9:46 am | Permalink

      This is great, but I can’t make it work. I just needed a code which could transform separated R, G, and B values into one RGB value. But when I use your rgb2hex function and I give in 0xFF, 0xFF, 0xFF, then it fills my rectangle with red color instead of white.
      Do you know why this happens? (I use _ to make the code more readable, they’re not in my AS file.)
      colorBitmapData = new BitmapData(256, 256, false);

      for (var i=0; i<256; i++) { ___for (var j=0; j<256; j++) {
      ______hex = rgb2Hex(0xFF, 0xFF, 0xFF);
      ______rect = new Rectangle(j, i, 1, 1);
      ______colorBitmapData.fillRect(rect, hex);
      ___};
      };

      private function rgb2Hex(r, g, b):Number {
      ___return (r<<16 || g<<8 || b);
      };

      I’m actually trying to create a gradient filled box which has 2 black corners, 1 white corner and 1 corner with a specified color value. But I don’t seem to manage the colors good enough even for a solid fill…

    9. Gabriel
      Posted May 13, 2009 at 11:46 am | Permalink

      Alright, I was pretty stupid with this. I forgot to add the r, g, b values to each other to get the total RGB value…

    10. Posted May 18, 2009 at 3:02 pm | Permalink

      Hi… Really thanks… Evan , i don’t know what were developers of AS3 thinking when not puting a HEX conversor in the flash libraries… but you solve it, almost i reinvent the wheel… But you solved this commnon issue… thanks..

    11. Posted October 13, 2009 at 9:23 pm | Permalink

      Thanks Evan! This helped me out a lot.

    12. Jack
      Posted October 27, 2009 at 3:18 am | Permalink

      awesome Evan, Great Job!

    13. Rose
      Posted March 10, 2010 at 10:33 pm | Permalink

      I’m using AS2, but this will help me with randomly generating hex values. Thank you so much!

    14. Posted February 23, 2012 at 3:43 am | Permalink

      nice

    Post a Comment

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

    *
    *

    You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    • Recent Posts

      WordPress updates plugin directory

      New additions to the plugin directory include: favorites, incorporating support forums into it's own tab for each plugin as well as support stats being displayed! Great! I think we also need the ability to give plugins ratings and reviews (bonus points if it can be done from within a wordpress admin dashboard when installing plugins). [...]

      Short Head

      Use zipf's short head to tune your website rather than redesign the whole thing. To make a website successful it needs to meet the needs of the users. Find out what those needs are by using the short head philosophy to equate most searched things as the biggest needs of the users. Use personas to [...]

      Img Set?

      Great article at a list apart discusing the state of the industry regarding responsive images. This picks apart the set attribute of the img element from a surprisingly objective view coming from someone so close to the picture element. Insightful discussion about the principle behind the proposals than the actual solution too. If the working [...]

      Triudo

      A mesmerizing animated triangle-ish shape form. Embedded Link triduo triduo Tweet

      Git – the paradigm shift

      A great developer story about the differences on what Git is vs other version control and what Git is not. This is how we should learn it. I heard over and over that it was distributed, but never grasped what that meant, so here are a few links and explanations that will help unlearn version [...]

      Tweening Lib comes to Javascript!

      I'm very excited to share the news that the tween library from GreenSock (hands down the best tweening library I used in flash) is not ported for use in javascript! This will be great! I missed that simple syntax from as3 when animating javascript, and now I can have my cake and eat it too. [...]

      Responsive CSS Tricks

      Here are a few useful css tricks to remember when building responsive design sites from web designer wall Embedded Link 5 Useful CSS Tricks for Responsive Design Making the design to be responsive is very easy as shown in my Responsive Design in 3 Steps tutorial, but maintaining the elements to look aesthetically balanced on [...]

      Picture element of srcset attribute?

      Bruce details the reasons and story behind the srcset attribute which is now introduced as an alternative to the picture element. Some aspects of the attribute are nice (like the fact that it's an attribute and not a new element, so it's creating up new elements with for problems. It's adapting currently used elements to [...]

      SVG Preloader with Raphael JS

      Here's a very creative use of using a newly available technology. Using svg graphics which are very lightweight, for a website preloader. I like the animation used as well. Embedded Link Make a stylish preloader with SVG | Tutorial | .net magazine Many sites neglect users with slow connections. Ian Culshaw explains how to use [...]

      CSS3 Button/Icon set

      I've been secretly hoping to see a few of these pop up once the whole icon font idea spread through the nets. I really like this idea and it's a very nice implementation too! I only see some quality issues on a couple of the icons (such as youtube), but it's awesome and I hope [...]

    • Recent Comments

      Bruce Brownlee

      Bruce Brownlee

      Ah IE6. I'd have 2 more years of sleep without IE6. Margin doubling, no properties,...
      versaena

      versaena

      how to give color at runtime…… thank you
      Mobile Websites

      Mobile Websites

      I disagree, mobile websites are the future – desktop websites and mobile websites...
      Matt Fasick

      Matt Fasick

      That's cool. I like the ripple effect as well.
      Nico

      Nico

      hi! really great job guy! very impressive.. just a question… do u have a solution to do a refresh...
      Evan Mullins

      Evan Mullins

      Agreed! I've just seen some people get pretty heated about separating all functionality...