package net.thoj.celebratepride;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import com.squareup.picasso.Transformation;

public class CelebratePrideTransformation implements Transformation {
  private final Resources res;

  public CelebratePrideTransformation(Context context) {
    this.res = context.getResources();
  }

  @Override public Bitmap transform(Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, source.getConfig());
    Bitmap overlay = Bitmap.createBitmap(width, height, source.getConfig());

    int delta = source.getHeight() / 6;
    int color1 = res.getColor(R.color.celebrate_color_overlay1);
    int color2 = res.getColor(R.color.celebrate_color_overlay2);
    int color3 = res.getColor(R.color.celebrate_color_overlay3);
    int color4 = res.getColor(R.color.celebrate_color_overlay4);
    int color5 = res.getColor(R.color.celebrate_color_overlay5);
    int color6 = res.getColor(R.color.celebrate_color_overlay6);
    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        if (y / delta < 1) {
          overlay.setPixel(x, y, color1);
        } else if (y / delta < 2) {
          overlay.setPixel(x, y, color2);
        } else if (y / delta < 3) {
          overlay.setPixel(x, y, color3);
        } else if (y / delta < 4) {
          overlay.setPixel(x, y, color4);
        } else if (y / delta < 5) {
          overlay.setPixel(x, y, color5);
        } else {
          overlay.setPixel(x, y, color6);
        }
      }
    }

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(source, new Matrix(), null);
    canvas.drawBitmap(overlay, 0, 0, null);
    if (source != result) {
      source.recycle();
    }
    if (overlay != result) {
      overlay.recycle();
    }
    return result;
  }

  @Override public String key() {
    return "celebrate_pride";
  }
}

@@ -193,16 +193,46 @@
 Matrix;%0A
+import android.graphics.Rect;%0A
 import c
@@ -358,20 +358,19 @@
 nal 
-Resources re
+int%5B%5D color
 s;%0A%0A
@@ -434,383 +434,81 @@
     
-this.res = context.getResources();%0A  %7D%0A%0A  @Override public Bitmap transform(Bitmap source) %7B%0A    int width = source.getWidth();%0A    int height = source.getHeight();%0A    Bitmap result = Bitmap.createBitmap(width, height, source.getConfig()
+Resources res = context.getResources(
 );%0A
+%0A
     
-Bitmap overlay = Bitmap.createBitmap(width, height, source.getConfig());%0A%0A    int delta = source.getHeight() / 6;%0A    int color1 =
+colors = new int%5B%5D %7B%0A       
  res
@@ -554,26 +554,17 @@
 ay1)
-;
+,
 %0A    
-int color2 =
+   
  res
@@ -610,26 +610,17 @@
 ay2)
-;
+,
 %0A    
-int color3 =
+   
  res
@@ -666,26 +666,17 @@
 ay3)
-;
+,
 %0A    
-int color4 =
+   
  res
@@ -722,26 +722,17 @@
 ay4)
-;
+,
 %0A    
-int color5 =
+   
  res
@@ -778,26 +778,17 @@
 ay5)
-;
+,
 %0A    
-int color6 =
+   
  res
@@ -834,499 +834,447 @@
 ay6)
-;
 %0A    
-for (int y = 0; y %3C height; ++y) %7B%0A      for (int x = 0; x %3C width; ++x) %7B%0A        if (y / delta %3C 1) %7B%0A          overlay.setPixel(x, y, color1);%0A        %7D else if (y / delta %3C 2) %7B%0A          overlay.setPixel(x, y, color2);%0A        %7D else if (y / delta %3C 3) %7B%0A          overlay.setPixel(x, y, color3);%0A        %7D else if (y / delta %3C 4) %7B%0A          overlay.setPixel(x, y, color4
+%7D;%0A  %7D%0A%0A  @Override public Bitmap transform(Bitmap source) %7B%0A    int width = source.getWidth();%0A    int height = source.getHeight();%0A    Bitmap result = Bitmap.createBitmap(width, height, source.getConfig());%0A    Bitmap overlay = Bitmap.createBitmap(1, colors.length, source.getConfig()
 );%0A
+%0A
     
-    %7D else if (y / delta %3C 5) %7B%0A          overlay.setPixel(x, y, color5
+Canvas canvas = new Canvas(result);%0A    canvas.drawBitmap(source, new Matrix(), null
 );%0A
+%0A
     
-    %7D else %7B%0A   
+for (int i = 0; i %3C colors.length; ++i) %7B%0A
       
- 
 over
@@ -1290,174 +1290,147 @@
 xel(
-x, y
+0, i
 , color
-6);%0A        %7D%0A      %7D%0A    %7D%0A%0A    Canvas canvas = new Canvas(result);%0A    canvas.drawBitmap(source, new Matrix(), null);%0A    canvas.drawBitmap(overlay, 0, 0
+s%5Bi%5D);%0A    %7D%0A%0A    canvas.drawBitmap(overlay, new Rect(0, 0, 1, colors.length - 1),%0A        new Rect(0, 0, width - 1, height - 1)
 , nu
@@ -1434,16 +1434,17 @@
  null);%0A
+%0A
     if (

Improve performance of transforming picture