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-----
From: [email protected]
[mailto:[email protected]] On Behalf Of ali
drongo
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
string
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,
I've
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
colorThis(this.parent.parent["colorPick"].hexValue);
}
// 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
file
private function sendData(e:MouseEvent):void
{
showSending(true);
var variables:URLVariables = new URLVariables();
//stores all colours as a string
variables.colors = collectColors().toString();
var request:URLRequest = new URLRequest();
request.url = "http://www.xxxx.com/text/test.php";
request.method = URLRequestMethod.POST;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, completeHandler);
try
{
loader.load(request);
}
catch (error:Error)
{
trace("Unable to load URL");
}
}
///////// viewer class
// 5)
public function colorIn(o:Object):void
{
var colA:Array = o.Colors.split(",");
colA.splice(0,1);
for ( var i:uint=1; i<=totSprites; i++ ) {
//calls the method in the coloured button class
mainImage["s"+i].colorThis(colA[i]).toString(16);
};
}
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders