1
10
11 import com.sekati.math.MathBase;
12
13
16 class com.sekati.utils.ColorUtils {
17
18
22 public static function hex2rgb(hex:Number):Object {
23 return { r: (hex >> 16) & 0xff, g: (hex >> 8) & 0xff, b: hex & 0xff};
24 }
25
26
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
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
48 public static function rgb2hex(r:Number, g:Number, b:Number):Number {
49 return (r << 16 | g << 8 | b);
50 }
51
52
57 public static function rgbObj2hex(o:Object):Number {
58 return (o.r << 16 | o.g << 8 | o.b);
59 }
60
61
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
79 public static function randHex():String {
80 return "0x" + Math.floor( Math.random( ) * 16777215 ).toString( 16 ).toUpperCase( );
81 }
82
83
89 public static function setColor(obj:Object,hex:Number):Void {
90 var c:Color = new Color( obj );
91 c.setRGB( hex );
92 }
93
94
99 public static function getColor(obj:Object):Number {
100 var c:Color = new Color( obj );
101 return c.getRGB( );
102 }
103
104
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 }