# Statistics

Viewed 424 times
Favorited by 0 user(s)

# AS3 Get Average Colour of an Image

/ Published in: ActionScript 3
Save to your folder(s)

The docs for the Sekati SWC are here: http://docs.sekati.com/sekati/sekati/utils/ColorUtil.html

Another useful blog post: http://blog.soulwire.co.uk/code/actionscript-3/extract-average-colours-from-bitmapdata

Copy this code and paste it in your HTML
`package {	import flash.display.Bitmap;	import flash.display.Sprite;	import flash.events.Event;	import flash.geom.ColorTransform; 	import sekati.utils.ColorUtil; 	public class Main extends Sprite 	{ 		[Embed(source="../assets/hero.jpg")]		private var StillImage:Class;  		public function Main():void 		{			if (stage) init();			else addEventListener(Event.ADDED_TO_STAGE, init);		} 		private function init(e:Event = null):void 		{			removeEventListener(Event.ADDED_TO_STAGE, init); 			var image:Bitmap = new StillImage();			addChild(image); 			var swatch:Sprite = new Sprite();			swatch.graphics.beginFill(0xFF0000);			swatch.graphics.drawRect(0, 0, image.width, 40);			swatch.graphics.endFill();			swatch.x = image.x;			swatch.y = image.y + image.height;			addChild(swatch); 			var averageRed:int = ColorUtil.averageRed(image);			var averageGreen:int = ColorUtil.averageGreen(image);			var averageBlue:int = ColorUtil.averageBlue(image); 			var colour:uint = averageRed << 16 | averageGreen << 8 | averageBlue; 			trace(averageRed + ", " + averageGreen + ", " + averageBlue); 			var ct:ColorTransform = new ColorTransform();			ct.color = (colour);			swatch.transform.colorTransform = ct; 			var hexColour:String = "#" + colour.toString(16).toUpperCase();			trace("hexColour: "+hexColour); 		} 	} }`