1 /*
2  * Copyright (C) 2016 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.view.cts.surfacevalidator;
17 
18 import android.annotation.ColorInt;
19 import android.graphics.Color;
20 
21 public class PixelColor {
22     public static final int TRANSLUCENT_RED = 0x7FFF0000;
23 
24     // Default to black
25     public short mMinAlpha;
26     public short mMaxAlpha;
27     public short mMinRed;
28     public short mMaxRed;
29     public short mMinBlue;
30     public short mMaxBlue;
31     public short mMinGreen;
32     public short mMaxGreen;
33 
34     public short mAlpha;
35     public short mRed;
36     public short mGreen;
37     public short mBlue;
38 
PixelColor(@olorInt int color)39     public PixelColor(@ColorInt int color) {
40         mAlpha = (short) ((color >> 24) & 0xFF);
41         mRed = (short) ((color >> 16) & 0xFF);
42         mGreen = (short) ((color >> 8) & 0xFF);
43         mBlue = (short) (color & 0xFF);
44 
45         mMinAlpha = (short) getMinValue(mAlpha);
46         mMaxAlpha = (short) getMaxValue(mAlpha);
47         mMinRed = (short) getMinValue(mRed);
48         mMaxRed = (short) getMaxValue(mRed);
49         mMinBlue = (short) getMinValue(mBlue);
50         mMaxBlue = (short) getMaxValue(mBlue);
51         mMinGreen = (short) getMinValue(mGreen);
52         mMaxGreen = (short) getMaxValue(mGreen);
53     }
54 
PixelColor()55     public PixelColor() {
56         this(Color.BLACK);
57     }
58 
getMinValue(short color)59     private int getMinValue(short color) {
60         return Math.max(color - 4, 0);
61     }
62 
getMaxValue(short color)63     private int getMaxValue(short color) {
64         return Math.min(color + 4, 0xFF);
65     }
66 
matchesColor(int color)67     public boolean matchesColor(int color) {
68         final float red = Color.red(color);
69         final float green = Color.green(color);
70         final float blue = Color.blue(color);
71         final float alpha = Color.alpha(color);
72 
73         return alpha <= mMaxAlpha
74                 && alpha >= mMinAlpha
75                 && red <= mMaxRed
76                 && red >= mMinRed
77                 && green <= mMaxGreen
78                 && green >= mMinGreen
79                 && blue <= mMaxBlue
80                 && blue >= mMinBlue;
81     }
82 
83 }
84