软油漆桶填充:颜色平等 [英] Soft Paint Bucket Fill: Colour Equality
问题描述
我在做一个小的应用程序,孩子们可以填补preSET插图颜色。我已经成功地实现用颜色填充argorithm一个MS-烤漆风格的油漆桶。然而,邻近的图像元素的像素的边缘悬空的,因为行是反走样。这是因为不论是否为填补目前的状况是 colourAtCurrentPixel == colourToReplace
,它不会在混合像素的线工作。
(颜色是RGB uints)
我想添加一个平滑/ treshold选项像Photoshop等复杂的工具,但什么是算法来确定两种颜色之间的平等/距离?
如果(匹配(像素(X,Y),colourToReplace)与setPixel(X,Y,colourToReplaceWith)
如何填写匹配
()?
下面,图像(左为的情况下,正确的是希望)
下面是我目前的全code:
变种B:的BitmapData = settings.background;
块();
从变种:UINT = b.getPixel(X,Y);
VAR问:数组= [];
VAR XX:INT;
VAR YY:INT;
变种W:INT = b.width;
VAR H:INT = b.height;
q.push(Y * W + X);
而(q.length!= 0){
VAR XY:INT = q.shift();
XX = XY%(重量);
YY =(XY - XX)/ W;
如果(b.getPixel(XX,YY)==从){//< - 要替换该行
b.setPixel(XX,YY,到);
如果(ⅹⅹ!= 0)q.push(XY-1);
如果(!XX =瓦特-1)q.push(XY + 1);
如果(YY!= 0)q.push(XY-w)的;
如果(YY = H-1!)q.push(XY + W);
}
}
b.unlock(空);
唉,我想最自然的方法是计算之间颜色的差异。实现一个合理的值,应该计算每个信道的区别。没有测试过,但下面应该工作:
常量perChanThreshold:UINT = 5;
常量overallThreshold:UINT = perChanThreshold * perChanThreshold * 3;
功能匹配(来源:UINT,目标:UINT):布尔{
VAR差异:UINT = 0,chanDiff:单元;
对于(VAR我:= 0;我3;;我++){
chanDiff =(来源>>(I * 8))及0xFF的;
差异+ = chanDiff * chanDiff;
}
返回DIFF< = overallThreshold;
}
I'm making a small app where children can fill preset illustrations with colours. I've succesfully implemented an MS-paint style paint bucket using the flood fill argorithm. However, near the edges of image elements pixels are left unfilled, because the lines are anti-aliased. This is because the current condition on whether to fill is colourAtCurrentPixel == colourToReplace
, which doesn't work on the blended pixels at the lines.
(the colours are RGB uints)
I'd like to add a smoothing/treshold option like in Photoshop and other sophisticated tools, but what's the algorithm to determine the equality/distance between two colours?
if (match(pixel(x,y), colourToReplace) setpixel(x,y,colourToReplaceWith)
How to fill in match
()?
Here, an image (left is situation, right is wanted)
Here's my current full code:
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);
well, i guess the most natural approach is to calculate the difference between to colors. to achieve a sensible value, one should calculate the difference per channel. haven't tested it, but the following should work:
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;
}
这篇关于软油漆桶填充:颜色平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!