xref: /aosp_15_r20/external/skia/gm/testgradient.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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:
getName() const26     SkString getName() const override { return SkString("testgradient"); }
27 
getISize()28     SkISize getISize() override { return SkISize::Make(800, 800); }
29 
onDraw(SkCanvas * canvas)30     void onDraw(SkCanvas* canvas) override {
31         // Set up a gradient paint for a rect.
32         // And non-gradient paint for other objects.
33         canvas->drawColor(SK_ColorWHITE);
34 
35         SkPaint paint;
36         paint.setStyle(SkPaint::kFill_Style);
37         paint.setAntiAlias(true);
38         paint.setStrokeWidth(4);
39         paint.setColor(0xFFFE938C);
40 
41         SkRect rect = SkRect::MakeXYWH(10, 10, 100, 160);
42 
43         SkPoint points[2] = {
44             SkPoint::Make(0.0f, 0.0f),
45             SkPoint::Make(256.0f, 256.0f)
46         };
47         SkColor colors[2] = {SK_ColorBLUE, SK_ColorYELLOW};
48         SkPaint newPaint(paint);
49         newPaint.setShader(SkGradientShader::MakeLinear(
50                 points, colors, nullptr, 2, SkTileMode::kClamp));
51         canvas->drawRect(rect, newPaint);
52 
53         SkRRect oval;
54         oval.setOval(rect);
55         oval.offset(40, 80);
56         paint.setColor(0xFFE6B89C);
57         canvas->drawRRect(oval, paint);
58 
59         paint.setColor(0xFF9CAFB7);
60         canvas->drawCircle(180, 50, 25, paint);
61 
62         rect.offset(80, 50);
63         paint.setColor(0xFF4281A4);
64         paint.setStyle(SkPaint::kStroke_Style);
65         canvas->drawRoundRect(rect, 10, 10, paint);
66     }
67 
68 private:
69     using INHERITED = skiagm::GM;
70 };
71 
72 // Register the GM
73 DEF_GM( return new TestGradientGM; )
74