blob: cde0155860c8ca4112b030efed8d2d90909187bf [file] [log] [blame]
Florin Malitaf2965062019-02-15 13:49:01 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Malitaf2965062019-02-15 13:49:01 -050018
Mike Reede869a1e2019-04-30 12:18:54 -040019const float gMat[] = {
Florin Malitaf2965062019-02-15 13:49:01 -050020 .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
26class 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
34public:
35 MixerView() {}
36
37protected:
Hal Canary8a027312019-07-03 10:55:44 -040038 SkString name() override { return SkString("Mixer"); }
Florin Malitaf2965062019-02-15 13:49:01 -050039
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 Reedb286bc22019-04-08 16:23:20 -040045 paint.setColorFilter(SkColorFilters::Lerp(fWeight, cf0, cf1));
Florin Malitaf2965062019-02-15 13:49:01 -050046 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 Reede869a1e2019-04-30 12:18:54 -040055 fCF0 = SkColorFilters::Matrix(gMat);
Mike Reedb286bc22019-04-08 16:23:20 -040056 fCF1 = SkColorFilters::Blend(0xFF44CC88, SkBlendMode::kScreen);
Florin Malitaf2965062019-02-15 13:49:01 -050057 }
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 Canaryb1f411a2019-08-29 10:39:22 -040074 virtual Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override {
Florin Malitaf2965062019-02-15 13:49:01 -050075 return fRect.contains(SkScalarRoundToInt(x),
Hal Canaryfcf63592019-07-12 11:32:43 -040076 SkScalarRoundToInt(y)) ? new Click() : nullptr;
Florin Malitaf2965062019-02-15 13:49:01 -050077 }
78
79 bool onClick(Click* click) override {
Hal Canaryfcf63592019-07-12 11:32:43 -040080 fRect.offset(click->fCurr.fX - click->fPrev.fX,
81 click->fCurr.fY - click->fPrev.fY);
Florin Malitaf2965062019-02-15 13:49:01 -050082 return true;
83 }
84
85private:
86 SkIRect fRect;
87
88 typedef Sample INHERITED;
89};
Mike Reed79a75422019-03-15 15:45:09 -040090DEF_SAMPLE( return new MixerView; )