xref: /aosp_15_r20/external/skia/docs/examples/Modulate.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE(Modulate, 256, 256, false, 0) {
draw(SkCanvas * canvas)5 void draw(SkCanvas* canvas) {
6     auto drawSquare = [=](int dx, int dy, SkBlendMode mode, const char* label) -> void {
7         const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
8         const SkPoint horz[] = { { 0, 0 }, { 128, 0 } };
9         SkPaint paint;
10         paint.setShader(SkGradientShader::MakeLinear(horz, colors, nullptr, std::size(colors),
11                 SkTileMode::kClamp));
12         paint.setBlendMode(mode);
13         canvas->translate(dx, dy);
14         canvas->drawRect({0, 0, 128, 128}, paint);
15         paint.setBlendMode(SkBlendMode::kXor);
16         SkFont font = SkFont(fontMgr->matchFamilyStyle(nullptr, {}));
17         canvas->drawString(label, 40, 100, font, paint);
18     };
19     drawSquare(0, 0, SkBlendMode::kSrc, "destination");
20     drawSquare(128, 0, SkBlendMode::kSrc, "");
21     drawSquare(0, 128, SkBlendMode::kSrc, "");
22     canvas->translate(-128, -128);
23     canvas->rotate(90, 0, 128);
24     drawSquare(0, 0, SkBlendMode::kSrc, "source");
25     drawSquare(0, -128, SkBlendMode::kModulate, "modulate");
26     drawSquare(-128, 0, SkBlendMode::kMultiply, "multiply");
27 }
28 }  // END FIDDLE
29