AS3: Pick color from object

626 views Asked by At

I make some object and colored with Color Transform. Here's my code:

function createColorItems():void
{
    for (var i:int = 0; i < colorLength; i++)
    {
        var myColor:Object = new colorArea  ;
        var colorTrans:ColorTransform = new ColorTransform  ;
        arrColorTrans[i] = myXML.bag.color.item[i];
        arrItem.push(myColor);
        arrItem[i].x = 40 * i + 40;
        arrItem[i].y = 300;
        addChild(arrItem[i]);
        colorTrans.color = Number(arrColorTrans[i]);
        arrItem[i].transform.colorTransform = colorTrans;
        arrItem[i].addEventListener(MouseEvent.CLICK,changeColor);
    }
}

This is where i change the color.

function changeColor():void
{
    trace(e.target.color);
    myBox.graphics.beginFill(0x000000,0.5);
    myBox.graphics.drawRect(myImg.x,myImg.y,bagImg.width,bagImg.height);
    myBox.graphics.endFill();
    myBox.transform.colorTransform = publicColor;
    addChild(myBox);
}

what i want is when the object is clicked, the other object's color is change. I trace it with trace(e.target.color) but it's wrong. i use publicColor to pick the color from colorTrans, but i don't know how to pick the color? is it possible??

Sorry for my bad grammar, please help.

2

There are 2 answers

0
eleven On

You can use getPixel method of BitmapData. Here is a documentation. Example of using:

import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.display.Sprite;

var s0:Sprite = new Sprite();
var s1:Sprite = new Sprite();
var testS:Sprite = new Sprite();
var bmd:BitmapData = new BitmapData(1,1);

s0.graphics.beginFill(0x010FFF);
s0.graphics.drawRect(0, 0, 100, 100);
s0.graphics.endFill();
s0.x = 200;
s0.y = 200;

s1.graphics.beginFill(0x555FFF);
s1.graphics.drawRect(0, 0, 100, 100);
s1.graphics.endFill();
s1.x = 300;
s1.y = 200;

addChild(s0);
addChild(s1);
addChild(testS);
addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:MouseEvent):void{
    const clicked:DisplayObject = event.target as DisplayObject;

    bmd.fillRect(bmd.rect, 0x000000);
    bmd.draw(clicked, new Matrix(1, 0, 0, 1, clicked.x - mouseX, clicked.y - mouseY));

    const color:uint = bmd.getPixel(0, 0);
    testS.graphics.beginFill(color);
    testS.graphics.drawRect(0, 0, 100, 100);
    testS.graphics.endFill();

    trace("color:" + color);
}
0
shahho On
BitmapData.getPixel(x:int, y:int):uint

or

BitmapData.getPixel32(x:int, y:int):uint

Returns an ARGB color value that contains alpha channel data and RGB data.