02
Aug
07

Finding a pixel’s color value using the Bitmap classes and getPixel()

This example loads an image and then uses a combination of the Bitmap and BitmapData classes to determine the color value under the mouse cursor. Pretty basic, but kind of neat. Maybe? Sorta?

Full code after the jump.

Download source (ZIP, 1K) | View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/02/finding-a-pixels-color-value-using-the-bitmap-classes-and-getpixel/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private var bm:Bitmap;
            private var bmd:BitmapData;

            private function image_complete(evt:Event):void {
                /* Create Bitmap from Image content. */
                bm = img.content as Bitmap;

                /* Create new BitmapData object. */
                bmd = new BitmapData(img.contentWidth, img.contentHeight);

                /* Draw Bitmap into BitmapData. */
                bmd.draw(bm.bitmapData);
            }

            private function image_mouseMove(evt:MouseEvent):void {
                /* Get the pixel currently under the mouse cursor. */
                var color:int = bmd.getPixel(evt.localX, evt.localY);

                /* Convert the color value from a number to Hex string. */
                var colorStr:String = color.toString(16).toUpperCase();

                /* Set the swatch Box instance's backgroundColor style to the color under the mouse cursor. */
                swatch.setStyle("backgroundColor", color);

                /* Make sure colorStr is at least 6 characters. */
                colorStr = "000000" + colorStr;

                /* Make sure colorStr is at MOST 6 characters. */
                lbl.text = "#" + colorStr.substr(colorStr.length - 6);
            }
        ]]>
    </mx:Script>

    <mx:Zoom id="zoom" />

    <mx:VBox>
        <mx:Image id="img" source="image1.jpg" completeEffect="{zoom}" complete="image_complete(event);" mouseMove="image_mouseMove(event)" />

        <mx:HBox>
            <mx:Box id="swatch" width="{lbl.height}" height="{lbl.height}" />
            <mx:Label id="lbl" width="100" fontFamily="_typewriter" fontSize="16" />
        </mx:HBox>
    </mx:VBox>

</mx:Application>

View source enabled in the following example.


4 Responses to “Finding a pixel's color value using the Bitmap classes and getPixel()”


  1. 1 Alex Nov 3rd, 2007 at 4:52 pm

    This cool aplication

  2. 2 Atom Nov 25th, 2007 at 8:34 pm

    Ok. It’s fine. I like it. But what about R G B color? What code should be instead of # ????

  3. 3 Daniel Dec 17th, 2007 at 10:22 pm

    Hello, your example looks cool and simple, but I can’t get it to run properly.. seems to hang on this line:

    var color:int = bmd.getPixel(evt.localX, evt.localY);

    Not sure why. I’m compiling on osx

    Daniel

  4. 4 Lee Jan 17th, 2008 at 6:06 am

    very beautiful~:)

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".