I'm looking for algorithm that is able to do something like this:
The algorithm should change the color of an image with a transparent background. But it should not only set the color like bitmap.SetPixel(x, y, newColor)
. instead it should respect the shadows of the foreground.
Currently I use the following algorithm:
public static Image Foreground(this Image xImage, Color xColor)
{
//FOREGROUND
if (xImage == null) return null;
Bitmap bitmap = new Bitmap(xImage);
//LOOP PIXELS
for (int x = 0; x < bitmap.Width; x++)
for (int y = 0; y < bitmap.Height; y++)
if (bitmap.GetPixel(x, y).A != 0)
{
Color color = bitmap.GetPixel(x, y);
int a = color.A;
int r = Math.Abs(xColor.R - color.R);
int g = Math.Abs(xColor.G - color.G);
int b = Math.Abs(xColor.B - color.B);
bitmap.SetPixel(x, y, Color.FromArgb(a, r, g, b));
}
return bitmap;
}
It works well if you repaint from black to white. But if you repaint from white to orange it dosn't work well.
Is there any better solution?