1  /**
     2   * com.sekati.utils.ColorUtils
     3   * @version 1.0.0
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   * 
     8   * Some methods adopted from ca.nectere and gskinner for dependencies only
     9   */
    10   
    11  import com.sekati.math.MathBase;
    12  
    13  /**
    14   * Static class wrapping various Color utilities.
    15   */
    16  class com.sekati.utils.ColorUtils {
    17  
    18  	/**
    19  	 * Convert hexadecimal Number to rgb Object
    20  	 * @return Object - {r,g,b}
    21  	 */
    22  	public static function hex2rgb(hex:Number):Object {
    23  		return { r: (hex >> 16) & 0xff, g: (hex >> 8) & 0xff, b: hex & 0xff};
    24  	}
    25  
    26  	/**
    27  	 * Convert argb Object to hexidecimal Number
    28  	 * @return Object - {a,r,g,b}
    29  	 */
    30  	public static function hex2argb(val:Number):Object {
    31  		return {a: (val >> 24) & 0xff, r: (val >> 16) & 0xff, g: (val >> 8) & 0xff, b: val & 0xff};
    32  	}	
    33  
    34  	/**
    35  	 * Convert argb to hexadecimal.
    36  	 * {@code Usage:
    37  	 * 	hex=argbtohex(255,0,255,0) //outputs a 32-bit red hexadecimal value as a base-10 number
    38  	 * 	}
    39  	 */
    40  	public static function argb2hex(a:Number, r:Number, g:Number, b:Number):Number {
    41  		return (a << 24 | r << 16 | g << 8 | b);
    42  	}
    43  
    44  	/**
    45  	 * Convert rgb to hexadecimal.
    46  	 * @return Number
    47  	 */
    48  	public static function rgb2hex(r:Number, g:Number, b:Number):Number {
    49  		return (r << 16 | g << 8 | b);
    50  	}	
    51  
    52  	/**
    53  	 * Convert rgb Object to hexadecimal
    54  	 * @param o (Object) a color object {r,g,b}
    55  	 * @return Number
    56  	 */
    57  	public static function rgbObj2hex(o:Object):Number {
    58  		return (o.r << 16 | o.g << 8 | o.b);
    59  	}		
    60  
    61  	/**
    62  	 * Convert rgb to hexadecimal String
    63  	 * @param r (Number)
    64  	 * @param g (Number)
    65  	 * @param b (Number)
    66  	 * @param hasPrefix (Boolean) add optional "0x" prefix to return
    67  	 * @return String
    68  	 */
    69  	public static function rgb2hexString(r:Number, g:Number, b:Number, hasPrefix:Boolean):String {
    70  		var hex:String = rgb2hex( r, g, b ).toString( 16 );
    71  		while(hex.length < 6) hex = "0" + hex;
    72  		return (hasPrefix) ? "0x" + hex : hex;
    73  	}
    74  
    75  	/**
    76  	 * get a random hexidecimal color
    77  	 * @return String
    78  	 */
    79  	public static function randHex():String {
    80  		return "0x" + Math.floor( Math.random( ) * 16777215 ).toString( 16 ).toUpperCase( );
    81  	}
    82  
    83  	/**
    84  	 * set color of a movieclip or textfield
    85  	 * @param obj (Object)
    86  	 * @param hex (Number)
    87  	 * @return Void
    88  	 */
    89  	public static function setColor(obj:Object,hex:Number):Void {
    90  		var c:Color = new Color( obj );
    91  		c.setRGB( hex );
    92  	}
    93  
    94  	/**
    95  	 * get color from a movieclip or textfield
    96  	 * @param obj (Object)
    97  	 * @return Number
    98  	 */
    99  	public static function getColor(obj:Object):Number {
   100  		var c:Color = new Color( obj );
   101  		return c.getRGB( );
   102  	}
   103  
   104  	/**
   105  	 * Change the contrast of a hexidecimal Number by a certain increment
   106  	 * @param hex (Number) color value to shift contrast on
   107  	 * @param inc (Number) increment value to shift
   108  	 * @return Number - new hex color value
   109  	 */
   110  	public static function changeContrast(hex:Number, inc:Number):Number {
   111  		var o:Object = ColorUtils.hex2rgb( hex );
   112  		o.r = MathBase.constrain( o.r + inc, 0, 255 );
   113  		o.g = MathBase.constrain( o.g + inc, 0, 255 );
   114  		o.b = MathBase.constrain( o.b + inc, 0, 255 );
   115  		return ColorUtils.rgb2hex( o.r, o.g, o.b );
   116  	}	
   117  
   118  	private function ColorUtils() {
   119  	}
   120  }