2
votes

I'm trying to implement flood fill method for raster image. For center pixels it's easy and works correct, but the problem is to fill pixels near border, which have different color.

For example, if draw Black figure on White background, some border pixels will have kind of gray color instead of black (for smoothing).

Image editors (like paint.net) during floodfill fixes it changing these pixels to some middle color between old and new one. Here I filled figure in red color, and gray pixels became in red gradient

img

I need to know method or algorithm how gray pixels became in kind of color to fill (here it's red, but can be any) using RGB pixel manipulation.

Thanks for any help.

1
you can fill color based on pixel color ci, start pixel color c0 and filling color c1 for example like this: ci = c1*|ci|/|c0| unless you are dealing with black colors.Do not forget to handle saturations (normalize). You can also use difference: |ci-c0|/maxdif where maxdif is threshold for filling .... - Spektre
#Lubch: what is your attempt? - gpasch
@Spektre Thanks for your answer but I afraid I didn't catch you :-( If I use it for 231;231;231 pixel I do not get 247;72;72. Maybe I do not use your formula correct (ci = c1*|ci|/|c0|) - Lubch
@gpasch I'm trying to fill left figure like on right side, changing not only white pixels, but light gray ones too. So I need algorithm how f.e. 231;231;231 by filling of 255;0;0 became to 247;72;72 - Lubch
@Lubch to derive the underlaying equations for specific implementation you want to mimic You need to create R,G,B,Gray gradients and fill them with control colors to see how the colors are combined exactly ... also is there alpha channel present? - Spektre

1 Answers

0
votes

So, for similar effect like in example we just need to use & operation between old and new color.

For RGB color:

resultColor.R = (byte)(oldColor.R & newColor.R);
resultColor.G = (byte)(oldColor.G & newColor.G);
resultColor.B = (byte)(oldColor.B & newColor.B);

If RGB color is Int number:

resultColor = oldColor & newColor;

It will not be exactly same color as in example below but pretty similar.