Colour Class

Made this the other day so I could work with colours more easily since I was constantly having to change between seperate ARGB channels and hexadecimal. And since I’m useless at almost everything to do with the two, here is the article which talks about converting between the two formats:

http://www.adobe.com/devnet/flash/articles/image_api_02.html

class Colour extends Number {
 private var __n:Number;
 public function Colour(n:Number) {
  if (n < 0 || !n) {
   this.__n = 0;
  } else if (n > 0xFFFFFFFF) {
   this.__n = 0xFFFFFFFF;
  } else {
   this.__n = n;
  }
 }
 public function get alpha():Number {
  return (this.__n >> 24) & 0xFF;
 }
 public function set alpha(n:Number):Void {
  this.__n = n << 24 | this.red << 16 | this.green << 8 | this.blue;
 }
 public function get red():Number {
  return (this.__n >> 16) & 0xFF;
 }
 public function set red(n:Number):Void {
  this.__n = this.alpha << 24 | n << 16 | this.green << 8 | this.blue;
 }
 public function get green():Number {
  return (this.__n >> 8) & 0xFF;
 }
 public function set green(n:Number):Void {
  this.__n = this.alpha << 24 | this.red << 16 | n << 8 | this.blue;
 }
 public function get blue():Number {
  return this.__n & 0xFF;
 }
 public function set blue(n:Number):Void {
  this.__n = this.alpha << 24 | this.red << 16 | this.green << 8 | n;
 }
 public function set hex(n:Number):Void {
  this.__n = n;
 }
 public function get hex():Number {
  return this.__n;
 }
 public function valueOf():Number {
  return this.__n;
 }
 public function toString(radix:Number):String {
  if (radix != 16 && radix != undefined) {
   return this.__n.toString(radix);
  }
  if (this.alpha == 0) {
   return this.__n.toString(16).toUpperCase();
  }
  var temp:String = new String();
  if (this.alpha < 0x10) {
   temp += "0" + this.alpha.toString(16);
  } else {
   temp += this.alpha.toString(16);
  }
  return new String(temp + this.red.toString(16) + this.green.toString(16) + this.blue.toString(16)).toUpperCase();
 }
 public static function fromARGB(a:Number, r:Number, g:Number, b:Number):Colour {
  var temp:Colour = new Colour();
  temp.alpha = a, temp.red = r, temp.green = g, temp.blue = b;
  return temp;
 }
}

It’s easy to use, modifying any of the colour channels will change the hexadecimal representation and vice versa.

:hoser: