blob: aa164b4cb4690cbcfe532675bd1df20ee3b5710f [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);
Mike Reed34c56a52021-01-22 15:26:41 -050043 canvas->drawImage(fImg, 0, 0, SkSamplingOptions(), &paint);
Florin Malitaf2965062019-02-15 13:49:01 -050044
Mike Reedb286bc22019-04-08 16:23:20 -040045 paint.setColorFilter(SkColorFilters::Lerp(fWeight, cf0, cf1));
Mike Reed34c56a52021-01-22 15:26:41 -050046 canvas->drawImage(fImg, fImg->width() + gap * fWeight, 0,
47 SkSamplingOptions(), &paint);
Florin Malitaf2965062019-02-15 13:49:01 -050048
49 paint.setColorFilter(cf1);
Mike Reed34c56a52021-01-22 15:26:41 -050050 canvas->drawImage(fImg, 2*fImg->width() + gap, 0, SkSamplingOptions(), &paint);
Florin Malitaf2965062019-02-15 13:49:01 -050051 }
52
53 void onDrawContent(SkCanvas* canvas) override {
54 if (!fImg) {
55 fImg = GetResourceAsImage("images/mandrill_256.png");
Mike Reede869a1e2019-04-30 12:18:54 -040056 fCF0 = SkColorFilters::Matrix(gMat);
Mike Reedb286bc22019-04-08 16:23:20 -040057 fCF1 = SkColorFilters::Blend(0xFF44CC88, SkBlendMode::kScreen);
Florin Malitaf2965062019-02-15 13:49:01 -050058 }
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
John Stiles1cf2c8d2020-08-13 22:58:04 -040075 Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override {
Florin Malitaf2965062019-02-15 13:49:01 -050076 return fRect.contains(SkScalarRoundToInt(x),
Hal Canaryfcf63592019-07-12 11:32:43 -040077 SkScalarRoundToInt(y)) ? new Click() : nullptr;
Florin Malitaf2965062019-02-15 13:49:01 -050078 }
79
80 bool onClick(Click* click) override {
Hal Canaryfcf63592019-07-12 11:32:43 -040081 fRect.offset(click->fCurr.fX - click->fPrev.fX,
82 click->fCurr.fY - click->fPrev.fY);
Florin Malitaf2965062019-02-15 13:49:01 -050083 return true;
84 }
85
86private:
87 SkIRect fRect;
88
John Stiles7571f9e2020-09-02 22:42:33 -040089 using INHERITED = Sample;
Florin Malitaf2965062019-02-15 13:49:01 -050090};
Mike Reed79a75422019-03-15 15:45:09 -040091DEF_SAMPLE( return new MixerView; )