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 android.graphics.Bitmap; 19 import android.graphics.Canvas; 20 import android.graphics.Color; 21 import android.graphics.Paint; 22 import android.graphics.PorterDuff; 23 import android.graphics.PorterDuffColorFilter; 24 import android.graphics.Bitmap.Config; 25 import android.test.AndroidTestCase; 26 27 public class PorterDuffColorFilterTest extends AndroidTestCase { 28 29 private static final int TOLERANCE = 5; 30 31 public void testPorterDuffColorFilter() { 32 int width = 100; 33 int height = 100; 34 Bitmap b1 = Bitmap.createBitmap(width / 2, height, Config.ARGB_8888); 35 b1.eraseColor(Color.RED); 36 Bitmap b2 = Bitmap.createBitmap(width, height / 2, Config.ARGB_8888); 37 b2.eraseColor(Color.BLUE); 38 39 Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888); 40 target.eraseColor(Color.TRANSPARENT); 41 Canvas canvas = new Canvas(target); 42 // semi-transparent green 43 int filterColor = Color.argb(0x80, 0, 0xFF, 0); 44 PorterDuffColorFilter filter = new PorterDuffColorFilter(filterColor, PorterDuff.Mode.SRC); 45 Paint p = new Paint(); 46 canvas.drawBitmap(b1, 0, 0, p); 47 p.setColorFilter(filter); 48 canvas.drawBitmap(b2, 0, height / 2, p); 49 assertEquals(Color.RED, target.getPixel(width / 4, height / 4)); 50 int lowerLeft = target.getPixel(width / 4, height * 3 / 4); 51 assertEquals(0x80, Color.red(lowerLeft), TOLERANCE); 52 assertEquals(0x80, Color.green(lowerLeft), TOLERANCE); 53 int lowerRight = target.getPixel(width * 3 / 4, height * 3 / 4); 54 assertEquals(filterColor, lowerRight); 55 56 target.eraseColor(Color.BLACK); 57 filter = new PorterDuffColorFilter(filterColor, PorterDuff.Mode.DST); 58 p.setColorFilter(null); 59 canvas.drawBitmap(b1, 0, 0, p); 60 p.setColorFilter(filter); 61 canvas.drawBitmap(b2, 0, height / 2, p); 62 assertEquals(Color.RED, target.getPixel(width / 4, height / 4)); 63 assertEquals(Color.BLUE, target.getPixel(width / 4, height * 3 / 4)); 64 assertEquals(Color.BLUE, target.getPixel(width * 3 / 4, height * 3 / 4)); 65 66 target.eraseColor(Color.BLACK); 67 filter = new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.SCREEN); 68 p.setColorFilter(null); 69 canvas.drawBitmap(b1, 0, 0, p); 70 p.setColorFilter(filter); 71 canvas.drawBitmap(b2, 0, height / 2, p); 72 assertEquals(Color.RED, target.getPixel(width / 4, height / 4)); 73 assertEquals(Color.CYAN, target.getPixel(width / 4, height * 3 / 4)); 74 assertEquals(Color.CYAN, target.getPixel(width * 3 / 4, height * 3 / 4)); 75 } 76 } 77