AddThis Social Bookmark Button
 Color picking with the BitmapData class and the getPixel method

download source files download source files


A practical application of the BitmapData class

The BitmapData class has been introduced in Flash 8 to give us pixel-level control of bitmap object. With the BitmapData class comes a fair amount of methods and properties that you can discover in the Flash 8 manual. But for now, and as a gentle introduction, I would like to show you how we can use the BitmapData class in a practical and simple way to approach the concept of color picking just using a couple of methods and very little code. First let's have a look at the final version below.

The code


Let's have a look at the code below, roll over the comments if you want to see the comments associated with the nearby code (be sure you have javascript enabled for this to work properly).

import flash.display.BitmapData;

linkageId = "landscape";
myBitmapData = BitmapData.loadBitmap(linkageId);

mc = this.createEmptyMovieClip("mc", 0);
mc.attachBitmap(myBitmapData, 0);

selectedColor.swapDepths(_root.getNextHighestDepth());
newColor = new Color(selectedColor.sample);

onMouseMove = function(){
myNewColor = "0x" + myBitmapData.getPixel(_xmouse,_ymouse).toString(16);
newColor.setRGB(myNewColor);
selectedColor.colorValue.text = myNewColor;
}

This little example just give you a little glimpse of the possibilities offered by the BitmapData class but hopefully you will find it useful if you are looking for that particular type of method to retrieve a color code from a picture.

Good Luck :)

AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved