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 "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/SkRRect.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkShader.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTileMode.h"
19 #include "include/effects/SkGradientShader.h"
20 
21 class TestGradientGM : public skiagm::GM {
22 public:
TestGradientGM()23     TestGradientGM() {}
24 
25 protected:
onShortName()26     SkString onShortName() override {
27         return SkString("testgradient");
28     }
29 
onISize()30     SkISize onISize() override {
31         return SkISize::Make(800, 800);
32     }
33 
onDraw(SkCanvas * canvas)34     void onDraw(SkCanvas* canvas) override {
35         // Set up a gradient paint for a rect.
36         // And non-gradient paint for other objects.
37         canvas->drawColor(SK_ColorWHITE);
38 
39         SkPaint paint;
40         paint.setStyle(SkPaint::kFill_Style);
41         paint.setAntiAlias(true);
42         paint.setStrokeWidth(4);
43         paint.setColor(0xFFFE938C);
44 
45         SkRect rect = SkRect::MakeXYWH(10, 10, 100, 160);
46 
47         SkPoint points[2] = {
48             SkPoint::Make(0.0f, 0.0f),
49             SkPoint::Make(256.0f, 256.0f)
50         };
51         SkColor colors[2] = {SK_ColorBLUE, SK_ColorYELLOW};
52         SkPaint newPaint(paint);
53         newPaint.setShader(SkGradientShader::MakeLinear(
54                 points, colors, nullptr, 2, SkTileMode::kClamp));
55         canvas->drawRect(rect, newPaint);
56 
57         SkRRect oval;
58         oval.setOval(rect);
59         oval.offset(40, 80);
60         paint.setColor(0xFFE6B89C);
61         canvas->drawRRect(oval, paint);
62 
63         paint.setColor(0xFF9CAFB7);
64         canvas->drawCircle(180, 50, 25, paint);
65 
66         rect.offset(80, 50);
67         paint.setColor(0xFF4281A4);
68         paint.setStyle(SkPaint::kStroke_Style);
69         canvas->drawRoundRect(rect, 10, 10, paint);
70     }
71 
72 private:
73     using INHERITED = skiagm::GM;
74 };
75 
76 // Register the GM
77 DEF_GM( return new TestGradientGM; )
78