Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the tint of a bitmap while preserving the overall brightness

Tags:

c#

.net

bitmap

rgb

I'm trying to write a function that will let me red-shift or blue-shift a bitmap while preserving the overall brightness of the image. Basically, a fully red-shifted bitmap would have the same brightness as the original but be thoroughly red-tinted (i.e. the G and B values would be equal for all pixels). Same for blue-tinting (but with R and G equal). The degree of spectrum shifting needs to vary from 0 to 1.

Thanks in advance.

like image 480
MusiGenesis Avatar asked Feb 27 '10 15:02

MusiGenesis


2 Answers

Here is the effect I was looking for (crappy JPEG, sorry):

alt text http://www.freeimagehosting.net/uploads/d15ff241ca.jpg

The image in the middle is the original, and the side images are fully red-shifted, partially red-shifted, partially blue-shifted and fully blue-shifted, respectively.

And here is the function that produces this effect:

public void RedBlueShift(Bitmap bmp, double factor)
{
    byte R = 0;
    byte G = 0;
    byte B = 0;
    byte Rmax = 0;
    byte Gmax = 0;
    byte Bmax = 0;
    double avg = 0;
    double normal = 0;
    if (factor > 1)
    {
        factor = 1;
    }
    else if (factor < -1)
    {
        factor = -1;
    }
    for (int x = 0; x < bmp.Width; x++)
    {
        for (int y = 0; y < bmp.Height; y++)
        {
            Color color = bmp.GetPixel(x, y);
            R = color.R;
            G = color.G;
            B = color.B;
            avg = (double)(R + G + B) / 3;
            normal = avg / 255.0; // to preserve overall intensity
            if (factor < 0) // red-tinted:
            {
                if (normal < .5)
                {
                    Rmax = (byte)((normal / .5) * 255);
                    Gmax = 0;
                    Bmax = 0;
                }
                else
                {
                    Rmax = 255;
                    Gmax = (byte)(((normal - .5) * 2) * 255);
                    Bmax = Gmax;
                }
                R = (byte)((double)R - ((double)(R - Rmax) * -factor));
                G = (byte)((double)G - ((double)(G - Gmax) * -factor));
                B = (byte)((double)B - ((double)(B - Bmax) * -factor));
            }
            else if (factor > 0) // blue-tinted:
            {
                if (normal < .5)
                {
                    Rmax = 0;
                    Gmax = 0;
                    Bmax = (byte)((normal / .5) * 255);
                }
                else
                {
                    Rmax = (byte)(((normal - .5) * 2) * 255);
                    Gmax = Rmax;
                    Bmax = 255;
                }
                R = (byte)((double)R - ((double)(R - Rmax) * factor));
                G = (byte)((double)G - ((double)(G - Gmax) * factor));
                B = (byte)((double)B - ((double)(B - Bmax) * factor));
            }
            color = Color.FromArgb(R, G, B);
            bmp.SetPixel(x, y, color);
        }
    }
}
like image 153
MusiGenesis Avatar answered Sep 21 '22 14:09

MusiGenesis


You'd use the ColorMatrix class for this. There's a good tutorial available in this project.

like image 35
Hans Passant Avatar answered Sep 21 '22 14:09

Hans Passant