PDA

View Full Version : Colour Class



TheCanadian
June 16th, 2006, 12:39 AM
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:

bootiteq
June 16th, 2006, 09:04 AM
My brain capacity is probably one tenth of yours TC. How would I use this if I wanted to?
Can you name an example...

gvozden
June 16th, 2006, 09:51 AM
nice class
eliminates using RGB converting functions ;)

TheCanadian
June 16th, 2006, 02:13 PM
Thanks :hoser:

Here's a little example, hope it helps:

this.colour = new Colour(0xFF00FF); //a nice magenta in format 0xRRGGBB

//we can get the values of the individual colour channels
trace(this.colour.red); //255 or 0xFF
trace(this.colour.green); //0
trace(this.colour.blue); //255 or 0xFF

//we can also set these channels
this.colour.green = 255;
this.colour.blue = 0;
trace(this.colour); //now we have yellow: 0xFFFF00

//we can use this colour anyway we want now, for instance setting the colour of a movie clip named myMovieClip
//dont confuse Color with Colour
this.myMovieClip.color = new Color(this.myMovieClip);
this.myMovieClip.color.setRGB(this.colour);

devonair
June 17th, 2006, 06:06 AM
extremely useful.. thank you, TheCanadian

gvozden
June 17th, 2006, 06:51 AM
so basiclly these color properties can be tweened, right?

TheCanadian
June 17th, 2006, 09:51 PM
so basiclly these color properties can be tweened, right?Yeah that can be done:

import flash.geom.ColorTransform;
import mx.transitions.Tween;
import mx.transitions.easing.Strong;
import mx.utils.Delegate;
Stage.scaleMode = "noScale";
this.colour = new Colour();
this.redChannelTween = new Tween(this.colour, "red", Strong.easeOut, 0, 0, 1, true);
this.greenChannelTween = new Tween(this.colour, "green", Strong.easeOut, 0, 0, 1, true);
this.blueChannelTween = new Tween(this.colour, "blue", Strong.easeOut, 0, 0, 1, true);
this.redChannelTween.onMotionChanged = Delegate.create(this, function ():Void {
this.transform.colorTransform = new ColorTransform(1, 1, 1, 1, this.colour.red, this.colour.green, this.colour.blue, this.colour.alpha);
});
this.onMouseDown = function():Void {
var tempColour:Colour = new Colour(0xFFFFFF * Math.random());
this.redChannelTween.continueTo(tempColour.red, 2);
this.greenChannelTween.continueTo(tempColour.green , 2);
this.blueChannelTween.continueTo(tempColour.blue, 2);
};
this.onMouseDown();
:)

bootiteq
June 18th, 2006, 06:13 AM
that is truely cool!

gvozden
June 18th, 2006, 06:28 AM
great job Canadian ;)

unclesond
June 18th, 2006, 02:00 PM
pimping :kommie:

Pattt
June 18th, 2006, 02:20 PM
Very cool class! :)

phorte
June 29th, 2006, 06:56 AM
Now thats some very cool and very usefull stuff!

TheCanadian
January 1st, 2007, 07:58 PM
Thanks a bundle :kommie:

Icy Penguin
January 2nd, 2007, 12:27 AM
Awesome!

creatify
January 3rd, 2007, 01:05 AM
This is very sweet - it really does solve a lot of the mystery behind rgb/hex conversion and usage, thanks TheCandian.

TheCanadian
January 3rd, 2007, 01:08 AM
Thanks and your welcome :P

TheCanadian
June 26th, 2007, 12:10 AM
I converted this to AS3 and added an offset method for someone on the forums. So, if anyone's interested, here it is:

package {
public class Colour extends Object {
private var n:uint;
public function Colour(n:uint = 0) {
if(n > 0xFFFFFFFF) throw new RangeError("Colour cannot be represented by a value greater than 0xFFFFFFFF");
this.n = n;
}
public function get alpha():uint {
return (this.n >> 24) & 0xFF;
}
public function set alpha(n:uint):void {
if(n > 255) throw new RangeError("Colour channel cannot exceed 255");
this.n = n << 24 | this.red << 16 | this.green << 8 | this.blue;
}
public function get red():uint {
return (this.n >> 16) & 0xFF;
}
public function set red(n:uint):void {
if(n > 255) throw new RangeError("Colour channel cannot exceed 255");
this.n = this.alpha << 24 | n << 16 | this.green << 8 | this.blue;
}
public function get green():uint {
return (this.n >> 8) & 0xFF;
}
public function set green(n:uint):void {
if(n > 255) throw new RangeError("Colour channel cannot exceed 255");
this.n = this.alpha << 24 | this.red << 16 | n << 8 | this.blue;
}
public function get blue():uint {
return this.n & 0xFF;
}
public function set blue(n:uint):void {
if(n > 255) throw new RangeError("Colour channel cannot exceed 255");
this.n = this.alpha << 24 | this.red << 16 | this.green << 8 | n;
}
public function get hex():uint {
return this.n;
}
public function set hex(n:uint):void {
if(n > 0xFFFFFFFF) throw new RangeError("Colour cannot be represented by a value greater than 0xFFFFFFFF");
this.n = n;
}

public function valueOf():uint {
return this.n;
}
public function toString(radix:uint = 16):String {
return this.n.toString(radix).toUpperCase();
}

public function offset(n:Number, a:Boolean):void {
if(a) {
var da:int = this.alpha + n;
if(da > 255) this.alpha = 255;
else if(da < 0) this.alpha = 0;
else this.alpha = da;
}
var dr:int = this.red + n;
if(dr > 255) this.red = 255;
else if(dr < 0) this.red = 0;
else this.red = dr;

var dg:int = this.green + n;
if(dg > 255) this.green = 255;
else if(dg < 0) this.green = 0;
else this.green = dg;

var db:int = this.blue + n;
if(db > 255) this.blue = 255;
else if(db < 0) this.blue = 0;
else this.blue = db;
}

public static function fromARGB(a:Number, r:Number, g:Number, b:Number):Colour {
if(a > 255 || r > 255 || g > 255 || b > 255) throw new RangeError();
var temp:Colour = new Colour();
temp.alpha = a, temp.red = r, temp.green = g, temp.blue = b;
return temp;
}
}
}

prg9
June 26th, 2007, 07:02 AM
Whoa sweet, I am glad this got bumped becuase I never saw this one, nice job Canadian! :mountie:

TheCanadian
June 26th, 2007, 08:19 PM
Thanks, glad you like it :hoser: