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 #include "gm.h"
8 #include "sk_tool_utils.h"
9 #include "SkGradientShader.h"
10 
11 using namespace skiagm;
12 
13 struct GradData {
14     int             fCount;
15     const SkColor*  fColors;
16     const SkScalar* fPos;
17 };
18 
19 constexpr SkColor gColors[] = {
20     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
21     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
22     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
23     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
24     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
25     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
26     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
27     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
28 };
29 
30 //constexpr SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 };
31 
32 constexpr GradData gGradData[] = {
33     { 40, gColors, nullptr },
34     //  { 2, gColors, gPos },
35     //  { 2, gCol2, nullptr },
36 };
37 
MakeLinear(const SkPoint pts[2],const GradData & data,SkShader::TileMode tm)38 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
39     return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
40 }
41 
MakeRadial(const SkPoint pts[2],const GradData & data,SkShader::TileMode tm)42 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
43     const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) };
44     return SkGradientShader::MakeRadial(pt, pt.fX, data.fColors, data.fPos, data.fCount, tm);
45 }
46 
MakeSweep(const SkPoint pts[2],const GradData & data,SkShader::TileMode)47 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
48     const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) };
49     return SkGradientShader::MakeSweep(pt.fX, pt.fY, data.fColors, data.fPos, data.fCount);
50 }
51 
52 
53 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode);
54 
55 constexpr GradMaker gGradMakers[] = {
56     MakeLinear, MakeRadial, MakeSweep,
57 };
58 
59 ///////////////////////////////////////////////////////////////////////////////
60 
61 class GradientsGM : public GM {
62 public:
GradientsGM()63     GradientsGM() {
64         this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
65     }
66 
67 protected:
onShortName()68     SkString onShortName() override { return SkString("gradient_dirty_laundry"); }
onISize()69     SkISize onISize() override { return SkISize::Make(640, 615); }
70 
onDraw(SkCanvas * canvas)71     void onDraw(SkCanvas* canvas) override {
72         SkPoint pts[2] = { { 0, 0 },
73                            { SkIntToScalar(100), SkIntToScalar(100) }
74         };
75         SkShader::TileMode tm = SkShader::kClamp_TileMode;
76         SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
77         SkPaint paint;
78         paint.setAntiAlias(true);
79 
80         canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
81         for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
82             canvas->save();
83             for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
84                 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
85                 canvas->drawRect(r, paint);
86                 canvas->translate(0, SkIntToScalar(120));
87             }
88             canvas->restore();
89             canvas->translate(SkIntToScalar(120), 0);
90         }
91     }
92 
93 private:
94     typedef GM INHERITED;
95 };
96 
97 ///////////////////////////////////////////////////////////////////////////////
98 
MyFactory(void *)99 static GM* MyFactory(void*) { return new GradientsGM; }
100 static GMRegistry reg(MyFactory);
101