• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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(BlendModes, 256, 4352, false, 0) {
drawBG(SkCanvas * canvas)5 void drawBG(SkCanvas* canvas) {
6     SkColor radColors[] = {0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF};
7     auto rad = SkGradientShader::MakeRadial(SkPoint::Make(128, 128), 128, radColors, nullptr, 3,
8                                             SkTileMode::kClamp);
9 
10     SkMatrix rotMtx;
11     rotMtx.setRotate(-90, 128, 128);
12     SkColor sweepColors[] = {0xFFFF00FF, 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00,
13                              0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF};
14     auto sweep = SkGradientShader::MakeSweep(128, 128, sweepColors, nullptr, 7, 0, &rotMtx);
15 
16     auto comp = SkShaders::Blend(SkBlendMode::kModulate, std::move(rad), std::move(sweep));
17     SkPaint p;
18     p.setShader(std::move(comp));
19 
20     canvas->drawPaint(p);
21 }
22 
draw(SkCanvas * canvas)23 void draw(SkCanvas* canvas) {
24     SkBlendMode blendModes[] = {
25         SkBlendMode::kDst,
26         SkBlendMode::kSrc,
27         SkBlendMode::kSrcOver,
28         SkBlendMode::kDstOver,
29         SkBlendMode::kSrcIn,
30         SkBlendMode::kDstIn,
31         SkBlendMode::kSrcOut,
32         SkBlendMode::kDstOut,
33         SkBlendMode::kSrcATop,
34         SkBlendMode::kDstATop,
35         SkBlendMode::kXor,
36         SkBlendMode::kPlus,
37         SkBlendMode::kModulate,
38         SkBlendMode::kScreen,
39         SkBlendMode::kOverlay,
40         SkBlendMode::kDarken,
41         SkBlendMode::kLighten,
42     };
43 
44     SkPaint labelPaint;
45     labelPaint.setAntiAlias(true);
46     SkFont font(nullptr, 12);
47 
48     for (auto mode : blendModes) {
49         SkPaint layerPaint;
50         layerPaint.setBlendMode(mode);
51 
52         canvas->save();
53         canvas->clipRect(SkRect::MakeWH(256, 256));
54 
55         drawBG(canvas);
56 
57         canvas->saveLayer(nullptr, &layerPaint);
58         const SkScalar r = 80;
59         SkPaint discP;
60         discP.setAntiAlias(true);
61         discP.setBlendMode(SkBlendMode::kPlus);
62         discP.setColor(SK_ColorGREEN); canvas->drawCircle(128, r, r, discP);
63         discP.setColor(SK_ColorRED);   canvas->drawCircle(r, 256 - r, r, discP);
64         discP.setColor(SK_ColorBLUE);  canvas->drawCircle(256 - r, 256 - r, r, discP);
65         canvas->restore();
66 
67         canvas->drawSimpleText(SkBlendMode_Name(mode), strlen(SkBlendMode_Name(mode)),
68                                SkTextEncoding::kUTF8, 10, 10, font, labelPaint);
69         canvas->restore();
70         canvas->translate(0, 256);
71     }
72 }
73 }  // END FIDDLE
74