I am working on a silverlight application where all my icons are PNG.
The color of all these icons is black, or rather black to gray, depending on the alpha value. Each PNG has a transparent background.
Inside my application, I want to make the color change in pixels due to black or gray, let's say red (to black) or light red (to gray).
Walking through the bytes of each pixel, I found out that every full black pixel has an alpha value of 255. Those with gray have an alpha value from 1 to 254. An alpha value of 0 seems to be transparent. RGB values ββare 0 for black.
My idea was that I change the color of each pixel, but keep the alpha value so that PNG retains its original appearance.
To this end, I wrote a small function that looks like this:
private static WriteableBitmap ColorChange(WriteableBitmap wbmi, System.Windows.Media.Color color) { for (int pixel = 0; pixel < wbmi.Pixels.Length; pixel++) { if (wbmi.Pixels[pixel] != 0) { byte[] colorArray = BitConverter.GetBytes(wbmi.Pixels[pixel]); byte blue = colorArray[0]; byte green = colorArray[1]; byte red = colorArray[2]; byte alpha = colorArray[3]; if (alpha > 0) {
And a little helper class to change the color, but save alpha:
namespace Gui.Colors { public class ToInt { public static int Get(System.Windows.Media.Color color, Byte alpha) { color.A = alpha; return color.A << 24 | color.R << 16 | color.G << 8 | color.B; } } }
My problem is that I can clearly see that the problem with pixels with alpha is less than 255 (gray). Although the alpha value is preserved and the color changes, the pixels look different, they are no longer transparent. This makes the edges look scraggy.
Long conversation, short question: Looking at my code - what is the problem? How can I achieve pixel to pixel conversion with transparency of the original PNG?
Sorry for my english. I am not a native speaker of English.