AS3:如何将图像用作颜色?

时间:2021-01-31 07:10:20

Under the "Fill and Stroke" option in Flash, you can color a shape as one of the textures from your library. Example: http://i1.minus.com/iban9aRUCI7UTf.png

在Flash中的“填充和描边”选项下,您可以将形状着色为库中的纹理之一。示例:http://i1.minus.com/iban9aRUCI7UTf.png

How can I do this with AS3 code?

如何使用AS3代码执行此操作?

2 个解决方案

#1


1  

If you're drawing with the Graphics API, you can use beginBitmapFill.

如果您使用Graphics API进行绘制,则可以使用beginBitmapFill。

Open the properties of an image in your library and check "Export for ActionScript" on the ActionScript tab. Give it a Class name such as "MyTexture", and then you can use it like this:

打开库中图像的属性,然后在“ActionScript”选项卡上选中“Export for ActionScript”。给它一个类名称,如“MyTexture”,然后你可以像这样使用它:

var texturedCircle:Sprite = new Sprite();
texturedCircle.graphics.beginBitmapFill(new MyTexture());
texturedCircle.graphics.drawCircle(0, 0, 100);

#2


0  

You can approach this in two steps.

您可以分两步完成此操作。

  1. Calculate the average color from bitmap values. The function below (written by Soulwire) does this "by looping through the pixels in a BitmapData object, adding up all of the red, green and blue values, dividing them by the total number of pixels and then creating a new colour from the results."
  2. 从位图值计算平均颜色。下面的函数(由Soulwire编写)执行此操作“通过循环BitmapData对象中的像素,将所有红色,绿色和蓝色值相加,将它们除以总像素数,然后从结果中创建新颜色“。

function averageColour(source:BitmapData):uint {
    var red:Number = 0;
    var green:Number = 0;
    var blue:Number = 0;
    var count:Number = 0;
    var pixel:Number;

    for (var x:int = 0; x < source.width; x++) {
        for (var y:int = 0; y < source.height; y++) {
            pixel = source.getPixel(x, y);

            red += pixel >> 16 & 0xFF;
            green += pixel >> 8 & 0xFF;
            blue += pixel & 0xFF;

            count++
        }
    }

    red /= count;
    green /= count;
    blue /= count;

    return red << 16 | green << 8 | blue;
}

  1. Use ColorTransform to apply it.
  2. 使用ColorTransform应用它。

function setColor(obj:Object, color:uint, alpha:Number = 1):void {
    /* Colors the object using uint */

    // Pull the individual primaries
    var r:Number = (color >> 16 ) & 0xFF;
    var g:Number = (color >> 8) & 0xFF;
    var b:Number = color & 0xFF;

    obj.transform.colorTransform = new ColorTransform(0,0,0,alpha,r,g,b,0);
}

#1


1  

If you're drawing with the Graphics API, you can use beginBitmapFill.

如果您使用Graphics API进行绘制,则可以使用beginBitmapFill。

Open the properties of an image in your library and check "Export for ActionScript" on the ActionScript tab. Give it a Class name such as "MyTexture", and then you can use it like this:

打开库中图像的属性,然后在“ActionScript”选项卡上选中“Export for ActionScript”。给它一个类名称,如“MyTexture”,然后你可以像这样使用它:

var texturedCircle:Sprite = new Sprite();
texturedCircle.graphics.beginBitmapFill(new MyTexture());
texturedCircle.graphics.drawCircle(0, 0, 100);

#2


0  

You can approach this in two steps.

您可以分两步完成此操作。

  1. Calculate the average color from bitmap values. The function below (written by Soulwire) does this "by looping through the pixels in a BitmapData object, adding up all of the red, green and blue values, dividing them by the total number of pixels and then creating a new colour from the results."
  2. 从位图值计算平均颜色。下面的函数(由Soulwire编写)执行此操作“通过循环BitmapData对象中的像素,将所有红色,绿色和蓝色值相加,将它们除以总像素数,然后从结果中创建新颜色“。

function averageColour(source:BitmapData):uint {
    var red:Number = 0;
    var green:Number = 0;
    var blue:Number = 0;
    var count:Number = 0;
    var pixel:Number;

    for (var x:int = 0; x < source.width; x++) {
        for (var y:int = 0; y < source.height; y++) {
            pixel = source.getPixel(x, y);

            red += pixel >> 16 & 0xFF;
            green += pixel >> 8 & 0xFF;
            blue += pixel & 0xFF;

            count++
        }
    }

    red /= count;
    green /= count;
    blue /= count;

    return red << 16 | green << 8 | blue;
}

  1. Use ColorTransform to apply it.
  2. 使用ColorTransform应用它。

function setColor(obj:Object, color:uint, alpha:Number = 1):void {
    /* Colors the object using uint */

    // Pull the individual primaries
    var r:Number = (color >> 16 ) & 0xFF;
    var g:Number = (color >> 8) & 0xFF;
    var b:Number = color & 0xFF;

    obj.transform.colorTransform = new ColorTransform(0,0,0,alpha,r,g,b,0);
}