reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 6 | static SkShader* setgrad(const SkRect& r, SkColor c0, SkColor c1) { |
| 7 | SkColor colors[] = { c0, c1 }; |
| 8 | SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } }; |
| 9 | return SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 10 | SkShader::kClamp_TileMode, NULL); |
| 11 | } |
| 12 | |
| 13 | static void test_alphagradients(SkCanvas* canvas) { |
| 14 | SkRect r; |
| 15 | r.set(SkIntToScalar(10), SkIntToScalar(10), |
| 16 | SkIntToScalar(410), SkIntToScalar(30)); |
| 17 | SkPaint p, p2; |
| 18 | p2.setStyle(SkPaint::kStroke_Style); |
| 19 | |
| 20 | p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00))->unref(); |
| 21 | canvas->drawRect(r, p); |
| 22 | canvas->drawRect(r, p2); |
| 23 | |
| 24 | r.offset(0, r.height() + SkIntToScalar(4)); |
| 25 | p.setShader(setgrad(r, 0xFF00FF00, 0x00000000))->unref(); |
| 26 | canvas->drawRect(r, p); |
| 27 | canvas->drawRect(r, p2); |
| 28 | |
| 29 | r.offset(0, r.height() + SkIntToScalar(4)); |
| 30 | p.setShader(setgrad(r, 0xFF00FF00, 0x00FF0000))->unref(); |
| 31 | canvas->drawRect(r, p); |
| 32 | canvas->drawRect(r, p2); |
| 33 | } |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 37 | struct GradData { |
| 38 | int fCount; |
| 39 | const SkColor* fColors; |
| 40 | const SkScalar* fPos; |
| 41 | }; |
| 42 | |
| 43 | static const SkColor gColors[] = { |
| 44 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 45 | }; |
| 46 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 47 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 48 | static const SkScalar gPos2[] = { |
| 49 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 50 | }; |
| 51 | |
| 52 | static const GradData gGradData[] = { |
| 53 | { 2, gColors, NULL }, |
| 54 | { 2, gColors, gPos0 }, |
| 55 | { 2, gColors, gPos1 }, |
| 56 | { 5, gColors, NULL }, |
| 57 | { 5, gColors, gPos2 } |
| 58 | }; |
| 59 | |
| 60 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
| 61 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 62 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 63 | data.fCount, tm, mapper); |
| 64 | } |
| 65 | |
| 66 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
| 67 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 68 | SkPoint center; |
| 69 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 70 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 71 | return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
| 72 | data.fPos, data.fCount, tm, mapper); |
| 73 | } |
| 74 | |
| 75 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
| 76 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 77 | SkPoint center; |
| 78 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 79 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 80 | return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
| 81 | data.fPos, data.fCount, mapper); |
| 82 | } |
| 83 | |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame^] | 84 | static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
| 85 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 86 | 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)); |
| 91 | return SkGradientShader::CreateTwoPointRadial( |
| 92 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 93 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 94 | data.fColors, data.fPos, data.fCount, tm, mapper); |
| 95 | } |
| 96 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 97 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 98 | SkShader::TileMode tm, SkUnitMapper* mapper); |
| 99 | static const GradMaker gGradMakers[] = { |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame^] | 100 | MakeLinear, MakeRadial, MakeSweep, Make2Radial |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | class GradientsView : public SkView { |
| 106 | public: |
| 107 | GradientsView() {} |
| 108 | |
| 109 | protected: |
| 110 | // overrides from SkEventSink |
| 111 | virtual bool onQuery(SkEvent* evt) { |
| 112 | if (SampleCode::TitleQ(*evt)) { |
| 113 | SampleCode::TitleR(evt, "Gradients"); |
| 114 | return true; |
| 115 | } |
| 116 | return this->INHERITED::onQuery(evt); |
| 117 | } |
| 118 | |
| 119 | void drawBG(SkCanvas* canvas) { |
| 120 | canvas->drawColor(0xFFDDDDDD); |
| 121 | } |
| 122 | |
| 123 | virtual void onDraw(SkCanvas* canvas) { |
| 124 | this->drawBG(canvas); |
| 125 | |
| 126 | SkPoint pts[2] = { |
| 127 | { 0, 0 }, |
| 128 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 129 | }; |
| 130 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 131 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 132 | SkPaint paint; |
| 133 | paint.setAntiAlias(true); |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 134 | paint.setDither(true); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 135 | |
| 136 | canvas->save(); |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame^] | 137 | canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 138 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 139 | canvas->save(); |
| 140 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
| 141 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); |
| 142 | paint.setShader(shader); |
| 143 | canvas->drawRect(r, paint); |
| 144 | shader->unref(); |
| 145 | canvas->translate(0, SkIntToScalar(120)); |
| 146 | } |
| 147 | canvas->restore(); |
| 148 | canvas->translate(SkIntToScalar(120), 0); |
| 149 | } |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 150 | canvas->restore(); |
| 151 | |
| 152 | canvas->translate(0, SkIntToScalar(370)); |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame^] | 153 | // test_alphagradients(canvas); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
| 157 | typedef SkView INHERITED; |
| 158 | }; |
| 159 | |
| 160 | /////////////////////////////////////////////////////////////////////////////// |
| 161 | |
| 162 | static SkView* MyFactory() { return new GradientsView; } |
| 163 | static SkViewRegister reg(MyFactory); |
| 164 | |