blob: 247bcf6111382bc9d36506865df8c54b46653cd1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed@android.coma3c05882009-04-02 20:49:39 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
11
reed8a21c9f2016-03-08 18:50:00 -080012static sk_sp<SkShader> setgrad(const SkRect& r, SkColor c0, SkColor c1) {
reed@android.com1c12abe2009-07-02 15:01:02 +000013 SkColor colors[] = { c0, c1 };
14 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
reed8a21c9f2016-03-08 18:50:00 -080015 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@android.com1c12abe2009-07-02 15:01:02 +000016}
17
18static void test_alphagradients(SkCanvas* canvas) {
19 SkRect r;
20 r.set(SkIntToScalar(10), SkIntToScalar(10),
21 SkIntToScalar(410), SkIntToScalar(30));
22 SkPaint p, p2;
23 p2.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000024
reed8a21c9f2016-03-08 18:50:00 -080025 p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00));
reed@android.com1c12abe2009-07-02 15:01:02 +000026 canvas->drawRect(r, p);
27 canvas->drawRect(r, p2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
reed@android.com1c12abe2009-07-02 15:01:02 +000029 r.offset(0, r.height() + SkIntToScalar(4));
reed8a21c9f2016-03-08 18:50:00 -080030 p.setShader(setgrad(r, 0xFF00FF00, 0x00000000));
reed@android.com1c12abe2009-07-02 15:01:02 +000031 canvas->drawRect(r, p);
32 canvas->drawRect(r, p2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000033
reed@android.com1c12abe2009-07-02 15:01:02 +000034 r.offset(0, r.height() + SkIntToScalar(4));
reed8a21c9f2016-03-08 18:50:00 -080035 p.setShader(setgrad(r, 0xFF00FF00, 0x00FF0000));
reed@android.com1c12abe2009-07-02 15:01:02 +000036 canvas->drawRect(r, p);
37 canvas->drawRect(r, p2);
38}
39
40///////////////////////////////////////////////////////////////////////////////
41
reed@android.coma3c05882009-04-02 20:49:39 +000042struct GradData {
43 int fCount;
44 const SkColor* fColors;
45 const SkScalar* fPos;
46};
47
48static const SkColor gColors[] = {
49 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
50};
51static const SkScalar gPos0[] = { 0, SK_Scalar1 };
52static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
53static const SkScalar gPos2[] = {
54 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
55};
56
57static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070058 { 2, gColors, nullptr },
reed@android.coma3c05882009-04-02 20:49:39 +000059 { 2, gColors, gPos0 },
60 { 2, gColors, gPos1 },
halcanary96fcdcc2015-08-27 07:41:13 -070061 { 5, gColors, nullptr },
reed@android.coma3c05882009-04-02 20:49:39 +000062 { 5, gColors, gPos2 }
63};
64
reed8a21c9f2016-03-08 18:50:00 -080065static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
66 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@android.coma3c05882009-04-02 20:49:39 +000067}
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed8a21c9f2016-03-08 18:50:00 -080069static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.coma3c05882009-04-02 20:49:39 +000070 SkPoint center;
71 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
72 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -080073 return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
74 data.fPos, data.fCount, tm);
reed@android.coma3c05882009-04-02 20:49:39 +000075}
76
reed8a21c9f2016-03-08 18:50:00 -080077static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.coma3c05882009-04-02 20:49:39 +000078 SkPoint center;
79 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
80 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -080081 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@android.coma3c05882009-04-02 20:49:39 +000082}
83
reed8a21c9f2016-03-08 18:50:00 -080084static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com8e2af7e2009-09-22 20:14:42 +000085 SkPoint center0, center1;
86 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
87 SkScalarAve(pts[0].fY, pts[1].fY));
88 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
89 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed8a21c9f2016-03-08 18:50:00 -080090 return SkGradientShader::MakeTwoPointConical(
reed@android.com8e2af7e2009-09-22 20:14:42 +000091 center1, (pts[1].fX - pts[0].fX) / 7,
92 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000093 data.fColors, data.fPos, data.fCount, tm);
reed@android.com8e2af7e2009-09-22 20:14:42 +000094}
95
reed8a21c9f2016-03-08 18:50:00 -080096static sk_sp<SkShader> Make2ConicalConcentric(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000097 SkShader::TileMode tm) {
bsalomon@google.comdc66c422011-04-06 19:35:37 +000098 SkPoint center;
99 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
100 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -0800101 return SkGradientShader::MakeTwoPointConical(
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000102 center, (pts[1].fX - pts[0].fX) / 7,
103 center, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000104 data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000105}
106
reed8a21c9f2016-03-08 18:50:00 -0800107typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000108
reed@android.coma3c05882009-04-02 20:49:39 +0000109static const GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -0700110 MakeLinear, MakeRadial, MakeSweep, Make2Conical, Make2ConicalConcentric
reed@android.coma3c05882009-04-02 20:49:39 +0000111};
112
113///////////////////////////////////////////////////////////////////////////////
114
reed@google.comf2183392011-04-22 14:10:48 +0000115class GradientsView : public SampleView {
reed@android.coma3c05882009-04-02 20:49:39 +0000116public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117 GradientsView() {
reed@google.comf2183392011-04-22 14:10:48 +0000118 this->setBGColor(0xFFDDDDDD);
119 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed@android.coma3c05882009-04-02 20:49:39 +0000121protected:
reed8a21c9f2016-03-08 18:50:00 -0800122 bool onQuery(SkEvent* evt) override {
reed@android.coma3c05882009-04-02 20:49:39 +0000123 if (SampleCode::TitleQ(*evt)) {
124 SampleCode::TitleR(evt, "Gradients");
125 return true;
126 }
127 return this->INHERITED::onQuery(evt);
128 }
129
reed8a21c9f2016-03-08 18:50:00 -0800130 void onDrawContent(SkCanvas* canvas) override {
reed@android.coma3c05882009-04-02 20:49:39 +0000131 SkPoint pts[2] = {
132 { 0, 0 },
133 { SkIntToScalar(100), SkIntToScalar(100) }
134 };
reed@android.coma3c05882009-04-02 20:49:39 +0000135 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
136 SkPaint paint;
reed@android.com32a42492009-07-10 03:33:52 +0000137 paint.setDither(true);
reed@android.com1c12abe2009-07-02 15:01:02 +0000138
139 canvas->save();
reed@android.com8e2af7e2009-09-22 20:14:42 +0000140 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000141
142 for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) {
reed@android.coma3c05882009-04-02 20:49:39 +0000143 canvas->save();
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000144 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
145 canvas->save();
146 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
reed8a21c9f2016-03-08 18:50:00 -0800147 paint.setShader(gGradMakers[j](pts, gGradData[i], (SkShader::TileMode)tm));
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000148 canvas->drawRect(r, paint);
149 canvas->translate(0, SkIntToScalar(120));
150 }
151 canvas->restore();
152 canvas->translate(SkIntToScalar(120), 0);
reed@android.coma3c05882009-04-02 20:49:39 +0000153 }
154 canvas->restore();
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000155 canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0);
reed@android.coma3c05882009-04-02 20:49:39 +0000156 }
reed@android.com1c12abe2009-07-02 15:01:02 +0000157 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@android.com1c12abe2009-07-02 15:01:02 +0000159 canvas->translate(0, SkIntToScalar(370));
caryclark@google.com02939ce2012-06-06 12:09:51 +0000160 if (false) { // avoid bit rot, suppress warning
161 test_alphagradients(canvas);
162 }
reed@android.coma3c05882009-04-02 20:49:39 +0000163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed@android.coma3c05882009-04-02 20:49:39 +0000165private:
reed@google.comf2183392011-04-22 14:10:48 +0000166 typedef SampleView INHERITED;
reed@android.coma3c05882009-04-02 20:49:39 +0000167};
168
169///////////////////////////////////////////////////////////////////////////////
170
171static SkView* MyFactory() { return new GradientsView; }
172static SkViewRegister reg(MyFactory);