Groups | Blog | Home
all groups > flash actionscript > april 2006 >

flash actionscript : How Do I Get the RGB values of MC clip to a variable?



Ney Alencar
4/11/2006 8:41:37 PM
Hello!

Does anyone know how to get the RGB value of movie clip to show in a variable?

Example: _root.redBox_mc;
var myColors;

Anyone?

Thanks!


abeall
4/11/2006 9:02:21 PM
"The RGB value"? Potentially a movieclip contains thousands of colors. If you
apply a Color Tint or something to the MovieClip, you can retrieve values via
the Color(Flash 6-7) object or the ColorTransform class(Flash 8)
babo_ya
4/11/2006 9:42:25 PM
var color = new Color(_root.redBox_mc);
var RGB = color.getRGB();

trace(RGB);

abeall
4/12/2006 3:13:11 PM
That won't work. Or, rather, it will only work to the extent that the clip has
already been tinted. In other words, a box authored in Flash as red will not
return 16711680(which is 0xff0000), it will return 0, which is basically like
saying the object is untinted. If, however, you apply a color tint to the
object(no matter what color it was) either in the authoring environment or via
Color.setRGB(), it will give you the tint color.

You should look up the Color object to understand better what's going on, but
also be warned that the Color object is deprecated in Flash 8, replaced by
ColorTransform.
abeall
4/12/2006 3:15:27 PM
I will mention at this point that in Flash 8, you could use BitmapData to
create a temporary bitmap from your MC, and use getPixel() to get the color
value of a certain pixel(probably it's center), and if your object is solid
that will in effect do what you want. Look around, I know there has been made a
custom MovieClip.getPixel() method someplace.
Ney Alencar
4/12/2006 3:35:45 PM
I don't want to set any color, I just want to get a rgb value or pixel value to
a variable... like this:

var color = new Color(_root.redBox_mc);
var RGB = color.getRGB(????????);

trace(RGB);
Like beally says, it will return "0" o my variable... How do I get the value?
Is there any sample out there or code? Maybe the get-pixel is the way to go...
but how is done? Thanks For The Help Everyone!

Best

Ney



babo_ya
4/12/2006 9:47:12 PM
BitmapData, getPixel, colorTransform are ONLY for Flash player 8.

I would use those if you are exporting your movie in Flash Player 8 .
You have to let me know more about _root.redBox_mc.
Is it just ONE COLOR movieclip? if it is, do you know what its RGB color value
is?




NSurveyor
4/12/2006 9:58:44 PM
Here's my prototype:

import flash.display.BitmapData;
import flash.geom.Matrix;
MovieClip.prototype.getPixel = function(x:Number, y:Number):Number {
var bmp:BitmapData = new BitmapData(this._width, this._height, true,
0);
var xm:Number = -this.getBounds(this).xMin;
var ym:Number = -this.getBounds(this).yMin;
bmp.draw(this, new Matrix(1, 0, 0, 1, xm, ym));
var col:Number = bmp.getPixel(x+xm, y+ym);
bmp.dispose();
return col;
};

Then you can use something like:

var col =
'0x'+_root.redBox_mc.getPixel(_root.redBox_mc._xmouse,_root.redBox_mc._ymouse).t
oString(16);
trace(col);
abeall
4/12/2006 10:10:19 PM
Ney Alencar
4/13/2006 8:50:17 PM
beally,

I am only getting
abeall
4/13/2006 8:55:17 PM
NSurveyor
4/13/2006 9:28:02 PM
Ney Alencar must have Flash 8 as it didn't generate errors and it returned 0x0
(which happens when you go over a "non-colored" area).

var col =
'0x'+_root.redBox_mc.getPixel(_root.redBox_mc._xmouse,_root.redBox_mc._ymouse).t
oString(16);

was just an example. That will get the pixel color under the mouse... if you
want to check a specific, (x,y), use:

_root.redBox_mc.getPixel(x,y)
Ney Alencar
4/14/2006 2:11:02 PM
Yes, I am using flash8...

and I did try your code:
import flash.display.BitmapData;
import flash.geom.Matrix;
MovieClip.prototype.getPixel = function(x:Number, y:Number):Number {
var bmp:BitmapData = new BitmapData(this._width, this._height, true, 0);
var xm:Number = -this.getBounds(this).xMin;
var ym:Number = -this.getBounds(this).yMin;
bmp.draw(this, new Matrix(1, 0, 0, 1, xm, ym));
var col:Number = bmp.getPixel(x+xm, y+ym);
bmp.dispose();
return col;
};


var col =
'0x'+_root.redBox_mc.getPixel(_root.redBox_mc._xmouse,_root.redBox_mc._ymouse).t
oString(16);
trace(col);

but what I get is 0x0. Am I doing something wrong? Thanks!
Ney Alencar
4/14/2006 3:17:17 PM
It works with the loop... Thanks You!, Now, if my box has more than one color
inside and I want to get a specific color, How do I get the pixel value
(color) from y,x location inside the box? Again, Thank for you time...

Best,

Ney
AddThis Social Bookmark Button