Lubch
Lubch

Reputation: 57

Algorithm for smart floodfill in raster image

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.

Upvotes: 3

Views: 781

Answers (1)

Lubch
Lubch

Reputation: 57

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.

Upvotes: 1

Related Questions