//bill.lee created in 2011.2.10
//version 1.0
package
{
public class Color
{
// contructor, use a number
// actionscript的构造函数不能重载
public function Color(color:String)
{
this.stringValue = color;
}
//--------------set value in different format-------------
// set value by integer (real value)
public function set realValue(value:Number):void
{
this.r = value>>16;
this.g = (value>>8) & 0xFF;
this.b = value & 0xFF;
}
public function setRGB(r:Number, g:Number, b:Number):void
{
this.r = r;
this.g = g;
this.b = b;
}
// set string value like "#FF0000"
public function set stringValue(value:String):void
{
// check
var reg1:RegExp = /#[0-9a-fA-F]{6}/; // #FF00000
var reg2:RegExp = /0x[0-9a-fA-F]{6}/;
var s:String;
if(reg1.test(value))
{
s = "0x"+value.substr(1, 6);
}
else if(reg2.test(value))
{
s = value;
}
else if(value.length==0)// 如果是"",则默认为黑色
{
s = "0x000000";
}
else
{
throw new Error("Color wrong");
}
//var s:String = "0x"+value.substring(1, 7);
this.realValue = Number(s);
}
public function set R(value:Number):void
{
this.r = value;
}
public function set G(value:Number):void
{
this.g = value;
}
public function set B(value:Number):void
{
this.b = value;
}
//-------------------get value in different format-------------------------
// to string like "#FF0000"
public function get stringValue():String
{
return "#" + this.realValue.toString(16);
}
// get the real color value
public function get realValue():Number
{
var c:Number = r<<16 | g<<8 | b;// bit operation
return c;
}
public function get R():Number
{
return this.r;
}
public function get G():Number
{
return this.g;
}
public function get B():Number
{
return this.b;
}
private var r:Number;
private var g:Number;
private var b:Number;
}
}