Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorFilter.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/core/SkRegion.h" |
| 13 | #include "include/core/SkShader.h" |
| 14 | #include "include/effects/SkGradientShader.h" |
| 15 | #include "samplecode/Sample.h" |
| 16 | #include "src/core/SkUtils.h" |
| 17 | #include "tools/Resources.h" |
Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 18 | |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 19 | const float gMat[] = { |
Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 20 | .3f, .6f, .1f, 0, 0, |
| 21 | .3f, .6f, .1f, 0, 0, |
| 22 | .3f, .6f, .1f, 0, 0, |
| 23 | 0, 0, 0, 1, 0, |
| 24 | }; |
| 25 | |
| 26 | class MixerView : public Sample { |
| 27 | sk_sp<SkImage> fImg; |
| 28 | sk_sp<SkColorFilter> fCF0; |
| 29 | sk_sp<SkColorFilter> fCF1; |
| 30 | |
| 31 | float fWeight = 0; |
| 32 | float fDW = 0.02f; |
| 33 | |
| 34 | public: |
| 35 | MixerView() {} |
| 36 | |
| 37 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 38 | SkString name() override { return SkString("Mixer"); } |
Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 39 | |
| 40 | void dodraw(SkCanvas* canvas, sk_sp<SkColorFilter> cf0, sk_sp<SkColorFilter> cf1, float gap) { |
| 41 | SkPaint paint; |
| 42 | paint.setColorFilter(cf0); |
| 43 | canvas->drawImage(fImg, 0, 0, &paint); |
| 44 | |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 45 | paint.setColorFilter(SkColorFilters::Lerp(fWeight, cf0, cf1)); |
Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 46 | canvas->drawImage(fImg, fImg->width() + gap * fWeight, 0, &paint); |
| 47 | |
| 48 | paint.setColorFilter(cf1); |
| 49 | canvas->drawImage(fImg, 2*fImg->width() + gap, 0, &paint); |
| 50 | } |
| 51 | |
| 52 | void onDrawContent(SkCanvas* canvas) override { |
| 53 | if (!fImg) { |
| 54 | fImg = GetResourceAsImage("images/mandrill_256.png"); |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 55 | fCF0 = SkColorFilters::Matrix(gMat); |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 56 | fCF1 = SkColorFilters::Blend(0xFF44CC88, SkBlendMode::kScreen); |
Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | float gap = fImg->width() * 3; |
| 60 | |
| 61 | canvas->translate(10, 10); |
| 62 | dodraw(canvas, nullptr, fCF1, gap); |
| 63 | canvas->translate(0, fImg->height() + 10); |
| 64 | dodraw(canvas, fCF0, nullptr, gap); |
| 65 | canvas->translate(0, fImg->height() + 10); |
| 66 | dodraw(canvas, fCF0, fCF1, gap); |
| 67 | |
| 68 | fWeight += fDW; |
| 69 | if (fWeight > 1 || fWeight < 0) { |
| 70 | fDW = -fDW; |
| 71 | } |
| 72 | } |
| 73 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 74 | virtual Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override { |
Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 75 | return fRect.contains(SkScalarRoundToInt(x), |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 76 | SkScalarRoundToInt(y)) ? new Click() : nullptr; |
Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool onClick(Click* click) override { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 80 | fRect.offset(click->fCurr.fX - click->fPrev.fX, |
| 81 | click->fCurr.fY - click->fPrev.fY); |
Florin Malita | f296506 | 2019-02-15 13:49:01 -0500 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | SkIRect fRect; |
| 87 | |
| 88 | typedef Sample INHERITED; |
| 89 | }; |
Mike Reed | 79a7542 | 2019-03-15 15:45:09 -0400 | [diff] [blame] | 90 | DEF_SAMPLE( return new MixerView; ) |