Debian Codegraph @

gm/hardstop_gradients_many.cpp

raw
1 /*
2 * Copyright 2021 Google LLC
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 /*
9 * This GM presents different gradients with an increasing number of
10 * hardstops, from 1 to 100.
11 */
12
13 #include "gm/gm.h"
14 #include "include/core/SkCanvas.h"
15 #include "include/core/SkColor.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkTileMode.h"
25 #include "include/effects/SkGradient.h"
26
27 #include <vector>
28
29 const int kWidth = 1000;
30 const int kHeight = 2000;
31 const int kNumRows = 100;
32 const int kCellHeight = kHeight / kNumRows;
33 const int kPadHeight = 1;
34 const int kRectHeight = kCellHeight - (2 * kPadHeight);
35
36 class HardstopGradientsManyGM : public skiagm::GM {
37 public:
38 HardstopGradientsManyGM() {}
39
40 protected:
41 SkString getName() const override { return SkString("hardstop_gradients_many"); }
42
43 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
44
45 void onDraw(SkCanvas* canvas) override {
46 static constexpr SkPoint points[] = {
47 SkPoint::Make(0, kRectHeight / 2),
48 SkPoint::Make(kWidth, kRectHeight / 2),
49 };
50
51 std::vector<SkColor4f> colors;
52 std::vector<float> positions;
53
54 for (int row = 1; row <= kNumRows; ++row) {
55 // Assemble a gradient containing a blue-to-white blend, repeated N times per row.
56 colors.push_back(SkColors::kBlue);
57 colors.push_back(SkColors::kWhite);
58
59 positions = {0.0f};
60 for (int pos = 1; pos < row; ++pos) {
61 float place = SkScalar(pos) / SkScalar(row);
62 positions.push_back(place);
63 positions.push_back(place);
64 }
65 positions.push_back(1.0f);
66 SkASSERT(positions.size() == colors.size());
67
68 // Draw it.
69 auto shader = SkShaders::LinearGradient(points,
70 {{colors, positions, SkTileMode::kClamp}, {}});
71 SkPaint paint;
72 paint.setShader(shader);
73 canvas->drawRect(SkRect::MakeXYWH(0, kPadHeight, kWidth, kRectHeight), paint);
74
75 canvas->translate(0, kCellHeight);
76 }
77 }
78
79 private:
80 using INHERITED = skiagm::GM;
81 };
82
83 DEF_GM(return new HardstopGradientsManyGM;)