blob: 12d24d2ae4f3713f0e1d27b91a56e0afb3b30be7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.coma3c05882009-04-02 20:49:39 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12
reed@android.com1c12abe2009-07-02 15:01:02 +000013static SkShader* setgrad(const SkRect& r, SkColor c0, SkColor c1) {
14 SkColor colors[] = { c0, c1 };
15 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000016 return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
reed@android.com1c12abe2009-07-02 15:01:02 +000017}
18
19static void test_alphagradients(SkCanvas* canvas) {
20 SkRect r;
21 r.set(SkIntToScalar(10), SkIntToScalar(10),
22 SkIntToScalar(410), SkIntToScalar(30));
23 SkPaint p, p2;
24 p2.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
reed@android.com1c12abe2009-07-02 15:01:02 +000026 p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00))->unref();
27 canvas->drawRect(r, p);
28 canvas->drawRect(r, p2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000029
reed@android.com1c12abe2009-07-02 15:01:02 +000030 r.offset(0, r.height() + SkIntToScalar(4));
31 p.setShader(setgrad(r, 0xFF00FF00, 0x00000000))->unref();
32 canvas->drawRect(r, p);
33 canvas->drawRect(r, p2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
reed@android.com1c12abe2009-07-02 15:01:02 +000035 r.offset(0, r.height() + SkIntToScalar(4));
36 p.setShader(setgrad(r, 0xFF00FF00, 0x00FF0000))->unref();
37 canvas->drawRect(r, p);
38 canvas->drawRect(r, p2);
39}
40
41///////////////////////////////////////////////////////////////////////////////
42
reed@android.coma3c05882009-04-02 20:49:39 +000043struct GradData {
44 int fCount;
45 const SkColor* fColors;
46 const SkScalar* fPos;
47};
48
49static const SkColor gColors[] = {
50 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
51};
52static const SkScalar gPos0[] = { 0, SK_Scalar1 };
53static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
54static const SkScalar gPos2[] = {
55 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
56};
57
58static const GradData gGradData[] = {
59 { 2, gColors, NULL },
60 { 2, gColors, gPos0 },
61 { 2, gColors, gPos1 },
62 { 5, gColors, NULL },
63 { 5, gColors, gPos2 }
64};
65
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000066static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
67 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@android.coma3c05882009-04-02 20:49:39 +000068}
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000070static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.coma3c05882009-04-02 20:49:39 +000071 SkPoint center;
72 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
73 SkScalarAve(pts[0].fY, pts[1].fY));
74 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000075 data.fPos, data.fCount, tm);
reed@android.coma3c05882009-04-02 20:49:39 +000076}
77
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000078static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.coma3c05882009-04-02 20:49:39 +000079 SkPoint center;
80 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
81 SkScalarAve(pts[0].fY, pts[1].fY));
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000082 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@android.coma3c05882009-04-02 20:49:39 +000083}
84
reed71a6cbf2015-05-04 08:32:51 -070085static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com8e2af7e2009-09-22 20:14:42 +000086 SkPoint center0, center1;
87 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
88 SkScalarAve(pts[0].fY, pts[1].fY));
89 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
90 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed71a6cbf2015-05-04 08:32:51 -070091 return SkGradientShader::CreateTwoPointConical(
reed@android.com8e2af7e2009-09-22 20:14:42 +000092 center1, (pts[1].fX - pts[0].fX) / 7,
93 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000094 data.fColors, data.fPos, data.fCount, tm);
reed@android.com8e2af7e2009-09-22 20:14:42 +000095}
96
reed71a6cbf2015-05-04 08:32:51 -070097static SkShader* Make2ConicalConcentric(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000098 SkShader::TileMode tm) {
bsalomon@google.comdc66c422011-04-06 19:35:37 +000099 SkPoint center;
100 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
101 SkScalarAve(pts[0].fY, pts[1].fY));
reed71a6cbf2015-05-04 08:32:51 -0700102 return SkGradientShader::CreateTwoPointConical(
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000103 center, (pts[1].fX - pts[0].fX) / 7,
104 center, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000105 data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000106}
107
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000108typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
109
reed@android.coma3c05882009-04-02 20:49:39 +0000110static const GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -0700111 MakeLinear, MakeRadial, MakeSweep, Make2Conical, Make2ConicalConcentric
reed@android.coma3c05882009-04-02 20:49:39 +0000112};
113
114///////////////////////////////////////////////////////////////////////////////
115
reed@google.comf2183392011-04-22 14:10:48 +0000116class GradientsView : public SampleView {
reed@android.coma3c05882009-04-02 20:49:39 +0000117public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118 GradientsView() {
reed@google.comf2183392011-04-22 14:10:48 +0000119 this->setBGColor(0xFFDDDDDD);
120 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed@android.coma3c05882009-04-02 20:49:39 +0000122protected:
123 // overrides from SkEventSink
124 virtual bool onQuery(SkEvent* evt) {
125 if (SampleCode::TitleQ(*evt)) {
126 SampleCode::TitleR(evt, "Gradients");
127 return true;
128 }
129 return this->INHERITED::onQuery(evt);
130 }
131
reed@google.comf2183392011-04-22 14:10:48 +0000132 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.coma3c05882009-04-02 20:49:39 +0000133 SkPoint pts[2] = {
134 { 0, 0 },
135 { SkIntToScalar(100), SkIntToScalar(100) }
136 };
reed@android.coma3c05882009-04-02 20:49:39 +0000137 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
138 SkPaint paint;
reed@android.com32a42492009-07-10 03:33:52 +0000139 paint.setDither(true);
reed@android.com1c12abe2009-07-02 15:01:02 +0000140
141 canvas->save();
reed@android.com8e2af7e2009-09-22 20:14:42 +0000142 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000143
144 for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) {
reed@android.coma3c05882009-04-02 20:49:39 +0000145 canvas->save();
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000146 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
147 canvas->save();
148 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
149 SkShader* shader;
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000150 shader = gGradMakers[j](pts, gGradData[i], (SkShader::TileMode)tm);
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000151 paint.setShader(shader)->unref();
152 canvas->drawRect(r, paint);
153 canvas->translate(0, SkIntToScalar(120));
154 }
155 canvas->restore();
156 canvas->translate(SkIntToScalar(120), 0);
reed@android.coma3c05882009-04-02 20:49:39 +0000157 }
158 canvas->restore();
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000159 canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0);
reed@android.coma3c05882009-04-02 20:49:39 +0000160 }
reed@android.com1c12abe2009-07-02 15:01:02 +0000161 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
reed@android.com1c12abe2009-07-02 15:01:02 +0000163 canvas->translate(0, SkIntToScalar(370));
caryclark@google.com02939ce2012-06-06 12:09:51 +0000164 if (false) { // avoid bit rot, suppress warning
165 test_alphagradients(canvas);
166 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000167 this->inval(NULL);
reed@android.coma3c05882009-04-02 20:49:39 +0000168 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000169
reed@android.coma3c05882009-04-02 20:49:39 +0000170private:
reed@google.comf2183392011-04-22 14:10:48 +0000171 typedef SampleView INHERITED;
reed@android.coma3c05882009-04-02 20:49:39 +0000172};
173
174///////////////////////////////////////////////////////////////////////////////
175
176static SkView* MyFactory() { return new GradientsView; }
177static SkViewRegister reg(MyFactory);