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

查看:17
本文介绍了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.

现在要计算颜色,我必须创建一个新位图",然后创建.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?

推荐答案

我有一个解决方案,但它可能不是最好的.基本上,我只是使用 as3swf 遍历 swf 的字节,然后查找形状定义然后进行填充.从那里我将颜色(实心填充和渐变中的颜色)推入一个数组.由于可能有重复,我最后将其删除.

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天全站免登陆