Leon Scroggins III | 626647c | 2015-01-29 13:46:57 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Leon Scroggins III | ed1ce37 | 2015-01-30 09:05:11 -0500 | [diff] [blame] | 8 | #ifndef AvoidXfermode_DEFINED |
| 9 | #define AvoidXfermode_DEFINED |
Leon Scroggins III | 626647c | 2015-01-29 13:46:57 -0500 | [diff] [blame] | 10 | |
| 11 | #include "SkColor.h" |
| 12 | #include "SkTypes.h" |
| 13 | #include "SkXfermode.h" |
| 14 | |
Leon Scroggins III | ed1ce37 | 2015-01-30 09:05:11 -0500 | [diff] [blame] | 15 | /** \class AvoidXfermode |
Leon Scroggins III | 626647c | 2015-01-29 13:46:57 -0500 | [diff] [blame] | 16 | |
| 17 | This xfermode will draw the src everywhere except on top of the specified |
| 18 | color. |
| 19 | */ |
Leon Scroggins III | ed1ce37 | 2015-01-30 09:05:11 -0500 | [diff] [blame] | 20 | class AvoidXfermode : public SkXfermode { |
Leon Scroggins III | 626647c | 2015-01-29 13:46:57 -0500 | [diff] [blame] | 21 | public: |
| 22 | enum Mode { |
| 23 | kAvoidColor_Mode, //!< draw everywhere except on the opColor |
| 24 | kTargetColor_Mode //!< draw only on top of the opColor |
| 25 | }; |
| 26 | |
| 27 | /** This xfermode draws, or doesn't draw, based on the destination's |
| 28 | distance from an op-color. |
| 29 | |
| 30 | There are two modes, and each mode interprets a tolerance value. |
| 31 | |
| 32 | Avoid: In this mode, drawing is allowed only on destination pixels that |
| 33 | are different from the op-color. |
| 34 | Tolerance near 0: avoid any colors even remotely similar to the op-color |
| 35 | Tolerance near 255: avoid only colors nearly identical to the op-color |
| 36 | |
| 37 | Target: In this mode, drawing only occurs on destination pixels that |
| 38 | are similar to the op-color |
| 39 | Tolerance near 0: draw only on colors that are nearly identical to the op-color |
| 40 | Tolerance near 255: draw on any colors even remotely similar to the op-color |
| 41 | */ |
Leon Scroggins III | ed1ce37 | 2015-01-30 09:05:11 -0500 | [diff] [blame] | 42 | static AvoidXfermode* Create(SkColor opColor, U8CPU tolerance, Mode mode) { |
Ben Wagner | d1cbc16 | 2015-08-19 12:45:09 -0400 | [diff] [blame] | 43 | return new AvoidXfermode(opColor, tolerance, mode); |
Leon Scroggins III | 626647c | 2015-01-29 13:46:57 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // overrides from SkXfermode |
| 47 | void xfer32(SkPMColor dst[], const SkPMColor src[], int count, |
| 48 | const SkAlpha aa[]) const override; |
| 49 | void xfer16(uint16_t dst[], const SkPMColor src[], int count, |
| 50 | const SkAlpha aa[]) const override; |
| 51 | void xferA8(SkAlpha dst[], const SkPMColor src[], int count, |
| 52 | const SkAlpha aa[]) const override; |
| 53 | |
| 54 | SK_TO_STRING_OVERRIDE() |
Leon Scroggins III | ed1ce37 | 2015-01-30 09:05:11 -0500 | [diff] [blame] | 55 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(AvoidXfermode) |
Leon Scroggins III | 626647c | 2015-01-29 13:46:57 -0500 | [diff] [blame] | 56 | |
| 57 | protected: |
Leon Scroggins III | ed1ce37 | 2015-01-30 09:05:11 -0500 | [diff] [blame] | 58 | AvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode); |
Leon Scroggins III | 626647c | 2015-01-29 13:46:57 -0500 | [diff] [blame] | 59 | void flatten(SkWriteBuffer&) const override; |
| 60 | |
| 61 | private: |
| 62 | SkColor fOpColor; |
| 63 | uint32_t fDistMul; // x.14 cached from fTolerance |
| 64 | uint8_t fTolerance; |
| 65 | Mode fMode; |
| 66 | |
| 67 | typedef SkXfermode INHERITED; |
| 68 | }; |
| 69 | |
| 70 | #endif |