I have a program that reads the grayscale (0 - 255) value of a pixel and changes it to an RGB color (formula see code).
Here is my code:
Bitmap img = new Bitmap(@"somepath");
Bitmap new_img = new Bitmap(img.Width, img.Height);
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
Color pixel = img.GetPixel(i, j);
Color my = new Color();
int R_new = 0;
if (pixel.R > 126)
{
R_new = (pixel.R -127) / 128 * 255;
}
int B_new = 0;
if (pixel.B < 128) {
B_new = (1 - pixel.R / 127) * 255;
}
int G_new = 0;
if (pixel.G < 128) {
G_new = pixel.R/127 * 255;
}
my = Color.FromArgb(R_new, G_new, B_new);
new_img.SetPixel(i, j, my);
}
}
new_img.Save(@"C:somepath");
In the following picture
you can see the green-value of the old pixel is 6 and of the new pixel it is set with 255, which is wrong. According to the formula it should be set at 12.
Here is my question: Why is the value of the pixel set wrong?