blob: f1a27d3f8391b7984ee434cfe4337053fb47b1e3 [file] [log] [blame]
reed@google.com4384fab2012-06-05 16:14:23 +00001/*
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
13static 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
19static void drawGrad(SkCanvas* canvas, const SkScalar d0[], const SkScalar d1[]) {
reed@google.comcb7be692012-06-06 20:31:56 +000020 const SkRect bounds = SkRect::MakeXYWH(SkIntToScalar(-50),
21 SkIntToScalar(-50),
22 SkIntToScalar(200),
23 SkIntToScalar(100));
24
reed@google.com4384fab2012-06-05 16:14:23 +000025 SkPoint c0 = { d0[0], d0[1] };
26 SkScalar r0 = d0[2];
27 SkPoint c1 = { d1[0], d1[1] };
28 SkScalar r1 = d1[2];
29
30 SkColor colors[] = { SK_ColorGREEN, SK_ColorRED };
31 SkPaint paint;
32 paint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -040033 sk_tool_utils::set_portable_typeface(&paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
reed@google.comcb7be692012-06-06 20:31:56 +000035 SkString str;
36 str.printf("%g,%g,%g %g,%g,%g",
37 SkScalarToFloat(c0.fX), SkScalarToFloat(c0.fY), SkScalarToFloat(r0),
38 SkScalarToFloat(c1.fX), SkScalarToFloat(c1.fY), SkScalarToFloat(r1));
39 canvas->drawText(str.c_str(), str.size(),
40 bounds.fLeft, bounds.fTop - paint.getTextSize()/2, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000041
reed@google.comcb7be692012-06-06 20:31:56 +000042 paint.setShader(SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1,
43 colors, NULL, 2,
44 SkShader::kClamp_TileMode))->unref();
45 canvas->drawRect(bounds, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@google.com4384fab2012-06-05 16:14:23 +000047 paint.setShader(NULL);
reed@google.comcb7be692012-06-06 20:31:56 +000048 paint.setColor(0x66000000);
reed@google.com4384fab2012-06-05 16:14:23 +000049 paint.setStyle(SkPaint::kStroke_Style);
50 canvas->drawCircle(c0.fX, c0.fY, r0, paint);
51 canvas->drawCircle(c1.fX, c1.fY, r1, paint);
reed@google.comcb7be692012-06-06 20:31:56 +000052 canvas->drawRect(bounds, paint);
reed@google.com4384fab2012-06-05 16:14:23 +000053}
54
55class TwoPointRadialGM : public skiagm::GM {
56public:
57 TwoPointRadialGM() {}
58
59protected:
60 SkString onShortName() {
reed@google.comcb7be692012-06-06 20:31:56 +000061 return SkString("twopointconical");
reed@google.com4384fab2012-06-05 16:14:23 +000062 }
63
tfarinaf5393182014-06-09 23:59:03 -070064 SkISize onISize() { return SkISize::Make(480, 780); }
reed@google.com4384fab2012-06-05 16:14:23 +000065
reed@google.com4384fab2012-06-05 16:14:23 +000066 virtual void onDraw(SkCanvas* canvas) {
67 if (false) {
68 SkPaint paint;
69 paint.setColor(SK_ColorBLUE);
robertphillips@google.com4bdfb8c2012-06-12 21:23:49 +000070 canvas->drawRect(
rmistry@google.comae933ce2012-08-23 18:19:56 +000071 SkRect::MakeWH(SkIntToScalar(this->getISize().fWidth),
72 SkIntToScalar(this->getISize().fHeight)),
robertphillips@google.com4bdfb8c2012-06-12 21:23:49 +000073 paint);
reed@google.com4384fab2012-06-05 16:14:23 +000074 }
75 SkPaint paint;
76 const int R0 = 20;
77 const int R1 = 40;
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
reed@google.com4384fab2012-06-05 16:14:23 +000079 const SkScalar DX = SkIntToScalar(250);
reed@google.comcb7be692012-06-06 20:31:56 +000080 const SkScalar DY = SkIntToScalar(130);
reed@google.com4384fab2012-06-05 16:14:23 +000081
reed@google.comcb7be692012-06-06 20:31:56 +000082 canvas->translate(SkIntToScalar(60), SkIntToScalar(70));
reed@google.com4384fab2012-06-05 16:14:23 +000083
84 static const int gData[] = {
85 0, 0, R0, 0, 0, R1,
reed@google.comcb7be692012-06-06 20:31:56 +000086 0, 0, R0, 20, 0, R1,
reed@google.com4384fab2012-06-05 16:14:23 +000087 0, 0, R0, 25, 0, R1,
88 0, 0, R0, 100, 0, R1,
reed@google.com4384fab2012-06-05 16:14:23 +000089 0, 0, R0, 25, 0, R0,
90 0, 0, R0, 100, 0, R0,
91 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@google.com4384fab2012-06-05 16:14:23 +000093 int count = SK_ARRAY_COUNT(gData) / 6;
94 for (int i = 0; i < count; ++i) {
95 SkScalar data[6];
96 intToScalars(data, &gData[i * 6], 6);
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@google.com4384fab2012-06-05 16:14:23 +000098 int n = canvas->save();
99 drawGrad(canvas, &data[0], &data[3]);
100 canvas->translate(DX, 0);
101 drawGrad(canvas, &data[3], &data[0]);
102 canvas->restoreToCount(n);
103 canvas->translate(0, DY);
104 }
105 }
106};
107
108//////////////////////////////////////////////////////////////////////////////
109
110static skiagm::GM* F(void*) { return new TwoPointRadialGM; }
111
112static skiagm::GMRegistry gR(F);