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 "Sample.h" 9 #include "SkCanvas.h" 10 #include "SkColorFilter.h" 11 #include "SkGradientShader.h" 12 #include "SkImage.h" 13 #include "SkPath.h" 14 #include "SkRegion.h" 15 #include "SkShader.h" 16 #include "SkUtils.h" 17 #include "Resources.h" 18 19 const SkScalar 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: onQuery(Event * evt)38 bool onQuery(Event* evt) override { 39 if (Sample::TitleQ(*evt)) { 40 Sample::TitleR(evt, "Mixer"); 41 return true; 42 } 43 return this->INHERITED::onQuery(evt); 44 } 45 dodraw(SkCanvas * canvas,sk_sp<SkColorFilter> cf0,sk_sp<SkColorFilter> cf1,float gap)46 void dodraw(SkCanvas* canvas, sk_sp<SkColorFilter> cf0, sk_sp<SkColorFilter> cf1, float gap) { 47 SkPaint paint; 48 paint.setColorFilter(cf0); 49 canvas->drawImage(fImg, 0, 0, &paint); 50 51 paint.setColorFilter(SkColorFilter::MakeLerp(cf0, cf1, fWeight)); 52 canvas->drawImage(fImg, fImg->width() + gap * fWeight, 0, &paint); 53 54 paint.setColorFilter(cf1); 55 canvas->drawImage(fImg, 2*fImg->width() + gap, 0, &paint); 56 } 57 onDrawContent(SkCanvas * canvas)58 void onDrawContent(SkCanvas* canvas) override { 59 if (!fImg) { 60 fImg = GetResourceAsImage("images/mandrill_256.png"); 61 fCF0 = SkColorFilter::MakeMatrixFilterRowMajor255(gMat); 62 fCF1 = SkColorFilter::MakeModeFilter(0xFF44CC88, SkBlendMode::kScreen); 63 } 64 65 float gap = fImg->width() * 3; 66 67 canvas->translate(10, 10); 68 dodraw(canvas, nullptr, fCF1, gap); 69 canvas->translate(0, fImg->height() + 10); 70 dodraw(canvas, fCF0, nullptr, gap); 71 canvas->translate(0, fImg->height() + 10); 72 dodraw(canvas, fCF0, fCF1, gap); 73 74 fWeight += fDW; 75 if (fWeight > 1 || fWeight < 0) { 76 fDW = -fDW; 77 } 78 } 79 onFindClickHandler(SkScalar x,SkScalar y,unsigned)80 virtual Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { 81 return fRect.contains(SkScalarRoundToInt(x), 82 SkScalarRoundToInt(y)) ? new Click(this) : nullptr; 83 } 84 onClick(Click * click)85 bool onClick(Click* click) override { 86 fRect.offset(click->fICurr.fX - click->fIPrev.fX, 87 click->fICurr.fY - click->fIPrev.fY); 88 return true; 89 } 90 91 private: 92 SkIRect fRect; 93 94 typedef Sample INHERITED; 95 }; 96 97 ////////////////////////////////////////////////////////////////////////////// 98 99 DEF_SAMPLE( return new MixerView; ) 100