# How to apply sepia effect on a bitmap

Excellence is not enough!

## How to apply sepia effect on a bitmap In this post i’m gonna show you how to apply sepia effect on your bitmap with just a simple method . Let’s dive into the code :

```public static Bitmap applySepiaEffect( Bitmap src , int depth , double red , double green , double blue ){
int width = src.getWidth();
int height = src.getHeight();

Bitmap  bitmapOut = Bitmap.createBitmap(width , height , src.getConfig()) ;

// constant grayscale
final double GS_RED = 0.6;
final double GS_GREEN = 0.2;
final double GS_BLUE = 0.2;

int A , R ,G ,B ;
int pixel ;

for (int x =0 ; x < width ; ++ x ){
for (int y = 0 ; y < height ; ++y ){
pixel = src.getPixel( x , y ) ;
A =Color.alpha(pixel) ;
R= Color.red(pixel) ;
G = Color.green(pixel)  ;
B = Color.blue(pixel) ;
B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);

R+= (depth * red ) ;
if (R > 255 ) R = 255 ;

G += (depth * green);
if(G > 255) { G = 255; }

B += (depth * blue);
if(B > 255) { B = 255; }

bitmapOut.setPixel(x , y , Color.argb(A , R ,G , B ));

}
}
return bitmapOut ;
}```

So let me discuss some theory : as you already knew each pixel in a bitmap has three color values , to apply sepia effect , we have to convert all colors to one of desired ( blue , green or red ) colors and determine color boldness on the color number itself . so let’s study the code :

After preparing the result bitmap , and define three constant values :

```final double GS_RED = 0.6;
final double GS_GREEN = 0.2;
final double GS_BLUE = 0.2;```

In fact this constant values define color weights in the pixel , for example i add 0.6f for the gs_red , 0.2 for gs_red and 0.2 for gs_blue witch means that red color in the pixel would have at least 3 times more power than others .

Then we travers through pixels and collect color values :

```pixel = src.getPixel( x , y ) ;
A =Color.alpha(pixel) ;
R= Color.red(pixel) ;
G = Color.green(pixel)  ;
B = Color.blue(pixel) ;```

Now it’s time to calculate colors based on our explanation :

`B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B)`

First of all we determine that all three color values would have an equal number : B = G = R .

Then we multiply each color value to it’s constant corresponding for example : GS_GREEN * G  and adds it to other calculated values .

Then if we have value greater than 255 (max pixel color value ) just trim it to 255 & add then multiply it with a debth value :

```R+= (depth * red ) ;
if (R > 255 ) R = 255 ;

G += (depth * green);
if(G > 255) { G = 255; }

B += (depth * blue);
if(B > 255) { B = 255; }```

That’s It Now You Can Make Any Picture Like Those Old Camera Negatives .