Posted By

hejaaa on 12/07/10


Tagged

color average


Versions (?)

Average color


 / Published in: ActionScript 3
 

  1. package code.utils {
  2. import flash.display.MovieClip;
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.geom.ColorTransform;
  6.  
  7. public class changeColor {
  8.  
  9. // AVERAGE BITMAP COLOR
  10. //
  11. public static function averageBitmapColor(mc:MovieClip):uint {
  12. var averageColor:uint;
  13. var bmpData:BitmapData = new BitmapData(mc.width, mc.height);
  14. bmpData.draw(mc);
  15. var r:Number = 0;
  16. var g:Number = 0;
  17. var b:Number = 0;
  18. var count:Number = 0;
  19. var pixel:Number;
  20. for (var ix:int = 0; ix < bmpData.width; ix++){
  21. for (var iy:int = 0; iy < bmpData.height; iy++){
  22. pixel = bmpData.getPixel(ix, iy);
  23. //if (pixel != 16777215){
  24. r += pixel >> 16 & 0xFF;
  25. g += pixel >> 8 & 0xFF;
  26. b += pixel & 0xFF;
  27. count++;
  28. //}
  29. }
  30. }
  31. r /= count;
  32. g /= count;
  33. b /= count;
  34. averageColor = r << 16 | g << 8 | b;
  35. return averageColor;
  36. }
  37.  
  38.  
  39. // MOVIECLIP CHANGE COLOR
  40. //
  41. public static function change(mc:MovieClip, color:uint):void {
  42. var newCT:ColorTransform = mc.transform.colorTransform;
  43. newCT.color = uint(color);
  44. mc.transform.colorTransform = newCT;
  45. }
  46.  
  47. }
  48.  
  49. }

Report this snippet  

You need to login to post a comment.