blob: 49084d61f150560e9801abad1668451fbde8a21e [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:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000026 SkString getName() const override { return SkString("alphagradients"); }
reed@google.com3d3a8602013-05-24 14:58:44 +000027
Leandro Lovisolo8f023882023-08-15 21:13:52 +000028 SkISize getISize() override { return SkISize::Make(640, 480); }
reed@google.com3d3a8602013-05-24 14:58:44 +000029
30 static void draw_grad(SkCanvas* canvas, const SkRect& r,
31 SkColor c0, SkColor c1, bool doPreMul) {
32 SkColor colors[] = { c0, c1 };
33 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } };
34 SkPaint paint;
35 uint32_t flags = doPreMul ? SkGradientShader::kInterpolateColorsInPremul_Flag : 0;
reed8a21c9f2016-03-08 18:50:00 -080036 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
Mike Reedfae8fce2019-04-03 10:27:45 -040037 SkTileMode::kClamp, flags, nullptr));
reed@google.com3d3a8602013-05-24 14:58:44 +000038 canvas->drawRect(r, paint);
skia.committer@gmail.com454ae462013-05-25 07:01:12 +000039
halcanary96fcdcc2015-08-27 07:41:13 -070040 paint.setShader(nullptr);
reed@google.com3d3a8602013-05-24 14:58:44 +000041 paint.setStyle(SkPaint::kStroke_Style);
42 canvas->drawRect(r, paint);
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070046 constexpr struct {
reed@google.com3d3a8602013-05-24 14:58:44 +000047 SkColor fColor0;
48 SkColor fColor1;
49 } gRec[] = {
50 { 0xFFFFFFFF, 0x00000000 },
51 { 0xFFFFFFFF, 0x00FF0000 },
52 { 0xFFFFFFFF, 0x00FFFF00 },
53 { 0xFFFFFFFF, 0x00FFFFFF },
54 { 0xFFFF0000, 0x00000000 },
55 { 0xFFFF0000, 0x00FF0000 },
56 { 0xFFFF0000, 0x00FFFF00 },
57 { 0xFFFF0000, 0x00FFFFFF },
58 { 0xFF0000FF, 0x00000000 },
59 { 0xFF0000FF, 0x00FF0000 },
60 { 0xFF0000FF, 0x00FFFF00 },
61 { 0xFF0000FF, 0x00FFFFFF },
62 };
skia.committer@gmail.com454ae462013-05-25 07:01:12 +000063
reed@google.com3d3a8602013-05-24 14:58:44 +000064 SkRect r = SkRect::MakeWH(300, 30);
skia.committer@gmail.com454ae462013-05-25 07:01:12 +000065
reed@google.com3d3a8602013-05-24 14:58:44 +000066 canvas->translate(10, 10);
67
68 for (int doPreMul = 0; doPreMul <= 1; ++doPreMul) {
69 canvas->save();
Herb Derbyc37b3862022-06-21 09:49:17 -040070 for (size_t i = 0; i < std::size(gRec); ++i) {
reed@google.com3d3a8602013-05-24 14:58:44 +000071 draw_grad(canvas, r, gRec[i].fColor0, gRec[i].fColor1, SkToBool(doPreMul));
72 canvas->translate(0, r.height() + 8);
73 }
74 canvas->restore();
75 canvas->translate(r.width() + 10, 0);
76 }
77 }
78
reed@google.com3d3a8602013-05-24 14:58:44 +000079private:
John Stiles7571f9e2020-09-02 22:42:33 -040080 using INHERITED = skiagm::GM;
reed@google.com3d3a8602013-05-24 14:58:44 +000081};
82
halcanary385fe4d2015-08-26 13:07:48 -070083DEF_GM(return new AlphaGradientsGM;)