1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.graphics.cts; 17 18 import static org.junit.Assert.assertEquals; 19 20 import android.graphics.Bitmap; 21 import android.graphics.Bitmap.Config; 22 import android.graphics.Canvas; 23 import android.graphics.Color; 24 import android.graphics.Paint; 25 import android.graphics.PorterDuff; 26 import android.graphics.PorterDuffColorFilter; 27 import android.support.test.filters.SmallTest; 28 import android.support.test.runner.AndroidJUnit4; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 @SmallTest 34 @RunWith(AndroidJUnit4.class) 35 public class PorterDuffColorFilterTest { 36 private static final int TOLERANCE = 5; 37 38 @Test 39 public void testPorterDuffColorFilter() { 40 int width = 100; 41 int height = 100; 42 Bitmap b1 = Bitmap.createBitmap(width / 2, height, Config.ARGB_8888); 43 b1.eraseColor(Color.RED); 44 Bitmap b2 = Bitmap.createBitmap(width, height / 2, Config.ARGB_8888); 45 b2.eraseColor(Color.BLUE); 46 47 Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888); 48 target.eraseColor(Color.TRANSPARENT); 49 Canvas canvas = new Canvas(target); 50 // semi-transparent green 51 int filterColor = Color.argb(0x80, 0, 0xFF, 0); 52 PorterDuffColorFilter filter = new PorterDuffColorFilter(filterColor, PorterDuff.Mode.SRC); 53 Paint p = new Paint(); 54 canvas.drawBitmap(b1, 0, 0, p); 55 p.setColorFilter(filter); 56 canvas.drawBitmap(b2, 0, height / 2, p); 57 assertEquals(Color.RED, target.getPixel(width / 4, height / 4)); 58 int lowerLeft = target.getPixel(width / 4, height * 3 / 4); 59 assertEquals(0x80, Color.red(lowerLeft), TOLERANCE); 60 assertEquals(0x80, Color.green(lowerLeft), TOLERANCE); 61 int lowerRight = target.getPixel(width * 3 / 4, height * 3 / 4); 62 assertEquals(filterColor, lowerRight); 63 64 target.eraseColor(Color.BLACK); 65 filter = new PorterDuffColorFilter(filterColor, PorterDuff.Mode.DST); 66 p.setColorFilter(null); 67 canvas.drawBitmap(b1, 0, 0, p); 68 p.setColorFilter(filter); 69 canvas.drawBitmap(b2, 0, height / 2, p); 70 assertEquals(Color.RED, target.getPixel(width / 4, height / 4)); 71 assertEquals(Color.BLUE, target.getPixel(width / 4, height * 3 / 4)); 72 assertEquals(Color.BLUE, target.getPixel(width * 3 / 4, height * 3 / 4)); 73 74 target.eraseColor(Color.BLACK); 75 filter = new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.SCREEN); 76 p.setColorFilter(null); 77 canvas.drawBitmap(b1, 0, 0, p); 78 p.setColorFilter(filter); 79 canvas.drawBitmap(b2, 0, height / 2, p); 80 assertEquals(Color.RED, target.getPixel(width / 4, height / 4)); 81 assertEquals(Color.CYAN, target.getPixel(width / 4, height * 3 / 4)); 82 assertEquals(Color.CYAN, target.getPixel(width * 3 / 4, height * 3 / 4)); 83 } 84 } 85