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 canvas.drawPoint(0, 0, paint); 59 ColorUtils.verifyColor(Color.CYAN, bitmap.getPixel(0, 0)); 60 paint.setColor(Color.GREEN); 61 canvas.drawPoint(0, 0, paint); 62 ColorUtils.verifyColor(Color.GREEN, bitmap.getPixel(0, 0)); 63 paint.setColor(Color.RED); 64 canvas.drawPoint(0, 0, paint); 65 ColorUtils.verifyColor(Color.RED, bitmap.getPixel(0, 0)); 66 // color components are clipped, not scaled 67 paint.setColor(Color.MAGENTA); 68 canvas.drawPoint(0, 0, paint); 69 ColorUtils.verifyColor(Color.WHITE, bitmap.getPixel(0, 0)); 70 71 float[] transparentRedAddBlue = new float[] { 72 1f, 0f, 0f, 0f, 0f, 73 0f, 1f, 0f, 0f, 0f, 74 0f, 0f, 1f, 0f, 64f, 75 -0.5f, 0f, 0f, 1f, 0f 76 }; 77 filter = new ColorMatrixColorFilter(transparentRedAddBlue); 78 paint.setColorFilter(filter); 79 paint.setColor(Color.RED); 80 bitmap.eraseColor(Color.TRANSPARENT); 81 canvas.drawPoint(0, 0, paint); 82 // the bitmap stores the result in premul colors and we read out an 83 // unpremultiplied result, which causes us to need a bigger tolerance in 84 // this case (due to the fact that scaling by 1/255 is not exact). 85 ColorUtils.verifyColor(Color.argb(128, 255, 0, 64), bitmap.getPixel(0, 0), 2); 86 paint.setColor(Color.CYAN); 87 canvas.drawPoint(0, 0, paint); 88 // blue gets clipped 89 ColorUtils.verifyColor(Color.CYAN, bitmap.getPixel(0, 0)); 90 91 // change array to filter out green 92 assertEquals(1f, transparentRedAddBlue[6], 0.0f); 93 transparentRedAddBlue[6] = 0f; 94 // changing the array has no effect 95 canvas.drawPoint(0, 0, paint); 96 ColorUtils.verifyColor(Color.CYAN, bitmap.getPixel(0, 0)); 97 // create a new filter with the changed matrix 98 paint.setColorFilter(new ColorMatrixColorFilter(transparentRedAddBlue)); 99 canvas.drawPoint(0, 0, paint); 100 ColorUtils.verifyColor(Color.BLUE, bitmap.getPixel(0, 0)); 101 } 102 103 @Test testGetColorMatrix()104 public void testGetColorMatrix() { 105 ColorMatrixColorFilter filter = new ColorMatrixColorFilter(new ColorMatrix()); 106 ColorMatrix getMatrix = new ColorMatrix(); 107 108 filter.getColorMatrix(getMatrix); 109 assertEquals(new ColorMatrix(), getMatrix); 110 111 ColorMatrix scaleTranslate = new ColorMatrix(new float[] { 112 1, 0, 0, 0, 8, 113 0, 2, 0, 0, 7, 114 0, 0, 3, 0, 6, 115 0, 0, 0, 4, 5 116 }); 117 118 filter = new ColorMatrixColorFilter(scaleTranslate); 119 filter.getColorMatrix(getMatrix); 120 assertEquals(scaleTranslate, getMatrix); 121 assertArrayEquals(scaleTranslate.getArray(), getMatrix.getArray(), 0); 122 } 123 } 124 125