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
17package android.graphics;
18
19import android.annotation.ColorInt;
20import android.annotation.NonNull;
21
22/**
23 * A color filter that can be used to tint the source pixels using a single
24 * color and a specific {@link PorterDuff Porter-Duff composite mode}.
25 */
26public class PorterDuffColorFilter extends ColorFilter {
27    @ColorInt
28    private int mColor;
29    private PorterDuff.Mode mMode;
30
31    /**
32     * Create a color filter that uses the specified color and Porter-Duff mode.
33     *
34     * @param color The ARGB source color used with the specified Porter-Duff mode
35     * @param mode The porter-duff mode that is applied
36     *
37     * @see Color
38     * @see #setColor(int)
39     * @see #setMode(android.graphics.PorterDuff.Mode)
40     */
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     * @see #setColor(int)
52     *
53     * @hide
54     */
55    @ColorInt
56    public int getColor() {
57        return mColor;
58    }
59
60    /**
61     * Specifies the color to tint the source pixels with when this color
62     * filter is applied.
63     *
64     * @param color An ARGB {@link Color color}
65     *
66     * @see Color
67     * @see #getColor()
68     * @see #getMode()
69     *
70     * @hide
71     */
72    public void setColor(@ColorInt int color) {
73        if (mColor != color) {
74            mColor = color;
75            discardNativeInstance();
76        }
77    }
78
79    /**
80     * Returns the Porter-Duff mode used to composite this color filter's
81     * color with the source pixel when this filter is applied.
82     *
83     * @see PorterDuff
84     * @see #setMode(android.graphics.PorterDuff.Mode)
85     *
86     * @hide
87     */
88    public PorterDuff.Mode getMode() {
89        return mMode;
90    }
91
92    /**
93     * Specifies the Porter-Duff mode to use when compositing this color
94     * filter's color with the source pixel at draw time.
95     *
96     * @see PorterDuff
97     * @see #getMode()
98     * @see #getColor()
99     *
100     * @hide
101     */
102    public void setMode(@NonNull PorterDuff.Mode mode) {
103        if (mode == null) {
104            throw new IllegalArgumentException("mode must be non-null");
105        }
106        mMode = mode;
107        discardNativeInstance();
108    }
109
110    @Override
111    long createNativeInstance() {
112        return native_CreatePorterDuffFilter(mColor, mMode.nativeInt);
113    }
114
115    @Override
116    public boolean equals(Object object) {
117        if (this == object) {
118            return true;
119        }
120        if (object == null || getClass() != object.getClass()) {
121            return false;
122        }
123        final PorterDuffColorFilter other = (PorterDuffColorFilter) object;
124        return (mColor == other.mColor && mMode.nativeInt == other.mMode.nativeInt);
125    }
126
127    @Override
128    public int hashCode() {
129        return 31 *  mMode.hashCode() + mColor;
130    }
131
132    private static native long native_CreatePorterDuffFilter(int srcColor, int porterDuffMode);
133}
134