• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkShader.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTileMode.h"
18 #include "include/core/SkTypes.h"
19 #include "include/effects/SkGradientShader.h"
20 
21 class AlphaGradientsGM : public skiagm::GM {
22 public:
AlphaGradientsGM()23     AlphaGradientsGM() {}
24 
25 protected:
onShortName()26     SkString onShortName() override {
27         return SkString("alphagradients");
28     }
29 
onISize()30     SkISize onISize() override {
31         return SkISize::Make(640, 480);
32     }
33 
draw_grad(SkCanvas * canvas,const SkRect & r,SkColor c0,SkColor c1,bool doPreMul)34     static void draw_grad(SkCanvas* canvas, const SkRect& r,
35                           SkColor c0, SkColor c1, bool doPreMul) {
36         SkColor colors[] = { c0, c1 };
37         SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } };
38         SkPaint paint;
39         uint32_t flags = doPreMul ? SkGradientShader::kInterpolateColorsInPremul_Flag : 0;
40         paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
41                                                      SkTileMode::kClamp, flags, nullptr));
42         canvas->drawRect(r, paint);
43 
44         paint.setShader(nullptr);
45         paint.setStyle(SkPaint::kStroke_Style);
46         canvas->drawRect(r, paint);
47     }
48 
onDraw(SkCanvas * canvas)49     void onDraw(SkCanvas* canvas) override {
50         constexpr struct {
51             SkColor fColor0;
52             SkColor fColor1;
53         } gRec[] = {
54             { 0xFFFFFFFF, 0x00000000 },
55             { 0xFFFFFFFF, 0x00FF0000 },
56             { 0xFFFFFFFF, 0x00FFFF00 },
57             { 0xFFFFFFFF, 0x00FFFFFF },
58             { 0xFFFF0000, 0x00000000 },
59             { 0xFFFF0000, 0x00FF0000 },
60             { 0xFFFF0000, 0x00FFFF00 },
61             { 0xFFFF0000, 0x00FFFFFF },
62             { 0xFF0000FF, 0x00000000 },
63             { 0xFF0000FF, 0x00FF0000 },
64             { 0xFF0000FF, 0x00FFFF00 },
65             { 0xFF0000FF, 0x00FFFFFF },
66         };
67 
68         SkRect r = SkRect::MakeWH(300, 30);
69 
70         canvas->translate(10, 10);
71 
72         for (int doPreMul = 0; doPreMul <= 1; ++doPreMul) {
73             canvas->save();
74             for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
75                 draw_grad(canvas, r, gRec[i].fColor0, gRec[i].fColor1, SkToBool(doPreMul));
76                 canvas->translate(0, r.height() + 8);
77             }
78             canvas->restore();
79             canvas->translate(r.width() + 10, 0);
80         }
81     }
82 
83 private:
84     typedef skiagm::GM INHERITED;
85 };
86 
87 DEF_GM(return new AlphaGradientsGM;)
88