Flex - 如何计算.SWF矢量文件中的颜色 [英] Flex - How to count colors in .SWF vector file

查看:182
本文介绍了Flex - 如何计算.SWF矢量文件中的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已将矢量图片文件(.AI / .SVG)转换为.SWF,以便于动态导入为Flex中的图片来源。

Suppose I've converted a vector image file (.AI/.SVG) to .SWF for ease of importing dynamically as the source of an Image in Flex.

现在,为了计算颜色,我必须创建一个新的BitMap,然后.draw()和迭代所有像素,并使用.getPixel()检索颜色。

To count colors now, I have to create a "new BitMap", then ".draw()" and iterate over all pixels and use ".getPixel()" to retrieve the color.

不幸的是,由于抗锯齿,这似乎返回了比实际用来绘制图像更多的颜色(例如,一个黑色的标志)。

Unfortunately, because of anti-aliasing, this seems to return several more colors than what are actually used to draw the image (say, a black logo).

在Flex中有更好的替代方法吗?

Is there a better alternative to doing this in Flex?

推荐答案

我有一个解决方案,是最好的一个。
基本上,我只是循环通过swf的字节使用 as3swf ,并寻找形状定义,然后到达填充。从那里,我把颜色(实体填充和渐变中找到的颜色)推入一个数组。

I've got a solution, but it might not be the best one. Basically I'm just looping through the swf's bytes using as3swf and looking for shape definitions then getting to the fills. From there I push the colors(solid fills and colors found in gradients) into an array. Since there might be duplicates, I remove them at the end.

以下是代码:

import com.codeazur.as3swf.*;
import com.codeazur.as3swf.tags.*;

var colors:Array = [];
var colorsNum:int = 0;
var request:URLRequest = new URLRequest("plank.swf");
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);

function completeHandler(e:Event):void {
    var swf:SWF = new SWF(URLLoader(e.target).data as ByteArray);
    var tagsNum:int = swf.tags.length;
    for(var i:int = 0 ; i < tagsNum ; i++){
        if(swf.tags[i].name.substr(0,11) == 'DefineShape'){
            var fillStylesNum:int = TagDefineShape4(swf.tags[i]).shapes.fillStyles.length;
            if(fillStylesNum > 0){
                for(var j:int = 0 ; j < fillStylesNum ; j++){
                    if(TagDefineShape4(swf.tags[i]).shapes.fillStyles[j].gradient){
                        var recordsNum:int = TagDefineShape4(swf.tags[i]).shapes.fillStyles[j].gradient.records.length;
                        for(var k:int = 0 ; k < recordsNum; k++){}
                            colors.push(TagDefineShape4(swf.tags[i]).shapes.fillStyles[j].gradient.records[k].color)
                    }else{
                        colors.push(TagDefineShape4(swf.tags[i]).shapes.fillStyles[j].rgb);
                    }
                }
            }
        }
    }
    colors.sort(Array.NUMERIC);
    removeDuplicates(colors);
    trace('numColors: ' + colors.length + '\ncolors: ' + colors);
}
function removeDuplicates(array:Array):Array{
    var length:int = array.length;
    var i:int, j:int;
    var newLength:int = 1;

    for(i=1; i< length; i++){

       for(j=0; j< newLength ; j++)
       {

          if(array[i] == array[j])
          break;
       }
        if (j==newLength )
          array[newLength++] = array[i];
    }
    array.length = newLength;
    return array;
}

HTH

这篇关于Flex - 如何计算.SWF矢量文件中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆