zoukankan      html  css  js  c++  java
  • 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?
    <?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>
  • 相关阅读:
    HDU 6187 Destroy Walls
    HDU1596 find the safest road
    美国机遇号失联已久,NASA想出一奇招,网友看后:这我上我也行!
    萨拉飞机或掉入恐怖深渊,那里满是核废料
    Problem : 恢复一棵树
    Problem : [tju1071]一个简单题
    Problem : 马农
    人类首个“触日”探测器绕日第二圈
    美国三岁男孩森林迷路两天 遇黑熊“热心帮忙”终获救
    世上最孤独鸭子去世
  • 原文地址:https://www.cnblogs.com/taobataoma/p/1283440.html
Copyright © 2011-2022 走看看