| 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/SkGradient.h" |
| 15 |
#include "src/base/SkUtils.h" |
| 16 |
#include "tools/DecodeUtils.h" |
| 17 |
#include "tools/Resources.h" |
| 18 |
#include "tools/viewer/ClickHandlerSlide.h" |
| 19 |
|
| 20 |
const float gMat[] = { |
| 21 |
.3f, .6f, .1f, 0, 0, |
| 22 |
.3f, .6f, .1f, 0, 0, |
| 23 |
.3f, .6f, .1f, 0, 0, |
| 24 |
0, 0, 0, 1, 0, |
| 25 |
}; |
| 26 |
|
| 27 |
class MixerSlide : public ClickHandlerSlide { |
| 28 |
sk_sp<SkImage> fImg; |
| 29 |
sk_sp<SkColorFilter> fCF0; |
| 30 |
sk_sp<SkColorFilter> fCF1; |
| 31 |
|
| 32 |
float fWeight = 0; |
| 33 |
float fDW = 0.02f; |
| 34 |
|
| 35 |
public: |
| 36 |
MixerSlide() { fName = "Mixer"; } |
| 37 |
|
| 38 |
void draw(SkCanvas* canvas) override { |
| 39 |
if (!fImg) { |
| 40 |
fImg = ToolUtils::GetResourceAsImage("images/mandrill_256.png"); |
| 41 |
fCF0 = SkColorFilters::Matrix(gMat); |
| 42 |
fCF1 = SkColorFilters::Blend(0xFF44CC88, SkBlendMode::kScreen); |
| 43 |
} |
| 44 |
|
| 45 |
float gap = fImg->width() * 3; |
| 46 |
|
| 47 |
canvas->translate(10, 10); |
| 48 |
dodraw(canvas, nullptr, fCF1, gap); |
| 49 |
canvas->translate(0, fImg->height() + 10); |
| 50 |
dodraw(canvas, fCF0, nullptr, gap); |
| 51 |
canvas->translate(0, fImg->height() + 10); |
| 52 |
dodraw(canvas, fCF0, fCF1, gap); |
| 53 |
|
| 54 |
fWeight += fDW; |
| 55 |
if (fWeight > 1 || fWeight < 0) { |
| 56 |
fDW = -fDW; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
protected: |
| 61 |
Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override { |
| 62 |
return fRect.contains(SkScalarRoundToInt(x), |
| 63 |
SkScalarRoundToInt(y)) ? new Click() : nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
bool onClick(Click* click) override { |
| 67 |
fRect.offset(click->fCurr.fX - click->fPrev.fX, |
| 68 |
click->fCurr.fY - click->fPrev.fY); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
private: |
| 73 |
void dodraw(SkCanvas* canvas, sk_sp<SkColorFilter> cf0, sk_sp<SkColorFilter> cf1, float gap) { |
| 74 |
SkPaint paint; |
| 75 |
paint.setColorFilter(cf0); |
| 76 |
canvas->drawImage(fImg, 0, 0, SkSamplingOptions(), &paint); |
| 77 |
|
| 78 |
paint.setColorFilter(SkColorFilters::Lerp(fWeight, cf0, cf1)); |
| 79 |
canvas->drawImage(fImg, fImg->width() + gap * fWeight, 0, |
| 80 |
SkSamplingOptions(), &paint); |
| 81 |
|
| 82 |
paint.setColorFilter(cf1); |
| 83 |
canvas->drawImage(fImg, 2*fImg->width() + gap, 0, SkSamplingOptions(), &paint); |
| 84 |
} |
| 85 |
|
| 86 |
SkIRect fRect; |
| 87 |
|
| 88 |
}; |
| 89 |
DEF_SLIDE( return new MixerSlide; ) |