AvoidXfermode.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/*
2 * Copyright (C) 2008 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
19/**
20 * AvoidXfermode xfermode will draw the src everywhere except on top of the
21 * opColor or, depending on the Mode, draw only on top of the opColor.
22 */
23public class AvoidXfermode extends Xfermode {
24
25    // these need to match the enum in SkAvoidXfermode.h on the native side
26    public enum Mode {
27        AVOID   (0),    //!< draw everywhere except on the opColor
28        TARGET  (1);    //!< draw only on top of the opColor
29
30        Mode(int nativeInt) {
31            this.nativeInt = nativeInt;
32        }
33        final int nativeInt;
34    }
35
36    /**
37     * This xfermode will draw the src everywhere except on top of the opColor
38     * or, depending on the Mode, draw only on top of the opColor.
39     *
40     * @param opColor The color to avoid (or to target depending on Mode). Note
41     *                that the alpha in opColor is ignored.
42     * @param tolerance How closely we compare a pixel to the opColor.
43     *                  0 - only operate if exact match
44     *                  255 - maximum gradation (blending) based on how
45     *                  similar the pixel is to our opColor (max tolerance)
46     * @param mode If we should avoid or target the opColor
47     */
48    public AvoidXfermode(int opColor, int tolerance, Mode mode) {
49        if (tolerance < 0 || tolerance > 255) {
50            throw new IllegalArgumentException("tolerance must be 0..255");
51        }
52        native_instance = nativeCreate(opColor, tolerance, mode.nativeInt);
53    }
54
55    private static native int nativeCreate(int opColor, int tolerance,
56                                           int nativeMode);
57}
58