PageRenderTime 31ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/utils/source/temple/utils/color/ARGB.as

http://templelibrary.googlecode.com/
ActionScript | 212 lines | 116 code | 21 blank | 75 comment | 16 complexity | 57fa9a8d9eb3f3ca97f319a718371391 MD5 | raw file
  1. /*
  2. * Temple Library for ActionScript 3.0
  3. * Copyright Š MediaMonks B.V.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by MediaMonks B.V.
  16. * 4. Neither the name of MediaMonks B.V. nor the
  17. * names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY MEDIAMONKS B.V. ''AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL MEDIAMONKS B.V. BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *
  32. * Note: This license does not apply to 3rd party classes inside the Temple
  33. * repository with their own license!
  34. */
  35. package temple.utils.color
  36. {
  37. import temple.core.debug.objectToString;
  38. /**
  39. * Data object for storing color value with some basic convertion method.
  40. *
  41. * @author Bart van der Schoor
  42. */
  43. public class ARGB
  44. {
  45. private static const _TO_STRING_PROPS:Vector.<String> = Vector.<String>(['r', 'g', 'b', 'a']);
  46. /**
  47. * Converts a red, green, blue and alpha value to a single ARGB uint.
  48. * @param r the red component of the color. Value must be between 0 and 255 (0xFF)
  49. * @param g the green component of the color. Value must be between 0 and 255 (0xFF)
  50. * @param b the blue component of the color. Value must be between 0 and 255 (0xFF)
  51. * @param a the alpha component of the color. Value must be between 0 and 255 (0xFF)
  52. */
  53. public static function getColor(r:uint, g:uint, b:uint, a:uint = 255):uint
  54. {
  55. return (a << 24) | (r << 16) | (g << 8) | b;
  56. }
  57. /**
  58. * Creates an ARBG object from a color value.
  59. */
  60. public static function fromColor(color:int):ARGB
  61. {
  62. var a:ARGB = new ARGB();
  63. a.color = color;
  64. return a;
  65. }
  66. public var a:uint = 255;
  67. public var r:uint;
  68. public var g:uint;
  69. public var b:uint;
  70. /**
  71. * Create a new ARGB object.
  72. * @param r the red component of the color. Value must be between 0 and 255 (0xFF)
  73. * @param g the green component of the color. Value must be between 0 and 255 (0xFF)
  74. * @param b the blue component of the color. Value must be between 0 and 255 (0xFF)
  75. * @param a the alpha component of the color. Value must be between 0 and 255 (0xFF)
  76. *
  77. */
  78. public function ARGB(r:uint = 0, g:uint = 0, b:uint = 0, a:uint = 255)
  79. {
  80. this.a = a;
  81. this.r = r;
  82. this.g = g;
  83. this.b = b;
  84. }
  85. /**
  86. * The color as uint value.
  87. */
  88. public function get color():uint
  89. {
  90. return (this.a << 24) | (this.r << 16) | (this.g << 8) | this.b;
  91. }
  92. /**
  93. * @private
  94. */
  95. public function set color(color:uint):void
  96. {
  97. this.a = color >> 24 & 0xFF;
  98. this.r = color >> 16 & 0xFF;
  99. this.g = color >> 8 & 0xFF;
  100. this.b = color & 0xFF;
  101. }
  102. /**
  103. * Returns the value as ARGB hexadecimal String
  104. */
  105. public function getARGBHexString():String
  106. {
  107. var aa:String = this.a.toString(16);
  108. var rr:String = this.r.toString(16);
  109. var gg:String = this.g.toString(16);
  110. var bb:String = this.b.toString(16);
  111. aa = (aa.length == 1) ? '0' + aa : aa;
  112. rr = (rr.length == 1) ? '0' + rr : rr;
  113. gg = (gg.length == 1) ? '0' + gg : gg;
  114. bb = (bb.length == 1) ? '0' + bb : bb;
  115. return (aa + rr + gg + bb).toUpperCase();
  116. }
  117. /**
  118. * Returns the value as RGB hexadecimal String
  119. */
  120. public function getRGBHexString():String
  121. {
  122. var rr:String = this.r.toString(16);
  123. var gg:String = this.g.toString(16);
  124. var bb:String = this.b.toString(16);
  125. rr = (rr.length == 1) ? '0' + rr : rr;
  126. gg = (gg.length == 1) ? '0' + gg : gg;
  127. bb = (bb.length == 1) ? '0' + bb : bb;
  128. return (rr + gg + bb).toUpperCase();
  129. }
  130. /**
  131. * Returns the uncorrected greyscale value
  132. */
  133. public function getAverage():Number
  134. {
  135. return (this.r + this.g + this.b) / (3 * 255);
  136. }
  137. public function getBrightness():Number
  138. {
  139. var _max:Number = Math.max(this.r, this.g, this.b);
  140. //var _min:Number = Math.min(this.r, this.g, this.b);
  141. return _max / 255 * 100;
  142. }
  143. public function getSaturation():Number
  144. {
  145. var _max:Number = Math.max(this.r, this.g, this.b);
  146. var _min:Number = Math.min(this.r, this.g, this.b);
  147. return (_max != 0) ? (_max - _min) / _max * 100 : 0;
  148. }
  149. public function fromUINT(color:uint):ARGB
  150. {
  151. this.a = color >> 24 & 0xFF;
  152. this.r = color >> 16 & 0xFF;
  153. this.g = color >> 8 & 0xFF;
  154. this.b = color & 0xFF;
  155. return this;
  156. }
  157. //parse standard hex color-string: FFFFFFFF, 0xFFFFFFFF, #FFFFFFFF
  158. public function fromString(hex:String):ARGB
  159. {
  160. if(hex.length < 1)
  161. {
  162. return this;
  163. }
  164. if(hex.substr(0, 2) == '0x')
  165. {
  166. hex = hex.substr(2);
  167. }
  168. else if(hex.substr(0, 1) == '#')
  169. {
  170. hex = hex.substr(1);
  171. }
  172. //chop zeroes
  173. var i:uint = 0;
  174. while(hex.substr(i, 1) == '0' || i > hex.length)
  175. {
  176. hex = hex.substr(i);
  177. i++;
  178. }
  179. var color:uint = parseInt(hex, 16);
  180. this.a = color >> 24 & 0xFF;
  181. this.r = color >> 16 & 0xFF;
  182. this.g = color >> 8 & 0xFF;
  183. this.b = color & 0xFF;
  184. return this;
  185. }
  186. public function toString():String
  187. {
  188. return objectToString(this, _TO_STRING_PROPS);
  189. }
  190. }
  191. }