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