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.

13 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!

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Recent Posts

    WordCamp Presentation Slides: From Photoshop PSD to WordPress Theme

    Here are my slides for my WordCamp Atlanta presentation, From PSD to WordPress Theme: Under the skin: PSD to WP on Prezi Tweet

    wordpress_wordcamp_atlanta_2012_feb_2_3

    Speaker at WordCamp Atlanta 2012

    I’m proud to announce that I’ve been asked to speak at WordCamp Atlanta this year! WordCamp will be held this weekend and hosted at SCAD Atlanta! My session is titled: From PSD to WordPress Theme: Under the skin. Obviously, I’ll be focusing on themes. We’ll look at what they are, what they can do, how [...]

    Adobe-like Arrow Headers | CSS-Tricks

    Zero images is something that always gets me excited, I really like these arrow button styles! I like the css used more and the hover/active states too, nice css3 transitions. via Adobe-like Arrow Headers | CSS-Tricks. Tweet

    snow

    Snow via Javascript & Canvas – Tis the Season

    After playing with the settings in my experiments I found a few settings I liked and wanted to develop further. The first was snow! An added bonus I was able to work on a project just for the holidays and used much of this code in it! I looked around the web and saw a [...]

  • Recent Shares

    Link: Responsive Images: How they Almost Worked and What We Need

    Mat discusses the options for getting responsive images along with responsive designs. We can use various means (server-side, client-side, mobile-first, html5 data attributes, cookies…), but none are fully satisfactory, especially with new browsers prefetching images before cookies can be set or html is even fully read and parsed. He states that bandwidth sniffing is… a [...]

    Screen shot 2012-01-26 at 2.46.59 PM

    Yiibu – About the site…

    Here’s a great article about the process of responsive design & mobile first design and how to practically use them both in a project. This site is a proof of concept for many of the ideas described in Rethinking the Mobile Web or (Mobile First Responsive Design). via Yiibu – About this site…. Tweet

    gridpak

    Link: Introducing Gridpak | Erskine Labs

    Here’s a great tool to make responsive grid layouts. Thanks to Erskine Labs! Introducing Gridpak | Erskine Labs. Tweet

    css-multiply

    Link: HTML5 multiply filter with canvas | Alberto Gasparin

    Here’s a great little script I found useful today as I was working on having dynamic effects applied to javascript via canvas. “The canvas element provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, or other visual images on the fly.” Thanks to the canvas APIs I was [...]

  • Recent Comments

    Karl

    Karl

    I have been using for some time this nice Banner, from developer FX. They have a really nice Live...
    Karl

    Karl

    Thank you for this wonderful link… recommend it! Fast, simple, easy… :-)
    Gabriel

    Gabriel

    Hi Valerie, I don’t know if you are still following this post, but I tried seeing if it is...
    avinash

    avinash

    Hi Evan, I am using the same code and trying it on chrome/firefox it is not working on neither...
    Matt

    Matt

    I needed to store url variables from advertising tracking servers – this method works like a...
    Evan Mullins

    Evan Mullins

    @Saket – you may want to look into swfaddress. I believe it will be more what...