blob: 2ec26a5cd5584f4c270b55b84cf6a4637cb9b7f7 [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
reed@android.coma3c05882009-04-02 20:49:39 +00008#include "SkCanvas.h"
9#include "SkGradientShader.h"
10
reed8a21c9f2016-03-08 18:50:00 -080011static sk_sp<SkShader> setgrad(const SkRect& r, SkColor c0, SkColor c1) {
reed@android.com1c12abe2009-07-02 15:01:02 +000012 SkColor colors[] = { c0, c1 };
13 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
reed8a21c9f2016-03-08 18:50:00 -080014 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@android.com1c12abe2009-07-02 15:01:02 +000015}
16
17static void test_alphagradients(SkCanvas* canvas) {
18 SkRect r;
19 r.set(SkIntToScalar(10), SkIntToScalar(10),
20 SkIntToScalar(410), SkIntToScalar(30));
21 SkPaint p, p2;
22 p2.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000023
reed8a21c9f2016-03-08 18:50:00 -080024 p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00));
reed@android.com1c12abe2009-07-02 15:01:02 +000025 canvas->drawRect(r, p);
26 canvas->drawRect(r, p2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000027
reed@android.com1c12abe2009-07-02 15:01:02 +000028 r.offset(0, r.height() + SkIntToScalar(4));
reed8a21c9f2016-03-08 18:50:00 -080029 p.setShader(setgrad(r, 0xFF00FF00, 0x00000000));
reed@android.com1c12abe2009-07-02 15:01:02 +000030 canvas->drawRect(r, p);
31 canvas->drawRect(r, p2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000032
reed@android.com1c12abe2009-07-02 15:01:02 +000033 r.offset(0, r.height() + SkIntToScalar(4));
reed8a21c9f2016-03-08 18:50:00 -080034 p.setShader(setgrad(r, 0xFF00FF00, 0x00FF0000));
reed@android.com1c12abe2009-07-02 15:01:02 +000035 canvas->drawRect(r, p);
36 canvas->drawRect(r, p2);
37}
38
39///////////////////////////////////////////////////////////////////////////////
40
reed@android.coma3c05882009-04-02 20:49:39 +000041struct GradData {
42 int fCount;
43 const SkColor* fColors;
44 const SkScalar* fPos;
45};
46
47static const SkColor gColors[] = {
48 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
49};
50static const SkScalar gPos0[] = { 0, SK_Scalar1 };
51static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
52static const SkScalar gPos2[] = {
53 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
54};
55
56static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070057 { 2, gColors, nullptr },
reed@android.coma3c05882009-04-02 20:49:39 +000058 { 2, gColors, gPos0 },
59 { 2, gColors, gPos1 },
halcanary96fcdcc2015-08-27 07:41:13 -070060 { 5, gColors, nullptr },
reed@android.coma3c05882009-04-02 20:49:39 +000061 { 5, gColors, gPos2 }
62};
63
reed8a21c9f2016-03-08 18:50:00 -080064static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
65 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@android.coma3c05882009-04-02 20:49:39 +000066}
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed8a21c9f2016-03-08 18:50:00 -080068static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.coma3c05882009-04-02 20:49:39 +000069 SkPoint center;
70 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
71 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -080072 return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
73 data.fPos, data.fCount, tm);
reed@android.coma3c05882009-04-02 20:49:39 +000074}
75
reed8a21c9f2016-03-08 18:50:00 -080076static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.coma3c05882009-04-02 20:49:39 +000077 SkPoint center;
78 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
79 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -080080 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@android.coma3c05882009-04-02 20:49:39 +000081}
82
reed8a21c9f2016-03-08 18:50:00 -080083static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com8e2af7e2009-09-22 20:14:42 +000084 SkPoint center0, center1;
85 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
86 SkScalarAve(pts[0].fY, pts[1].fY));
87 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
88 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed8a21c9f2016-03-08 18:50:00 -080089 return SkGradientShader::MakeTwoPointConical(
reed@android.com8e2af7e2009-09-22 20:14:42 +000090 center1, (pts[1].fX - pts[0].fX) / 7,
91 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000092 data.fColors, data.fPos, data.fCount, tm);
reed@android.com8e2af7e2009-09-22 20:14:42 +000093}
94
reed8a21c9f2016-03-08 18:50:00 -080095static sk_sp<SkShader> Make2ConicalConcentric(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000096 SkShader::TileMode tm) {
bsalomon@google.comdc66c422011-04-06 19:35:37 +000097 SkPoint center;
98 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
99 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -0800100 return SkGradientShader::MakeTwoPointConical(
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000101 center, (pts[1].fX - pts[0].fX) / 7,
102 center, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000103 data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000104}
105
reed8a21c9f2016-03-08 18:50:00 -0800106typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000107
reed@android.coma3c05882009-04-02 20:49:39 +0000108static const GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -0700109 MakeLinear, MakeRadial, MakeSweep, Make2Conical, Make2ConicalConcentric
reed@android.coma3c05882009-04-02 20:49:39 +0000110};
111
112///////////////////////////////////////////////////////////////////////////////
113
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400114class GradientsView : public Sample {
reed@android.coma3c05882009-04-02 20:49:39 +0000115public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116 GradientsView() {
reed@google.comf2183392011-04-22 14:10:48 +0000117 this->setBGColor(0xFFDDDDDD);
118 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed@android.coma3c05882009-04-02 20:49:39 +0000120protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400121 bool onQuery(Sample::Event* evt) override {
122 if (Sample::TitleQ(*evt)) {
123 Sample::TitleR(evt, "Gradients");
reed@android.coma3c05882009-04-02 20:49:39 +0000124 return true;
125 }
126 return this->INHERITED::onQuery(evt);
127 }
128
reed8a21c9f2016-03-08 18:50:00 -0800129 void onDrawContent(SkCanvas* canvas) override {
reed@android.coma3c05882009-04-02 20:49:39 +0000130 SkPoint pts[2] = {
131 { 0, 0 },
132 { SkIntToScalar(100), SkIntToScalar(100) }
133 };
reed@android.coma3c05882009-04-02 20:49:39 +0000134 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
135 SkPaint paint;
reed@android.com32a42492009-07-10 03:33:52 +0000136 paint.setDither(true);
reed@android.com1c12abe2009-07-02 15:01:02 +0000137
138 canvas->save();
reed@android.com8e2af7e2009-09-22 20:14:42 +0000139 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000140
141 for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) {
reed@android.coma3c05882009-04-02 20:49:39 +0000142 canvas->save();
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000143 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
144 canvas->save();
145 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
reed8a21c9f2016-03-08 18:50:00 -0800146 paint.setShader(gGradMakers[j](pts, gGradData[i], (SkShader::TileMode)tm));
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000147 canvas->drawRect(r, paint);
148 canvas->translate(0, SkIntToScalar(120));
149 }
150 canvas->restore();
151 canvas->translate(SkIntToScalar(120), 0);
reed@android.coma3c05882009-04-02 20:49:39 +0000152 }
153 canvas->restore();
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000154 canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0);
reed@android.coma3c05882009-04-02 20:49:39 +0000155 }
reed@android.com1c12abe2009-07-02 15:01:02 +0000156 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
reed@android.com1c12abe2009-07-02 15:01:02 +0000158 canvas->translate(0, SkIntToScalar(370));
caryclark@google.com02939ce2012-06-06 12:09:51 +0000159 if (false) { // avoid bit rot, suppress warning
160 test_alphagradients(canvas);
161 }
reed@android.coma3c05882009-04-02 20:49:39 +0000162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@android.coma3c05882009-04-02 20:49:39 +0000164private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400165 typedef Sample INHERITED;
reed@android.coma3c05882009-04-02 20:49:39 +0000166};
167
168///////////////////////////////////////////////////////////////////////////////
169
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400170DEF_SAMPLE( return new GradientsView(); )