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 | */ |
reed@google.com | bf0001d | 2014-01-13 14:53:55 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkFont.h" |
| 10 | #include "include/core/SkString.h" |
| 11 | #include "include/effects/SkGradientShader.h" |
| 12 | #include "samplecode/Sample.h" |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 13 | |
| 14 | static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) { |
| 15 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 16 | SkScalar pos[] = { 0, 0.25f, 0.75f, SK_Scalar1 }; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 17 | |
| 18 | SkScalar l = rect.fLeft; |
| 19 | SkScalar t = rect.fTop; |
| 20 | SkScalar w = rect.width(); |
| 21 | SkScalar h = rect.height(); |
| 22 | |
| 23 | SkASSERT(0 == SkScalarMod(w, SK_Scalar1 * 5)); |
| 24 | |
| 25 | SkPoint c0 = { l + 2 * w / 5 + delta, t + h / 2 }; |
| 26 | SkPoint c1 = { l + 3 * w / 5, t + h / 2 }; |
| 27 | SkScalar r0 = w / 5; |
| 28 | SkScalar r1 = 2 * w / 5; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 29 | SkPaint paint; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 30 | paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors, |
| 31 | pos, SK_ARRAY_COUNT(pos), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 32 | SkTileMode::kClamp)); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 33 | canvas->drawRect(rect, paint); |
| 34 | } |
| 35 | |
| 36 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 37 | class DegenerateTwoPtRadialsView : public Sample { |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 38 | public: |
| 39 | DegenerateTwoPtRadialsView() { |
| 40 | fTime = 0; |
| 41 | this->setBGColor(0xFFDDDDDD); |
| 42 | } |
| 43 | |
| 44 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 45 | SkString name() override { return SkString("DegenerateTwoPtRadials"); } |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onDrawContent(SkCanvas* canvas) override { |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 48 | SkScalar delta = fTime / 15.f; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 49 | int intPart = SkScalarFloorToInt(delta); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 50 | delta = delta - SK_Scalar1 * intPart; |
| 51 | if (intPart % 2) { |
| 52 | delta = SK_Scalar1 - delta; |
| 53 | } |
| 54 | delta -= SK_ScalarHalf; |
| 55 | static const int DELTA_SCALE = 500; |
| 56 | delta /= DELTA_SCALE; |
| 57 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 58 | SkScalar w = SK_Scalar1 * 500; |
| 59 | SkScalar h = SK_Scalar1 * 500; |
| 60 | SkScalar l = SK_Scalar1 * 100; |
| 61 | SkScalar t = SK_Scalar1 * 100; |
| 62 | draw_gradient2(canvas, SkRect::MakeXYWH(l, t, w, h), delta); |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 63 | SkString txt; |
| 64 | txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta)); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 65 | canvas->drawString(txt, l + w / 2 + w * DELTA_SCALE * delta, t + h + SK_Scalar1 * 10, |
| 66 | SkFont(), SkPaint()); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 69 | bool onAnimate(double nanos) override { |
| 70 | fTime = SkDoubleToScalar(1e-9 * nanos / 15); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 71 | return true; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | private: |
| 75 | SkScalar fTime; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 76 | typedef Sample INHERITED; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | ////////////////////////////////////////////////////////////////////////////// |
| 80 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 81 | DEF_SAMPLE( return new DegenerateTwoPtRadialsView(); ) |