epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 7 | #include "Sample.h" |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 11 | static sk_sp<SkShader> setgrad(const SkRect& r, SkColor c0, SkColor c1) { |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 12 | SkColor colors[] = { c0, c1 }; |
| 13 | SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 14 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | static 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 23 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 24 | p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00)); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 25 | canvas->drawRect(r, p); |
| 26 | canvas->drawRect(r, p2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 27 | |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 28 | r.offset(0, r.height() + SkIntToScalar(4)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 29 | p.setShader(setgrad(r, 0xFF00FF00, 0x00000000)); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 30 | canvas->drawRect(r, p); |
| 31 | canvas->drawRect(r, p2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 32 | |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 33 | r.offset(0, r.height() + SkIntToScalar(4)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 34 | p.setShader(setgrad(r, 0xFF00FF00, 0x00FF0000)); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 35 | canvas->drawRect(r, p); |
| 36 | canvas->drawRect(r, p2); |
| 37 | } |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 41 | struct GradData { |
| 42 | int fCount; |
| 43 | const SkColor* fColors; |
| 44 | const SkScalar* fPos; |
| 45 | }; |
| 46 | |
| 47 | static const SkColor gColors[] = { |
| 48 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 49 | }; |
| 50 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 51 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 52 | static const SkScalar gPos2[] = { |
| 53 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 54 | }; |
| 55 | |
| 56 | static const GradData gGradData[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | { 2, gColors, nullptr }, |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 58 | { 2, gColors, gPos0 }, |
| 59 | { 2, gColors, gPos1 }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | { 5, gColors, nullptr }, |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 61 | { 5, gColors, gPos2 } |
| 62 | }; |
| 63 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 64 | static 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.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 66 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 68 | static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 69 | SkPoint center; |
| 70 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 71 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 72 | return SkGradientShader::MakeRadial(center, center.fX, data.fColors, |
| 73 | data.fPos, data.fCount, tm); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 74 | } |
| 75 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 76 | static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 77 | SkPoint center; |
| 78 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 79 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 80 | return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 81 | } |
| 82 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 83 | static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame] | 84 | 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)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 89 | return SkGradientShader::MakeTwoPointConical( |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame] | 90 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 91 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 92 | data.fColors, data.fPos, data.fCount, tm); |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame] | 93 | } |
| 94 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 95 | static sk_sp<SkShader> Make2ConicalConcentric(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 96 | SkShader::TileMode tm) { |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 97 | SkPoint center; |
| 98 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 99 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 100 | return SkGradientShader::MakeTwoPointConical( |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 101 | center, (pts[1].fX - pts[0].fX) / 7, |
| 102 | center, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 103 | data.fColors, data.fPos, data.fCount, tm); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 104 | } |
| 105 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 106 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm); |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 107 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 108 | static const GradMaker gGradMakers[] = { |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 109 | MakeLinear, MakeRadial, MakeSweep, Make2Conical, Make2ConicalConcentric |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 114 | class GradientsView : public Sample { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 115 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | GradientsView() { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 117 | this->setBGColor(0xFFDDDDDD); |
| 118 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 120 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 121 | bool onQuery(Sample::Event* evt) override { |
| 122 | if (Sample::TitleQ(*evt)) { |
| 123 | Sample::TitleR(evt, "Gradients"); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | return this->INHERITED::onQuery(evt); |
| 127 | } |
| 128 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 129 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 130 | SkPoint pts[2] = { |
| 131 | { 0, 0 }, |
| 132 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 133 | }; |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 134 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 135 | SkPaint paint; |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 136 | paint.setDither(true); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 137 | |
| 138 | canvas->save(); |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame] | 139 | canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 140 | |
| 141 | for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 142 | canvas->save(); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 143 | 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++) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 146 | paint.setShader(gGradMakers[j](pts, gGradData[i], (SkShader::TileMode)tm)); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 147 | canvas->drawRect(r, paint); |
| 148 | canvas->translate(0, SkIntToScalar(120)); |
| 149 | } |
| 150 | canvas->restore(); |
| 151 | canvas->translate(SkIntToScalar(120), 0); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 152 | } |
| 153 | canvas->restore(); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 154 | canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 155 | } |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 156 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 157 | |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 158 | canvas->translate(0, SkIntToScalar(370)); |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 159 | if (false) { // avoid bit rot, suppress warning |
| 160 | test_alphagradients(canvas); |
| 161 | } |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 162 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 163 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 164 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 165 | typedef Sample INHERITED; |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /////////////////////////////////////////////////////////////////////////////// |
| 169 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 170 | DEF_SAMPLE( return new GradientsView(); ) |