Courtesy Evan Mullins of Circlecube Studio:

//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});

Jason Merrill

 Bank of  America   
Learning Performance Solutions Instructional Technology & Media   

Learn about the Adobe Flash platform for rich media experiences - join
the Bank of America Flash Platform Community 

-----Original Message-----
[] On Behalf Of ali
Sent: Monday, February 09, 2009 12:26 PM
To: Flash Coders List
Subject: [Flashcoders] Converting hex colour numbers back and forth

Hi there, I'm saving a bunch of colours as a string then using this
later to colour objects in the same way.  Something is going wrong
though as
the colours I am getting back are not the same as the ones I put in.
If anyone could point out where I'm going wrong I'd be really grateful,
googled this and and have checked the help files and as far as I can see
it's correct (though obviously it isn't!).

Here's my code, I've written comments next to the relevant lines:

/// in my coloured button class

//  1) colour is set on mouse click
private function clickHandler(e:MouseEvent):void
// passes hexValue from ColorPicker
//  2) color is stored in var
public function colorThis(c:String):void
//stores colour
currColor = Number("0x"+c);
var newColorTransform:ColorTransform = this.transform.colorTransform;
newColorTransform.color = currColor;
this.transform.colorTransform = newColorTransform;

// application class
 //  3) stores colors in an array
private function collectColors():Array
colors_ar = new Array();
for ( var i:uint=1; i<=totSprites; i++ ) {
//stores colour in array
colors_ar[i] = mainImage["s"+i].currColor;
return colors_ar;

//  4) array is written to string and sent to php to be written to txt

private function sendData(e:MouseEvent):void
var variables:URLVariables = new URLVariables();
//stores all colours as a string
variables.colors = collectColors().toString();
var request:URLRequest = new URLRequest();
request.url = "";;
request.method = URLRequestMethod.POST; = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, completeHandler);
catch (error:Error)
    trace("Unable to load URL");

/////////   viewer class
//  5)

public function colorIn(o:Object):void
var colA:Array = o.Colors.split(",");
for ( var i:uint=1; i<=totSprites; i++ ) {
//calls the method in the coloured button class
Flashcoders mailing list
Flashcoders mailing list

Reply via email to