软油漆桶填充:颜色平等

时间:2010-03-31 11:12:11

标签: actionscript-3 actionscript graphics colors paint

我正在制作一个小应用程序,孩子们可以使用颜色填充预设插图。我已经使用泛洪填充算法成功实现了MS-paint样式的油漆桶。然而,在图像元素的边缘附近,像素未被填充,因为这些线是抗锯齿的。这是因为关于是否填充的当前条件是colourAtCurrentPixel == colourToReplace,这对线上的混合像素不起作用。 (颜色是RGB uint)

我想在Photoshop和其他复杂的工具中添加平滑/阈值选项,但是确定两种颜色之间的相等/距离的算法是什么?

if (match(pixel(x,y), colourToReplace) setpixel(x,y,colourToReplaceWith)

如何填写match()?

这里是一个图像(左边是情况,右边是想要的)

alt text http://www.freeimagehosting.net/uploads/6aa7b4ad53.png

这是我目前的完整代码:

            var b:BitmapData = settings.background;
            b.lock();

            var from:uint = b.getPixel(x,y);


            var q:Array = [];


            var xx:int;
            var yy:int;
            var w:int = b.width;
            var h:int = b.height;
            q.push(y*w + x);
            while (q.length != 0) {
                var xy:int = q.shift();
                xx = xy % w;
                yy = (xy - xx) / w;
                if (b.getPixel(xx,yy) == from) { //<- want to replace this line
                    b.setPixel(xx,yy,to);
                    if (xx != 0) q.push(xy-1);
                    if (xx != w-1) q.push(xy+1);
                    if (yy != 0) q.push(xy-w);
                    if (yy != h-1) q.push(xy+w);
                }
            }
            b.unlock(null);

2 个答案:

答案 0 :(得分:1)

嗯,我想最自然的方法是计算颜色之间的差异。为了达到合理的价值,人们应该计算每个渠道的差异。尚未测试过,但以下情况应该有效:

const perChanThreshold:uint = 5;
const overallThreshold:uint = perChanThreshold * perChanThreshold * 3;
function match(source:uint, target:uint):Boolean {
    var diff:uint = 0, chanDiff:uint;
    for (var i:int = 0; i < 3; i++) {
        chanDiff = (source >> (i * 8)) & 0xFF;
        diff += chanDiff * chanDiff;
    }
    return diff <= overallThreshold;
}

答案 1 :(得分:1)

制造有用的东西:

                c = b.getPixel(xx,yy);
                if (c == to) continue;
                if (c != from) d = 
                    Math.pow(f1 - (c & 0xFF), 2) +
                    Math.pow(f2 - (c >> 8 & 0xFF), 2) +
                    Math.pow(f3 - (c >> 16 & 0xFF), 2)
                if (c == from || d < tres) {