Posts Tagged ‘ util ’

Util for Picking Random XML Entry

Code for picking up random XML entries from XMLList : code

Random Password Generation

A Small Util Function to create random Password Generation https://gist.github.com/988479

Convert int to String

The AS3 Util Function is posted on the below link
Utility to convert number or integer to String. The util function is useful in many applications.
https://gist.github.com/703108

AS3 Color Utility Functions

Utility for Color functions, like RGB to CMYK, RGB to HSV, HSV to RGB, ColorTransformation, int color value to RGB and Color Calculations. Gist Link

package
{
import flash.geom.ColorTransform;
public class ColorUtil
{
/**
* RGBColorTransform Create an instance of the information.
* @ Param rgb RGB integer value that indicates (0x000000 – 0xFFFFFF)
* @ Param amount of fill adaptive value (0.0 – 1.0)
* @ Param alpha transparency (0.0 – 1.0)
* @ Return a new instance ColorTransform
* */
public static function colorTransform (rgb: uint = 0, amount: Number = 1.0, alpha: Number = 1.0): ColorTransform
{
amount = (amount> 1)? 1: (amount 1)? 1: (alpha > 16) & 0xff) * amount;
var g: Number = ((rgb>> 8 ) & 0xff) * amount;
var b: Number = (rgb & 0xff) * amount;
var a: Number = 1-amount;
return new ColorTransform (a, a, a, alpha, r, g, b, 0);
}

/**
* Subtraction.
* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) is subtracted from the return numbers.
* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Return value subtracted Blend
**/
public static function subtract (col1: uint, col2: uint): uint
{
var colA: Array = toRGB (col1);
var colB: Array = toRGB (col2);
var r: uint = Math.max (Math.max (colB [0] – (256-colA [0]), colA [0] – (256-colB [0])), 0);
var g: uint = Math.max (Math.max (colB [1] – (256-colA [1]), colA [1] – (256-colB [1])), 0);
var b: uint = Math.max (Math.max (colB [2] – (256-colA [2]), colA [2] – (256-colB [2])), 0);
return r <<16 | g <<8 | b;
}

/**
* Additive color.
* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) Returns the value of the additive mixture.
* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Return the additive color
**/
public static function sum (col1: uint, col2: uint): uint
{
var c1: Array = toRGB (col1);
var c2: Array = toRGB (col2);
var r: uint = Math.min (c1 [0] + c2 [0], 255);
var g: uint = Math.min (c1 [1] + c2 [1], 255);
var b: uint = Math.min (c1 [2] + c2 [2], 255);
return r <<16 | g <<8 | b;
}

/**
* Subtractive.
* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) Returns the value of the subtractive color.
* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Return the subtractive
**/
public static function sub (col1: uint, col2: uint): uint
{
var c1: Array = toRGB (col1);
var c2: Array = toRGB (col2);
var r: uint = Math.max (c1 [0]-c2 [0], 0);
var g: uint = Math.max (c1 [1]-c2 [1], 0);
var b: uint = Math.max (c1 [2]-c2 [2], 0);
return r <<16 | g <<8 | b;
}

/**
* Comparison (dark).
* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) to compare, RGB lower combined returns a numeric value for each number.
* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Return comparison (dark) values
**/
public static function min (col1: uint, col2: uint): uint
{
var c1: Array = toRGB (col1);
var c2: Array = toRGB (col2);
var r: uint = Math.min (c1 [0], c2 [0]);
var g: uint = Math.min (c1 [1], c2 [1]);
var b: uint = Math.min (c1 [2], c2 [2]);
return r <<16 | g <<8 | b;
}

/**
* Comparison (light).
* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) to compare, RGB values combined with higher returns to their numbers.
* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
* @ Return comparison (light) value
**/
public static function max (col1: uint, col2: uint): uint
{
var c1: Array = toRGB (col1);
var c2: Array = toRGB (col2);
var r: uint = Math.max (c1 [0], c2 [0]);
var g: uint = Math.max (c1 [1], c2 [1]);
var b: uint = Math.max (c1 [2], c2 [2]);
return r <<16 | g <<8 | b;
}

/**
* Values calculated from each RGB * RGB color value.
* @ Param r the red (R) indicating the number (0-255)
* @ Param g green (G) indicates the number (0-255)
* @ Param b blue (B) shows the number (0-255)
* @ Return obtained from the RGB color value for each indicating the number
**/
public static function rgb (r: uint, g: uint, b: uint): uint
{
return r <<16 | g <> 16 & 0xFF;
var g: uint = rgb>> 8 & 0xFF;
var b: uint = rgb & 0xFF;
return [r, g, b];
}

/**
* RGB from the respective figures, HSV sequences in terms of returns.
* RGB values are as follows.
* R – a number from 0 to 255
* G – a number from 0 to 255
* B – a number from 0 to 255
*
* HSV values are as follows.
* H – a number between 360-0
* S – number between 0 and 1.0
* V – number between 0 and 1.0
*
* Can not compute, including alpha.
* @ Param r the red (R) indicating the number (0x00 to 0xFF to)
* @ Param g green (G) indicates the number (0x00 to 0xFF to)
* @ Param b blue (B) shows the number (0x00 to 0xFF to)
* @ Return HSV values into an array of [H, S, V]
**/
public static function RGBtoHSV (r: Number, g: Number, b: Number): Array
{
r / = 255; g / = 255; b / = 255;
var h: Number = 0, s: Number = 0, v: Number = 0;
var x: Number, y: Number;
if (r> = g) x = r; else x = g; if (b> x) x = b;
if (r <= g) y = r; else y = g; if (b <y) y = b;
v = x;
var c: Number = xy;
if (x == 0) s = 0; else s = c / x;
if (s! = 0) {
if (r == x) {
h = (gb) / c;
} else {
if (g == x) {
h = 2 + (br) / c;
} Else {
if (b == x) {
h = 4 + (rg) / c;
}
}
}
h = h * 60;
if (h <0) h = h +360;
}
return [h, s, v];
}
/**
* RGB from the respective figures, HSV sequences in terms of returns.
* RGB values are as follows.
* R – a number from 0 to 255
* G – a number from 0 to 255
* B – a number from 0 to 255
*
* CMYK values are as follows.
* C – a number between 0 to 255 representing cyan
* M – number between 0 to 255 representing magenta
* Y – number between 0 to 255 representing yellow
* K – number between 0 to 255 representing black
*
* Can not compute, including alpha.
* @ Param r the red (R) indicating the number (0x00 to 0xFF to)
* @ Param g green (G) indicates the number (0x00 to 0xFF to)
* @ Param b blue (B) shows the number (0x00 to 0xFF to)
* @ Return CMYK values into an array of [H, S, V]
**/
public static function RGBtoCMYK( r:Number, g:Number, b:Number ):Array
{
var c:Number=0, m:Number=0, y:Number=0, k:Number=0, z:Number=0;
c = 255 – r;
m = 255 – g;
y = 255 – b;
k = 255;

if (c < k)
k=c;
if (m < k)
k=m;
if (y < k)
k=y;
if (k == 255)
{
c=0;
m=0;
y=0;
}else
{
c=Math.round(255*(c-k)/(255-k));
m=Math.round (255*(m-k)/(255-k));
y=Math.round (255*(y-k)/(255-k));
}
return [ c, m, y, k ];
}

/**
* HSV from each of the RGB values to determine a return as an array.
* RGB values are as follows.
* R – a number from 0 to 255
* G – a number from 0 to 255
* B – a number from 0 to 255
*
* HSV values are as follows.
* H – a number between 360-0
* S – number between 0 and 1.0
* V – number between 0 and 1.0
*
* H is replaced with equivalent numbers in the range of the 360-0 that is out of range.
* Can not compute, including alpha.
*
* @ Param h hue (Hue) number that indicates (to 360-0)
* @ Param s the saturation (Saturation) shows the number (0.0 to 1.0)
* @ Param v lightness (Value) indicates the number (0.0 to 1.0)
* @ Return RGB values into an array of [R, G, B]
**/
public static function HSVtoRGB (h: Number, s: Number, v: Number): Array
{
var r: Number = 0, g: Number = 0, b: Number = 0;
var i: Number, x: Number, y: Number, z: Number;
if (s 1) s = 1; if (v 1) v = 1;
h = h% 360; if (h > 0;
x = v * (1 – s); y = v * (1 – s * (h – i)); z = v * (1 – s * (1 – h + i));
switch (i) {
case 0: r = v; g = z; b = x; break;
case 1: r = y; g = v; b = x; break;
case 2: r = x; g = v; b = z; break;
case 3: r = x; g = y; b = v; break;
case 4: r = z; g = x; b = v; break;
case 5: r = v; g = x; b = y; break;
}
return [r * 255>> 0, g * 255>> 0, b * 255>> 0];
}

}
}

Date Util Functiions

Utility functions to find First Day of a month, Relative Date information like “Today”, “Yesterday”

public static function getFirstDayofMonth(fullyear:uint,month:uint):uint
{
var tmp:Date = new Date(fullyear,month,1);
return tmp.getDay();
}

public static function isLeapYear( fullyear:uint ):Boolean
{
var flag:Boolean = false;
if(((fullyear%4==0) && (fullyear%100!=0)) || (fullyear%400==0)) flag = true;
return flag;
}

public static function getWeekName( index:uint , type:uint=0 ):String
{
var week:Array;
if(type==0) week = [“SUNDAY”,”MONDAY”,”TUSEDAY”,”WEDNESDAY”,”THURSDAY”,”FRIDAY”,”SATURDAY”];
else if(type==1) week = [“SUN”,”MON”,”TUE”,”WED”,”THU”,”FRI”,”SAT”];
else week = [“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”];
return week[index];
}

public static function getMonthName( index:uint , type:uint=0 ):String
{
var month:Array;
if(type==0) month = [“JANUARY”,”FEBRUARY”,”MARCH”,”APRIL”,”MAY”,”JUNE”,”JULY”,”AUGUST”,”SEPTEMBER”,”OCTOBER”,”NOVEMBER”,”DECEMBER”];
else if(type==1) month = [“jan”,”feb”,”mar”,”apr”,”may”,”jun”,”jul”,”aug”,”sep”,”oct”,”nov”,”dec”];
else month = [“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”];
return month[index];
}

public static function getRelativeDateName( date:Date, type:uint=0 ):String
{
var d1:Date = new Date();
d1 = new Date(d1.getFullYear(),d1.getMonth(),d1.getDate());
var d2:Date = new Date(date.getFullYear(),date.getMonth(),date.getDate());
if(d11000*60*60*24*7) return “”;
if(type>8||isNaN(type)) type = 0;
var list:Array;
switch(type) {
case 0: case 3:
list = [“TODAY”,”YESTERDAY”]; break;
case 1: case 4:
list = [“Today”,”Yesterday”]; break;
case 2: case 5:
list = [“today”,”yesterday”]; break;
}
var diff:int = d1.getDate()-d2.getDate();
if(diff<=1) return list[diff];
return getWeekName( d2.getDay(), type );
}

Collision Detection AS3 Util

Collision Detection Utility class for AS3,

Usage: var collide:Rectangle = checkForCollision(obj1, obj2);

package {
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
import flash.geom.Rectangle;

public class CollisionDetection{

public static function checkForCollision(firstObj: DisplayObject,secondObj: DisplayObject):Rectangle
{
var bounds1:Object = firstObj.getBounds(firstObj.root);
var bounds2:Object = secondObj.getBounds(secondObj.root);

if (((bounds1.right < bounds2.left) || (bounds2.right < bounds1.left)) || ((bounds1.bottom < bounds2.top) || (bounds2.bottom < bounds1.top)) ) {
return null;
}

var bounds:Object = {};
bounds.left = Math.max(bounds1.left,bounds2.left);
bounds.right= Math.min(bounds1.right,bounds2.right);
bounds.top = Math.max(bounds1.top,bounds2.top);
bounds.bottom = Math.min(bounds1.bottom,bounds2.bottom);

var w:Number = bounds.right-bounds.left;
var h:Number = bounds.bottom-bounds.top;

if(w < 1 || h < 1)
{
return null;
}

var bitmapData:BitmapData = new BitmapData(w,h,false);

var matrix:Matrix = firstObj.transform.concatenatedMatrix;
matrix.tx -= bounds.left;
matrix.ty -= bounds.top;
bitmapData.draw(firstObj,matrix,new ColorTransform(1,1,1,1,255,-255,-255,255));

matrix = secondObj.transform.concatenatedMatrix;
matrix.tx -= bounds.left;
matrix.ty -= bounds.top;
bitmapData.draw(secondObj,matrix,new ColorTransform(1,1,1,1,255,255,255,255),BlendMode.DIFFERENCE);

var intersection:Rectangle = bitmapData.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF);

/*try{
bitmapData.dispose();
}catch(e:Error){}*/

if (intersection.width == 0) { return null; }

intersection.x += bounds.left;
intersection.y += bounds.top;

return intersection;
}
}
}

Execute Commands on queue

The AS3 Utility Class to add commands in a queue, the usage and class is below:

/** 
* var commands : CommandsQueue = new CommandsQueue();
* commands.addCommand( this, myFunc );
* commands.addCommand( this, myFunc2, [0,2], this, Event.COMPLETE );
* commands.execute();
*/
package
{
import flash.events.Event;
import flash.events.EventDispatcher;

public class CommandsQueue extends EventDispatcher
{
protected var commands : Array;
protected var _index : Number = 0;
public var onComplete : Function;

public function CommandsQueue():void
{
commands = [];
}
public function addCommand( thisObj : Object, func : Function, params: Array = null , completeEventDispatcher : Object = null, completeEventName : String= null ) : void
{
commands.push({
thisObj : thisObj,
func : func,
completeEventDispatcher : completeEventDispatcher || thisObj,
completeEventName : completeEventName,
params : params
})
}
public function get progress():Number
{
return (commands.length>0)? _index / commands.length * 100 : 0;
}
public function cancel() : void
{
throw new Error(“commandQueue.cancel is not implemented yet”);
}
public function execute() : void
{
doNext();
}
public function get index():int
{
return _index;
}
public function get length():int
{
return commands.length;
}
protected function doNext() : void
{
var act : Object = commands[ _index ];

if( act.completeEventName ){
act.completeEventDispatcher.addEventListener(act.completeEventName, _commandComplete, false, 0, true);
act.func.apply( act.thisObj, act.params );
} else {
act.func.apply( act.thisObj, act.params );
doNextCommand();
}
}
protected function doNextCommand() : void
{
var act : Object = commands[ _index ];

if( act.completeEventName )
act.completeEventDispatcher.removeEventListener(act.completeEventName, _commandComplete);

_index ++;

if( _index == commands.length )
{
commands = []; //remove all registerd command for GC
if( onComplete != null )
onComplete();
dispatchEvent( new Event(Event.COMPLETE) );
}else{
doNext();
}
}
protected function _commandComplete( e : Event ) : void
{
doNextCommand();
}
}