blob: cbf5e9678a39c0764fc73dc071b0cd1c4ccd4e71 [file] [log] [blame]
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +00001/*
2 * Copyright 2013 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 */
Ben Wagner7fde8e12019-05-01 17:28:53 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkPoint.h"
13#include "include/core/SkRect.h"
14#include "include/core/SkRefCnt.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTileMode.h"
20#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/effects/SkGradientShader.h"
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000022
23using namespace skiagm;
24
25struct GradData {
26 int fCount;
27 const SkColor* fColors;
28 const SkScalar* fPos;
29};
30
mtkleindbfd7ab2016-09-01 11:24:54 -070031constexpr SkColor gColors[] = {
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000032 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
33 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
34 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
35 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
36 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
37 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
38 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
39 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
40};
41
mtkleindbfd7ab2016-09-01 11:24:54 -070042//constexpr SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 };
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000043
mtkleindbfd7ab2016-09-01 11:24:54 -070044constexpr GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070045 { 40, gColors, nullptr },
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000046 // { 2, gColors, gPos },
halcanary96fcdcc2015-08-27 07:41:13 -070047 // { 2, gCol2, nullptr },
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000048};
49
Mike Reedfae8fce2019-04-03 10:27:45 -040050static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
reed1a9b9642016-03-13 14:13:58 -070051 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000052}
53
Mike Reedfae8fce2019-04-03 10:27:45 -040054static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
reed1a9b9642016-03-13 14:13:58 -070055 const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) };
56 return SkGradientShader::MakeRadial(pt, pt.fX, data.fColors, data.fPos, data.fCount, tm);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000057}
58
Mike Reedfae8fce2019-04-03 10:27:45 -040059static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkTileMode) {
reed1a9b9642016-03-13 14:13:58 -070060 const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) };
61 return SkGradientShader::MakeSweep(pt.fX, pt.fY, data.fColors, data.fPos, data.fCount);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000062}
63
64
Mike Reedfae8fce2019-04-03 10:27:45 -040065typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkTileMode);
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000066
mtkleindbfd7ab2016-09-01 11:24:54 -070067constexpr GradMaker gGradMakers[] = {
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000068 MakeLinear, MakeRadial, MakeSweep,
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000069};
70
71///////////////////////////////////////////////////////////////////////////////
72
73class GradientsGM : public GM {
74public:
75 GradientsGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040076 this->setBGColor(0xFFDDDDDD);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000077 }
78
79protected:
mtklein36352bf2015-03-25 18:17:31 -070080 SkString onShortName() override { return SkString("gradient_dirty_laundry"); }
81 SkISize onISize() override { return SkISize::Make(640, 615); }
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000082
mtklein36352bf2015-03-25 18:17:31 -070083 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000084 SkPoint pts[2] = { { 0, 0 },
85 { SkIntToScalar(100), SkIntToScalar(100) }
86 };
Mike Reedfae8fce2019-04-03 10:27:45 -040087 SkTileMode tm = SkTileMode::kClamp;
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000088 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
89 SkPaint paint;
90 paint.setAntiAlias(true);
91
92 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
93 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
94 canvas->save();
95 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
reed1a9b9642016-03-13 14:13:58 -070096 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000097 canvas->drawRect(r, paint);
98 canvas->translate(0, SkIntToScalar(120));
99 }
100 canvas->restore();
101 canvas->translate(SkIntToScalar(120), 0);
102 }
103 }
104
105private:
John Stiles7571f9e2020-09-02 22:42:33 -0400106 using INHERITED = GM;
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +0000107};
108
109///////////////////////////////////////////////////////////////////////////////
110
Hal Canarye964c182019-01-23 10:22:01 -0500111DEF_GM( return new GradientsGM; )