Skip to content

Instantly share code, notes, and snippets.

@loteixeira
Last active December 17, 2015 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loteixeira/5637721 to your computer and use it in GitHub Desktop.
Save loteixeira/5637721 to your computer and use it in GitHub Desktop.
AS3 Pixel Conversion Utils - supports uint, Array and Object.
//
// AS3 Pixel Conversion Utils
// Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
// Profile page: https://github.com/loteixeira
//
// This software is distribuited under the terms of the WTFPL
// http://www.wtfpl.net/txt/copying/
//
package
{
public class Pixel
{
/**
* Converts a 24 bits pixel (red, green and blue components) from an uint to an Object.
* @param v 24 bits pixel
* @param r Result object - if not provided a new one is created
* @return Result object (the same as r if isn't null)
*/
public static function int2obj(v:uint, r:Object = null):Object
{
if (!r)
r = {r: null, g: null, b: null};
r.r = (v & 0xff0000) >> 16;
r.g = (v & 0x00ff00) >> 8;
r.b = (v & 0x0000ff);
return r;
}
/**
* Converts a 24 bits pixel (red, green and blue components) from an uint to an Array.
* @param v 24 bits pixel
* @param r Result array - if not provided a new one is created
* @return Result array (the same as r if isn't null)
*/
public static function int2arr(v:uint, r:Array = null):Array
{
if (!r)
r = [null, null, null];
r[0] = (v & 0xff0000) >> 16;
r[1] = (v & 0x00ff00) >> 8;
r[2] = (v & 0x0000ff)
return r;
}
/**
* Converts a 32 bits pixel (alpha, red, green and blue components) from an uint to an Object.
* @param v 32 bits pixel
* @param r Result object - if not provided a new one is created
* @return Result object (the same as r if isn't null)
*/
public static function int2obj32(v:uint, r:Object = null):Object
{
if (!r)
r = {a: null, r: null, g: null, b: null};
r.a = (v & 0xff000000) >> 24;
r.r = (v & 0x00ff0000) >> 16;
r.g = (v & 0x0000ff00) >> 8;
r.b = (v & 0x000000ff);
return r;
}
/**
* Converts a 32 bits pixel (alpha, red, green and blue components) from an uint to an Array.
* @param v 32 bits pixel
* @param r Result array - if not provided a new one is created
* @return Result array (the same as r if isn't null)
*/
public static function int2arr32(v:uint, r:Array = null):Array
{
if (!r)
r = [null, null, null, null];
r[0] = (v & 0xff000000) >> 24;
r[1] = (v & 0x00ff0000) >> 16;
r[2] = (v & 0x0000ff00) >> 8;
r[3] = (v & 0x000000ff)
return r;
}
/**
* Converts a 24 bits pixel (red, green and blue components) from an Object to an unit
* @param v Source object - must have r, g and b attributes
* @return Result 24 bits pixel
*/
public static function obj2int(v:Object):uint
{
return (v.r << 16) | (v.g << 8) | v.b;
}
/**
* Converts a 24 bits pixel (red, green and blue components) from an Array to an unit
* @param v Source array - must have at least three elements
* @return Result 24 bits pixel
*/
public static function arr2int(v:Array):uint
{
return (v[0] << 16) | (v[1] << 8) | v[2];
}
/**
* Converts a 32 bits pixel (alpha, red, green and blue components) from an Object to an unit
* @param v Source object - must have a, r, g and b attributes
* @return Result 32 bits pixel
*/
public static function obj2int32(v:Object):uint
{
return (v.a << 24) | (v.r << 16) | (v.g << 8) | v.b;
}
/**
* Converts a 32 bits pixel (alpha, red, green and blue components) from an Array to an unit
* @param v Source array - must have at least four elements
* @return Result 32 bits pixel
*/
public static function arr2int32(v:Array):uint
{
return (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | v[3];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment