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 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" 18 19 const float gMat[] = { 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: MixerView()35 MixerView() {} 36 37 protected: name()38 SkString name() override { return SkString("Mixer"); } 39 dodraw(SkCanvas * canvas,sk_sp<SkColorFilter> cf0,sk_sp<SkColorFilter> cf1,float gap)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, SkSamplingOptions(), &paint); 44 45 paint.setColorFilter(SkColorFilters::Lerp(fWeight, cf0, cf1)); 46 canvas->drawImage(fImg, fImg->width() + gap * fWeight, 0, 47 SkSamplingOptions(), &paint); 48 49 paint.setColorFilter(cf1); 50 canvas->drawImage(fImg, 2*fImg->width() + gap, 0, SkSamplingOptions(), &paint); 51 } 52 onDrawContent(SkCanvas * canvas)53 void onDrawContent(SkCanvas* canvas) override { 54 if (!fImg) { 55 fImg = GetResourceAsImage("images/mandrill_256.png"); 56 fCF0 = SkColorFilters::Matrix(gMat); 57 fCF1 = SkColorFilters::Blend(0xFF44CC88, SkBlendMode::kScreen); 58 } 59 60 float gap = fImg->width() * 3; 61 62 canvas->translate(10, 10); 63 dodraw(canvas, nullptr, fCF1, gap); 64 canvas->translate(0, fImg->height() + 10); 65 dodraw(canvas, fCF0, nullptr, gap); 66 canvas->translate(0, fImg->height() + 10); 67 dodraw(canvas, fCF0, fCF1, gap); 68 69 fWeight += fDW; 70 if (fWeight > 1 || fWeight < 0) { 71 fDW = -fDW; 72 } 73 } 74 onFindClickHandler(SkScalar x,SkScalar y,skui::ModifierKey)75 Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override { 76 return fRect.contains(SkScalarRoundToInt(x), 77 SkScalarRoundToInt(y)) ? new Click() : nullptr; 78 } 79 onClick(Click * click)80 bool onClick(Click* click) override { 81 fRect.offset(click->fCurr.fX - click->fPrev.fX, 82 click->fCurr.fY - click->fPrev.fY); 83 return true; 84 } 85 86 private: 87 SkIRect fRect; 88 89 using INHERITED = Sample; 90 }; 91 DEF_SAMPLE( return new MixerView; ) 92