blob: 26eee9d6c4beac98e3f8e5e1b472b8b05b072d8a [file] [log] [blame]
reed@android.com42309d42009-06-22 02:06:35 +00001#include "gm.h"
2#include "SkGradientShader.h"
3
4namespace skiagm {
5
6struct GradData {
7 int fCount;
8 const SkColor* fColors;
9 const SkScalar* fPos;
10};
11
12static const SkColor gColors[] = {
13 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
14};
15static const SkScalar gPos0[] = { 0, SK_Scalar1 };
16static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
17static const SkScalar gPos2[] = {
18 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
19};
20
21static const GradData gGradData[] = {
22 { 2, gColors, NULL },
23 { 2, gColors, gPos0 },
24 { 2, gColors, gPos1 },
25 { 5, gColors, NULL },
26 { 5, gColors, gPos2 }
27};
28
29static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data,
30 SkShader::TileMode tm, SkUnitMapper* mapper) {
31 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos,
32 data.fCount, tm, mapper);
33}
reed@google.comf3c1cc92010-12-23 16:45:33 +000034
reed@android.com42309d42009-06-22 02:06:35 +000035static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data,
36 SkShader::TileMode tm, SkUnitMapper* mapper) {
37 SkPoint center;
38 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
39 SkScalarAve(pts[0].fY, pts[1].fY));
40 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
41 data.fPos, data.fCount, tm, mapper);
42}
43
44static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data,
45 SkShader::TileMode tm, SkUnitMapper* mapper) {
46 SkPoint center;
47 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
48 SkScalarAve(pts[0].fY, pts[1].fY));
49 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors,
50 data.fPos, data.fCount, mapper);
51}
52
reed@google.comf3c1cc92010-12-23 16:45:33 +000053static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data,
54 SkShader::TileMode tm, SkUnitMapper* mapper) {
55 SkPoint center0, center1;
56 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
57 SkScalarAve(pts[0].fY, pts[1].fY));
58 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
59 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
60 return SkGradientShader::CreateTwoPointRadial(
61 center1, (pts[1].fX - pts[0].fX) / 7,
62 center0, (pts[1].fX - pts[0].fX) / 2,
63 data.fColors, data.fPos, data.fCount, tm, mapper);
64}
65
reed@android.com42309d42009-06-22 02:06:35 +000066typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
reed@google.comf3c1cc92010-12-23 16:45:33 +000067 SkShader::TileMode tm, SkUnitMapper* mapper);
reed@android.com42309d42009-06-22 02:06:35 +000068static const GradMaker gGradMakers[] = {
reed@google.comf3c1cc92010-12-23 16:45:33 +000069 MakeLinear, MakeRadial, MakeSweep, Make2Radial
reed@android.com42309d42009-06-22 02:06:35 +000070};
71
72///////////////////////////////////////////////////////////////////////////////
73
74class GradientsGM : public GM {
75public:
76 GradientsGM() {}
77
78protected:
79 SkString onShortName() {
80 return SkString("gradients");
81 }
82
reed@google.comf3c1cc92010-12-23 16:45:33 +000083 SkISize onISize() { return make_isize(640, 510); }
reed@android.com42309d42009-06-22 02:06:35 +000084
85 void drawBG(SkCanvas* canvas) {
86 canvas->drawColor(0xFFDDDDDD);
87 }
88
89 virtual void onDraw(SkCanvas* canvas) {
90 this->drawBG(canvas);
91
92 SkPoint pts[2] = {
93 { 0, 0 },
94 { SkIntToScalar(100), SkIntToScalar(100) }
95 };
96 SkShader::TileMode tm = SkShader::kClamp_TileMode;
97 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
98 SkPaint paint;
99 paint.setAntiAlias(true);
100
101 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
102 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
103 canvas->save();
104 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
105 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL);
106 paint.setShader(shader);
107 canvas->drawRect(r, paint);
108 shader->unref();
109 canvas->translate(0, SkIntToScalar(120));
110 }
111 canvas->restore();
112 canvas->translate(SkIntToScalar(120), 0);
113 }
114 }
115
116private:
117 typedef GM INHERITED;
118};
119
120///////////////////////////////////////////////////////////////////////////////
121
122static GM* MyFactory(void*) { return new GradientsGM; }
123static GMRegistry reg(MyFactory);
124
125}
126