| reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | |
| 13 | static void intToScalars(SkScalar dst[], const int src[], int n) { |
| 14 | for (int i = 0; i < n; ++i) { |
| 15 | dst[i] = SkIntToScalar(src[i]); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | static void drawGrad(SkCanvas* canvas, const SkScalar d0[], const SkScalar d1[]) { |
| 20 | SkPoint c0 = { d0[0], d0[1] }; |
| 21 | SkScalar r0 = d0[2]; |
| 22 | SkPoint c1 = { d1[0], d1[1] }; |
| 23 | SkScalar r1 = d1[2]; |
| 24 | |
| 25 | SkColor colors[] = { SK_ColorGREEN, SK_ColorRED }; |
| 26 | SkPaint paint; |
| 27 | paint.setAntiAlias(true); |
| 28 | paint.setShader(SkGradientShader::CreateTwoPointRadial(c0, r0, c1, r1, |
| 29 | colors, NULL, 2, |
| 30 | SkShader::kClamp_TileMode))->unref(); |
| 31 | canvas->drawRect(SkRect::MakeXYWH(SkIntToScalar(-50), |
| 32 | SkIntToScalar(-50), |
| 33 | SkIntToScalar(200), |
| 34 | SkIntToScalar(100)), paint); |
| 35 | |
| 36 | paint.setShader(NULL); |
| 37 | paint.setStyle(SkPaint::kStroke_Style); |
| 38 | canvas->drawCircle(c0.fX, c0.fY, r0, paint); |
| 39 | canvas->drawCircle(c1.fX, c1.fY, r1, paint); |
| 40 | } |
| 41 | |
| 42 | class TwoPointRadialGM : public skiagm::GM { |
| 43 | public: |
| 44 | TwoPointRadialGM() {} |
| 45 | |
| 46 | protected: |
| 47 | SkString onShortName() { |
| 48 | return SkString("two_point_radial"); |
| 49 | } |
| 50 | |
| 51 | SkISize onISize() { return skiagm::make_isize(480, 725); } |
| 52 | |
| 53 | virtual void onDraw(SkCanvas* canvas) { |
| 54 | if (false) { |
| 55 | SkPaint paint; |
| 56 | paint.setColor(SK_ColorBLUE); |
| 57 | canvas->drawRect(SkRect::MakeWH(this->getISize().fWidth, this->getISize().fHeight), paint); |
| 58 | } |
| 59 | SkPaint paint; |
| 60 | const int R0 = 20; |
| 61 | const int R1 = 40; |
| 62 | |
| 63 | const SkScalar DX = SkIntToScalar(250); |
| 64 | const SkScalar DY = SkIntToScalar(120); |
| 65 | |
| 66 | canvas->translate(SkIntToScalar(60), SkIntToScalar(60)); |
| 67 | |
| 68 | static const int gData[] = { |
| 69 | 0, 0, R0, 0, 0, R1, |
| 70 | 0, 0, R0, 25, 0, R1, |
| 71 | 0, 0, R0, 100, 0, R1, |
| 72 | 0, 0, R0, 0, 0, R0, |
| 73 | 0, 0, R0, 25, 0, R0, |
| 74 | 0, 0, R0, 100, 0, R0, |
| 75 | }; |
| 76 | |
| 77 | int count = SK_ARRAY_COUNT(gData) / 6; |
| 78 | for (int i = 0; i < count; ++i) { |
| 79 | SkScalar data[6]; |
| 80 | intToScalars(data, &gData[i * 6], 6); |
| 81 | |
| 82 | int n = canvas->save(); |
| 83 | drawGrad(canvas, &data[0], &data[3]); |
| 84 | canvas->translate(DX, 0); |
| 85 | drawGrad(canvas, &data[3], &data[0]); |
| 86 | canvas->restoreToCount(n); |
| 87 | canvas->translate(0, DY); |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | ////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | static skiagm::GM* F(void*) { return new TwoPointRadialGM; } |
| 95 | |
| 96 | static skiagm::GMRegistry gR(F); |
| 97 | |