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 */
7
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"
13
draw_gradient2(SkCanvas * canvas,const SkRect & rect,SkScalar delta)14 static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) {
15 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA };
16 SkScalar pos[] = { 0, 0.25f, 0.75f, SK_Scalar1 };
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;
29 SkPaint paint;
30 paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors,
31 pos, SK_ARRAY_COUNT(pos),
32 SkTileMode::kClamp));
33 canvas->drawRect(rect, paint);
34 }
35
36
37 class DegenerateTwoPtRadialsView : public Sample {
38 public:
DegenerateTwoPtRadialsView()39 DegenerateTwoPtRadialsView() {
40 fTime = 0;
41 this->setBGColor(0xFFDDDDDD);
42 }
43
44 protected:
name()45 SkString name() override { return SkString("DegenerateTwoPtRadials"); }
46
onDrawContent(SkCanvas * canvas)47 void onDrawContent(SkCanvas* canvas) override {
48 SkScalar delta = fTime / 15.f;
49 int intPart = SkScalarFloorToInt(delta);
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
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);
63 SkString txt;
64 txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta));
65 canvas->drawString(txt, l + w / 2 + w * DELTA_SCALE * delta, t + h + SK_Scalar1 * 10,
66 SkFont(), SkPaint());
67 }
68
onAnimate(double nanos)69 bool onAnimate(double nanos) override {
70 fTime = SkDoubleToScalar(1e-9 * nanos / 15);
71 return true;
72 }
73
74 private:
75 SkScalar fTime;
76 typedef Sample INHERITED;
77 };
78
79 //////////////////////////////////////////////////////////////////////////////
80
81 DEF_SAMPLE( return new DegenerateTwoPtRadialsView(); )
82