19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.graphics;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1913656743cc21bac43676568314366497346713eeRomain Guy/**
2013656743cc21bac43676568314366497346713eeRomain Guy * A color filter that can be used to tint the source pixels using a single
2113656743cc21bac43676568314366497346713eeRomain Guy * color and a specific {@link PorterDuff Porter-Duff composite mode}.
2213656743cc21bac43676568314366497346713eeRomain Guy */
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class PorterDuffColorFilter extends ColorFilter {
2413656743cc21bac43676568314366497346713eeRomain Guy    private int mColor;
2513656743cc21bac43676568314366497346713eeRomain Guy    private PorterDuff.Mode mMode;
2613656743cc21bac43676568314366497346713eeRomain Guy
2713656743cc21bac43676568314366497346713eeRomain Guy    /**
2813656743cc21bac43676568314366497346713eeRomain Guy     * Create a color filter that uses the specified color and Porter-Duff mode.
2913656743cc21bac43676568314366497346713eeRomain Guy     *
3013656743cc21bac43676568314366497346713eeRomain Guy     * @param color The ARGB source color used with the specified Porter-Duff mode
3113656743cc21bac43676568314366497346713eeRomain Guy     * @param mode The porter-duff mode that is applied
3213656743cc21bac43676568314366497346713eeRomain Guy     *
3313656743cc21bac43676568314366497346713eeRomain Guy     * @see Color
3413656743cc21bac43676568314366497346713eeRomain Guy     * @see #setColor(int)
3513656743cc21bac43676568314366497346713eeRomain Guy     * @see #setMode(android.graphics.PorterDuff.Mode)
3613656743cc21bac43676568314366497346713eeRomain Guy     */
3713656743cc21bac43676568314366497346713eeRomain Guy    public PorterDuffColorFilter(int color, PorterDuff.Mode mode) {
3813656743cc21bac43676568314366497346713eeRomain Guy        mColor = color;
3913656743cc21bac43676568314366497346713eeRomain Guy        mMode = mode;
4013656743cc21bac43676568314366497346713eeRomain Guy        update();
4113656743cc21bac43676568314366497346713eeRomain Guy    }
4213656743cc21bac43676568314366497346713eeRomain Guy
4313656743cc21bac43676568314366497346713eeRomain Guy    /**
4413656743cc21bac43676568314366497346713eeRomain Guy     * Returns the ARGB color used to tint the source pixels when this filter
4513656743cc21bac43676568314366497346713eeRomain Guy     * is applied.
4613656743cc21bac43676568314366497346713eeRomain Guy     *
4713656743cc21bac43676568314366497346713eeRomain Guy     * @see Color
4813656743cc21bac43676568314366497346713eeRomain Guy     * @see #setColor(int)
49f559326b182e321f51ab9711614d3e37fefa603aChris Craik     *
50f559326b182e321f51ab9711614d3e37fefa603aChris Craik     * @hide
5113656743cc21bac43676568314366497346713eeRomain Guy     */
5213656743cc21bac43676568314366497346713eeRomain Guy    public int getColor() {
5313656743cc21bac43676568314366497346713eeRomain Guy        return mColor;
5413656743cc21bac43676568314366497346713eeRomain Guy    }
5513656743cc21bac43676568314366497346713eeRomain Guy
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5713656743cc21bac43676568314366497346713eeRomain Guy     * Specifies the color to tint the source pixels with when this color
5813656743cc21bac43676568314366497346713eeRomain Guy     * filter is applied.
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6013656743cc21bac43676568314366497346713eeRomain Guy     * @param color An ARGB {@link Color color}
6113656743cc21bac43676568314366497346713eeRomain Guy     *
6213656743cc21bac43676568314366497346713eeRomain Guy     * @see Color
6313656743cc21bac43676568314366497346713eeRomain Guy     * @see #getColor()
6413656743cc21bac43676568314366497346713eeRomain Guy     * @see #getMode()
65954ad629477176b891ab51fe55140fc6f11e2a5bChris Craik     *
66954ad629477176b891ab51fe55140fc6f11e2a5bChris Craik     * @hide
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6813656743cc21bac43676568314366497346713eeRomain Guy    public void setColor(int color) {
6913656743cc21bac43676568314366497346713eeRomain Guy        mColor = color;
7013656743cc21bac43676568314366497346713eeRomain Guy        update();
7113656743cc21bac43676568314366497346713eeRomain Guy    }
7213656743cc21bac43676568314366497346713eeRomain Guy
7313656743cc21bac43676568314366497346713eeRomain Guy    /**
7413656743cc21bac43676568314366497346713eeRomain Guy     * Returns the Porter-Duff mode used to composite this color filter's
7513656743cc21bac43676568314366497346713eeRomain Guy     * color with the source pixel when this filter is applied.
7613656743cc21bac43676568314366497346713eeRomain Guy     *
7713656743cc21bac43676568314366497346713eeRomain Guy     * @see PorterDuff
7813656743cc21bac43676568314366497346713eeRomain Guy     * @see #setMode(android.graphics.PorterDuff.Mode)
79f559326b182e321f51ab9711614d3e37fefa603aChris Craik     *
80f559326b182e321f51ab9711614d3e37fefa603aChris Craik     * @hide
8113656743cc21bac43676568314366497346713eeRomain Guy     */
8213656743cc21bac43676568314366497346713eeRomain Guy    public PorterDuff.Mode getMode() {
8313656743cc21bac43676568314366497346713eeRomain Guy        return mMode;
8413656743cc21bac43676568314366497346713eeRomain Guy    }
8513656743cc21bac43676568314366497346713eeRomain Guy
8613656743cc21bac43676568314366497346713eeRomain Guy    /**
8713656743cc21bac43676568314366497346713eeRomain Guy     * Specifies the Porter-Duff mode to use when compositing this color
8813656743cc21bac43676568314366497346713eeRomain Guy     * filter's color with the source pixel at draw time.
8913656743cc21bac43676568314366497346713eeRomain Guy     *
9013656743cc21bac43676568314366497346713eeRomain Guy     * @see PorterDuff
9113656743cc21bac43676568314366497346713eeRomain Guy     * @see #getMode()
9213656743cc21bac43676568314366497346713eeRomain Guy     * @see #getColor()
93954ad629477176b891ab51fe55140fc6f11e2a5bChris Craik     *
94954ad629477176b891ab51fe55140fc6f11e2a5bChris Craik     * @hide
9513656743cc21bac43676568314366497346713eeRomain Guy     */
9613656743cc21bac43676568314366497346713eeRomain Guy    public void setMode(PorterDuff.Mode mode) {
9713656743cc21bac43676568314366497346713eeRomain Guy        mMode = mode;
9813656743cc21bac43676568314366497346713eeRomain Guy        update();
9913656743cc21bac43676568314366497346713eeRomain Guy    }
10013656743cc21bac43676568314366497346713eeRomain Guy
10113656743cc21bac43676568314366497346713eeRomain Guy    private void update() {
10276d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger        destroyFilter(native_instance);
10313656743cc21bac43676568314366497346713eeRomain Guy        native_instance = native_CreatePorterDuffFilter(mColor, mMode.nativeInt);
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
106607bd848269fb802550e63aa61945790616f97a7Alan Viverette    @Override
107607bd848269fb802550e63aa61945790616f97a7Alan Viverette    public boolean equals(Object object) {
108607bd848269fb802550e63aa61945790616f97a7Alan Viverette        if (this == object) {
109607bd848269fb802550e63aa61945790616f97a7Alan Viverette            return true;
110607bd848269fb802550e63aa61945790616f97a7Alan Viverette        }
111607bd848269fb802550e63aa61945790616f97a7Alan Viverette        if (object == null || getClass() != object.getClass()) {
112607bd848269fb802550e63aa61945790616f97a7Alan Viverette            return false;
113607bd848269fb802550e63aa61945790616f97a7Alan Viverette        }
114607bd848269fb802550e63aa61945790616f97a7Alan Viverette        final PorterDuffColorFilter other = (PorterDuffColorFilter) object;
115607bd848269fb802550e63aa61945790616f97a7Alan Viverette        if (mColor != other.mColor || mMode != other.mMode) {
116607bd848269fb802550e63aa61945790616f97a7Alan Viverette            return false;
117607bd848269fb802550e63aa61945790616f97a7Alan Viverette        }
118607bd848269fb802550e63aa61945790616f97a7Alan Viverette        return true;
119607bd848269fb802550e63aa61945790616f97a7Alan Viverette    }
120607bd848269fb802550e63aa61945790616f97a7Alan Viverette
121607bd848269fb802550e63aa61945790616f97a7Alan Viverette    @Override
122607bd848269fb802550e63aa61945790616f97a7Alan Viverette    public int hashCode() {
123607bd848269fb802550e63aa61945790616f97a7Alan Viverette        return 31 *  mMode.hashCode() + mColor;
124607bd848269fb802550e63aa61945790616f97a7Alan Viverette    }
125607bd848269fb802550e63aa61945790616f97a7Alan Viverette
12636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native long native_CreatePorterDuffFilter(int srcColor, int porterDuffMode);
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
128