Posted By

adrianparr on 02/05/11


Tagged

colour color as3 middle average mix


Versions (?)

AS3 Average Colour Between Two Colours


 / Published in: ActionScript 3
 

URL: http://www.libspark.org/wiki/scratchbrain/AverageColor

Full credit for this code goes to Scratchbrain (http://www.scratchbrain.net/blog/ver2/)

  1. package net.scratchbrain.color
  2. {
  3. import net.scratchbrain.color.AverageColor;
  4. public class AverageColor
  5. {
  6.  
  7. public static function averageHex(_hex1:uint,_hex2:uint):uint
  8. {
  9. var rgb1:Object = ConvertColor.HexToRGB(_hex1);
  10. var rgb2:Object = ConvertColor.HexToRGB(_hex2);
  11. var hsb1:Object = ConvertColor.RGBToHSB(rgb1.r, rgb1.g, rgb1.b);
  12. var hsb2:Object = ConvertColor.RGBToHSB(rgb2.r, rgb2.g, rgb2.b);
  13. return averageHsb(hsb1.h, hsb1.s, hsb1.b, hsb2.h, hsb2.s, hsb2.b);
  14.  
  15. }
  16. public static function averageRgb(_r1:int,_g1:int,_b1:int,_r2:int,_g2:int,_b2:int):uint
  17. {
  18. var hsb1:Object = ConvertColor.RGBToHSB(_r1, _g1, _b1);
  19. var hsb2:Object = ConvertColor.RGBToHSB(_r2, _g2, _b2);
  20. return averageHsb(hsb1.h, hsb1.s, hsb1.b, hsb2.h, hsb2.s, hsb2.b);
  21. }
  22. public static function averageHsb(_h1:int, _s1:int, _b1:int, _h2:int, _s2:int, _b2:int):uint
  23. {
  24. var _h:int;
  25. var _s:int;
  26. var _b:int;
  27.  
  28. if(_h1 <= 18 && _s1 != 0 && _h2 >= 180){
  29. _h1 = 360;
  30. }
  31. if(_h2 <= 18 && _s2 != 0 && _h1 >= 180){
  32. _h2 = 360;
  33. }
  34.  
  35. if(_s1 != 0 && _s2 != 0){
  36. _h = (_h1 + _h2)/2;
  37. _s = (_s1 + _s2)/2;
  38. _b = (_b1 + _b2)/2;
  39. }else if(_s1 == 0 && _s2 == 0){
  40. _h = 0;
  41. _s = 0;
  42. _b = (_b1 + _b2)/2;
  43. }else{
  44. _h = (_h1 != 0) ? _h1 : _h2;
  45. _s = (_s1 + _s2)/2;
  46. _b = (_b1 + _b2)/2;
  47. }
  48.  
  49. var rgb:Object = ConvertColor.HSBToRGB(_h, _s, _b);
  50. return ConvertColor.RGBToHex(rgb.r, rgb.g, rgb.b);
  51. }
  52. }
  53. }
  54.  
  55.  
  56. package net.scratchbrain.color
  57. {
  58. public class ConvertColor
  59. {
  60.  
  61. public static function RGBToHex(r:int,g:int,b:int):uint
  62. {
  63. var hex:uint = r<<16 | g<<8 | b;
  64. return hex;
  65. }
  66.  
  67. public static function HexToRGB(value:uint):Object{
  68. var rgb:Object = new Object();
  69. rgb.r = (value >> 16) & 0xFF
  70. rgb.g = (value >> 8) & 0xFF
  71. rgb.b = value & 0xFF
  72. return rgb;
  73. }
  74.  
  75. public static function RGBToHSB(r:int,g:int,b:int):Object{
  76. var hsb:Object = new Object;
  77. var _max:Number = Math.max(r,g,b);
  78. var _min:Number = Math.min(r,g,b);
  79.  
  80. hsb.s = (_max != 0) ? (_max - _min) / _max * 100: 0;
  81.  
  82. hsb.b = _max / 255 * 100;
  83.  
  84. if(hsb.s == 0){
  85. hsb.h = 0;
  86. }else{
  87. switch(_max)
  88. {
  89. case r:
  90. hsb.h = (g - b)/(_max - _min)*60 + 0;
  91. break;
  92. case g:
  93. hsb.h = (b - r)/(_max - _min)*60 + 120;
  94. break;
  95. case b:
  96. hsb.h = (r - g)/(_max - _min)*60 + 240;
  97. break;
  98. }
  99. }
  100.  
  101. hsb.h = Math.min(360, Math.max(0, Math.round(hsb.h)))
  102. hsb.s = Math.min(100, Math.max(0, Math.round(hsb.s)))
  103. hsb.b = Math.min(100, Math.max(0, Math.round(hsb.b)))
  104.  
  105. return hsb;
  106. }
  107.  
  108. public static function HSBToRGB(h:int,s:int,b:int):Object{
  109. var rgb:Object = new Object();
  110.  
  111. var max:Number = (b*0.01)*255;
  112. var min:Number = max*(1-(s*0.01));
  113.  
  114. if(h == 360){
  115. h = 0;
  116. }
  117.  
  118. if(s == 0){
  119. rgb.r = rgb.g = rgb.b = b*(255*0.01) ;
  120. }else{
  121. var _h:Number = Math.floor(h / 60);
  122.  
  123. switch(_h){
  124. case 0:
  125. rgb.r = max ;
  126. rgb.g = min+h * (max-min)/ 60;
  127. rgb.b = min;
  128. break;
  129. case 1:
  130. rgb.r = max-(h-60) * (max-min)/60;
  131. rgb.g = max;
  132. rgb.b = min;
  133. break;
  134. case 2:
  135. rgb.r = min ;
  136. rgb.g = max;
  137. rgb.b = min+(h-120) * (max-min)/60;
  138. break;
  139. case 3:
  140. rgb.r = min;
  141. rgb.g = max-(h-180) * (max-min)/60;
  142. rgb.b =max;
  143. break;
  144. case 4:
  145. rgb.r = min+(h-240) * (max-min)/60;
  146. rgb.g = min;
  147. rgb.b = max;
  148. break;
  149. case 5:
  150. rgb.r = max;
  151. rgb.g = min;
  152. rgb.b = max-(h-300) * (max-min)/60;
  153. break;
  154. case 6:
  155. rgb.r = max;
  156. rgb.g = min+h * (max-min)/ 60;
  157. rgb.b = min;
  158. break;
  159. }
  160.  
  161. rgb.r = Math.min(255, Math.max(0, Math.round(rgb.r)))
  162. rgb.g = Math.min(255, Math.max(0, Math.round(rgb.g)))
  163. rgb.b = Math.min(255, Math.max(0, Math.round(rgb.b)))
  164. }
  165. return rgb;
  166. }
  167. }
  168. }

Report this snippet  

You need to login to post a comment.