blob: b50c21c95194187f9b3e48af689148c766c84b20 [file] [log] [blame]
reed@google.com3d3a8602013-05-24 14:58:44 +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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040010#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/SkShader.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
17#include "include/core/SkTileMode.h"
18#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/effects/SkGradientShader.h"
reed@google.com3d3a8602013-05-24 14:58:44 +000020
21class AlphaGradientsGM : public skiagm::GM {
22public:
23 AlphaGradientsGM() {}
24
25protected:
mtklein36352bf2015-03-25 18:17:31 -070026 SkString onShortName() override {
reed@google.com3d3a8602013-05-24 14:58:44 +000027 return SkString("alphagradients");
28 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 SkISize onISize() override {
reed@google.com3d3a8602013-05-24 14:58:44 +000031 return SkISize::Make(640, 480);
32 }
33
34 static void draw_grad(SkCanvas* canvas, const SkRect& r,
35 SkColor c0, SkColor c1, bool doPreMul) {
36 SkColor colors[] = { c0, c1 };
37 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } };
38 SkPaint paint;
39 uint32_t flags = doPreMul ? SkGradientShader::kInterpolateColorsInPremul_Flag : 0;
reed8a21c9f2016-03-08 18:50:00 -080040 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
Mike Reedfae8fce2019-04-03 10:27:45 -040041 SkTileMode::kClamp, flags, nullptr));
reed@google.com3d3a8602013-05-24 14:58:44 +000042 canvas->drawRect(r, paint);
skia.committer@gmail.com454ae462013-05-25 07:01:12 +000043
halcanary96fcdcc2015-08-27 07:41:13 -070044 paint.setShader(nullptr);
reed@google.com3d3a8602013-05-24 14:58:44 +000045 paint.setStyle(SkPaint::kStroke_Style);
46 canvas->drawRect(r, paint);
47 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070050 constexpr struct {
reed@google.com3d3a8602013-05-24 14:58:44 +000051 SkColor fColor0;
52 SkColor fColor1;
53 } gRec[] = {
54 { 0xFFFFFFFF, 0x00000000 },
55 { 0xFFFFFFFF, 0x00FF0000 },
56 { 0xFFFFFFFF, 0x00FFFF00 },
57 { 0xFFFFFFFF, 0x00FFFFFF },
58 { 0xFFFF0000, 0x00000000 },
59 { 0xFFFF0000, 0x00FF0000 },
60 { 0xFFFF0000, 0x00FFFF00 },
61 { 0xFFFF0000, 0x00FFFFFF },
62 { 0xFF0000FF, 0x00000000 },
63 { 0xFF0000FF, 0x00FF0000 },
64 { 0xFF0000FF, 0x00FFFF00 },
65 { 0xFF0000FF, 0x00FFFFFF },
66 };
skia.committer@gmail.com454ae462013-05-25 07:01:12 +000067
reed@google.com3d3a8602013-05-24 14:58:44 +000068 SkRect r = SkRect::MakeWH(300, 30);
skia.committer@gmail.com454ae462013-05-25 07:01:12 +000069
reed@google.com3d3a8602013-05-24 14:58:44 +000070 canvas->translate(10, 10);
71
72 for (int doPreMul = 0; doPreMul <= 1; ++doPreMul) {
73 canvas->save();
74 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
75 draw_grad(canvas, r, gRec[i].fColor0, gRec[i].fColor1, SkToBool(doPreMul));
76 canvas->translate(0, r.height() + 8);
77 }
78 canvas->restore();
79 canvas->translate(r.width() + 10, 0);
80 }
81 }
82
reed@google.com3d3a8602013-05-24 14:58:44 +000083private:
84 typedef skiagm::GM INHERITED;
85};
86
halcanary385fe4d2015-08-26 13:07:48 -070087DEF_GM(return new AlphaGradientsGM;)