1 /*
2  * Copyright (C) 2006 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 
17 package android.graphics;
18 
19 import android.annotation.ColorInt;
20 import android.annotation.NonNull;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.os.Build;
23 
24 /**
25  * A color filter that can be used to tint the source pixels using a single
26  * color and a specific {@link PorterDuff Porter-Duff composite mode}.
27  */
28 public class PorterDuffColorFilter extends ColorFilter {
29     @ColorInt
30     private int mColor;
31     private PorterDuff.Mode mMode;
32 
33     /**
34      * Create a color filter that uses the specified color and Porter-Duff mode.
35      *
36      * @param color The ARGB source color used with the specified Porter-Duff mode
37      * @param mode The porter-duff mode that is applied
38      *
39      * @see Color
40      */
PorterDuffColorFilter(@olorInt int color, @NonNull PorterDuff.Mode mode)41     public PorterDuffColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode) {
42         mColor = color;
43         mMode = mode;
44     }
45 
46     /**
47      * Returns the ARGB color used to tint the source pixels when this filter
48      * is applied.
49      *
50      * @see Color
51      *
52      * @hide
53      */
54     @ColorInt
55     @UnsupportedAppUsage
getColor()56     public int getColor() {
57         return mColor;
58     }
59 
60     /**
61      * Returns the Porter-Duff mode used to composite this color filter's
62      * color with the source pixel when this filter is applied.
63      *
64      * @see PorterDuff
65      *
66      * @hide
67      */
68     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
getMode()69     public PorterDuff.Mode getMode() {
70         return mMode;
71     }
72 
73     @Override
createNativeInstance()74     long createNativeInstance() {
75         return native_CreateBlendModeFilter(mColor, mMode.nativeInt);
76     }
77 
78     @Override
equals(Object object)79     public boolean equals(Object object) {
80         if (this == object) {
81             return true;
82         }
83         if (object == null || getClass() != object.getClass()) {
84             return false;
85         }
86         final PorterDuffColorFilter other = (PorterDuffColorFilter) object;
87         return (mColor == other.mColor && mMode.nativeInt == other.mMode.nativeInt);
88     }
89 
90     @Override
hashCode()91     public int hashCode() {
92         return 31 *  mMode.hashCode() + mColor;
93     }
94 
native_CreateBlendModeFilter(int srcColor, int blendmode)95     private static native long native_CreateBlendModeFilter(int srcColor, int blendmode);
96 }
97