1 /* 2 * Copyright (C) 2009 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.assertArrayEquals; 19 import static org.junit.Assert.assertEquals; 20 21 import android.graphics.Bitmap; 22 import android.graphics.Bitmap.Config; 23 import android.graphics.Canvas; 24 import android.graphics.Color; 25 import android.graphics.ColorMatrix; 26 import android.graphics.ColorMatrixColorFilter; 27 import android.graphics.Paint; 28 29 import androidx.test.filters.SmallTest; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import com.android.compatibility.common.util.ColorUtils; 33 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 public class ColorMatrixColorFilterTest { 40 41 @Test testColorMatrixColorFilter()42 public void testColorMatrixColorFilter() { 43 ColorMatrixColorFilter filter; 44 45 ColorMatrix cm = new ColorMatrix(); 46 float[] blueToCyan = new float[] { 47 1f, 0f, 0f, 0f, 0f, 48 0f, 1f, 1f, 0f, 0f, 49 0f, 0f, 1f, 0f, 0f, 50 0f, 0f, 0f, 1f, 0f }; 51 cm.set(blueToCyan); 52 filter = new ColorMatrixColorFilter(cm); 53 Bitmap bitmap = Bitmap.createBitmap(1, 1, Config.ARGB_8888); 54 Canvas canvas = new Canvas(bitmap); 55 Paint paint = new Paint(); 56 paint.setColor(Color.BLUE); 57 paint.setColorFilter(filter); 58 paint.setAntiAlias(false); 59 canvas.drawPoint(0, 0, paint); 60 ColorUtils.verifyColor(Color.CYAN, bitmap.getPixel(0, 0)); 61 paint.setColor(Color.GREEN); 62 canvas.drawPoint(0, 0, paint); 63 ColorUtils.verifyColor(Color.GREEN, bitmap.getPixel(0, 0)); 64 paint.setColor(Color.RED); 65 canvas.drawPoint(0, 0, paint); 66 ColorUtils.verifyColor(Color.RED, bitmap.getPixel(0, 0)); 67 // color components are clipped, not scaled 68 paint.setColor(Color.MAGENTA); 69 canvas.drawPoint(0, 0, paint); 70 ColorUtils.verifyColor(Color.WHITE, bitmap.getPixel(0, 0)); 71 72 float[] transparentRedAddBlue = new float[] { 73 1f, 0f, 0f, 0f, 0f, 74 0f, 1f, 0f, 0f, 0f, 75 0f, 0f, 1f, 0f, 64f, 76 -0.5f, 0f, 0f, 1f, 0f 77 }; 78 filter = new ColorMatrixColorFilter(transparentRedAddBlue); 79 paint.setColorFilter(filter); 80 paint.setColor(Color.RED); 81 bitmap.eraseColor(Color.TRANSPARENT); 82 canvas.drawPoint(0, 0, paint); 83 // the bitmap stores the result in premul colors and we read out an 84 // unpremultiplied result, which causes us to need a bigger tolerance in 85 // this case (due to the fact that scaling by 1/255 is not exact). 86 ColorUtils.verifyColor(Color.argb(128, 255, 0, 64), bitmap.getPixel(0, 0), 2); 87 paint.setColor(Color.CYAN); 88 canvas.drawPoint(0, 0, paint); 89 // blue gets clipped 90 ColorUtils.verifyColor(Color.CYAN, bitmap.getPixel(0, 0)); 91 92 // change array to filter out green 93 assertEquals(1f, transparentRedAddBlue[6], 0.0f); 94 transparentRedAddBlue[6] = 0f; 95 // changing the array has no effect 96 canvas.drawPoint(0, 0, paint); 97 ColorUtils.verifyColor(Color.CYAN, bitmap.getPixel(0, 0)); 98 // create a new filter with the changed matrix 99 paint.setColorFilter(new ColorMatrixColorFilter(transparentRedAddBlue)); 100 canvas.drawPoint(0, 0, paint); 101 ColorUtils.verifyColor(Color.BLUE, bitmap.getPixel(0, 0)); 102 } 103 104 @Test testGetColorMatrix()105 public void testGetColorMatrix() { 106 ColorMatrixColorFilter filter = new ColorMatrixColorFilter(new ColorMatrix()); 107 ColorMatrix getMatrix = new ColorMatrix(); 108 109 filter.getColorMatrix(getMatrix); 110 assertEquals(new ColorMatrix(), getMatrix); 111 112 ColorMatrix scaleTranslate = new ColorMatrix(new float[] { 113 1, 0, 0, 0, 8, 114 0, 2, 0, 0, 7, 115 0, 0, 3, 0, 6, 116 0, 0, 0, 4, 5 117 }); 118 119 filter = new ColorMatrixColorFilter(scaleTranslate); 120 filter.getColorMatrix(getMatrix); 121 assertEquals(scaleTranslate, getMatrix); 122 assertArrayEquals(scaleTranslate.getArray(), getMatrix.getArray(), 0); 123 } 124 } 125 126