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/SkGradientShader.h" 26 27 const int kWidth = 1000; 28 const int kHeight = 2000; 29 const int kNumRows = 100; 30 const int kCellHeight = kHeight / kNumRows; 31 const int kPadHeight = 1; 32 const int kRectHeight = kCellHeight - (2 * kPadHeight); 33 34 class HardstopGradientsManyGM : public skiagm::GM { 35 public: HardstopGradientsManyGM()36 HardstopGradientsManyGM() {} 37 38 protected: onShortName()39 SkString onShortName() override { 40 return SkString("hardstop_gradients_many"); 41 } 42 onISize()43 SkISize onISize() override { 44 return SkISize::Make(kWidth, kHeight); 45 } 46 onDraw(SkCanvas * canvas)47 void onDraw(SkCanvas* canvas) override { 48 static constexpr SkPoint points[] = { 49 SkPoint::Make(0, kRectHeight / 2), 50 SkPoint::Make(kWidth, kRectHeight / 2), 51 }; 52 53 std::vector<SkColor> colors; 54 std::vector<SkScalar> positions; 55 56 for (int row = 1; row <= kNumRows; ++row) { 57 // Assemble a gradient containing a blue-to-white blend, repeated N times per row. 58 colors.push_back(SK_ColorBLUE); 59 colors.push_back(SK_ColorWHITE); 60 61 positions = {0.0f}; 62 for (int pos = 1; pos < row; ++pos) { 63 float place = SkScalar(pos) / SkScalar(row); 64 positions.push_back(place); 65 positions.push_back(place); 66 } 67 positions.push_back(1.0f); 68 SkASSERT(positions.size() == colors.size()); 69 70 // Draw it. 71 auto shader = SkGradientShader::MakeLinear(points, 72 colors.data(), 73 positions.data(), 74 colors.size(), 75 SkTileMode::kClamp, 76 /*flags=*/0, 77 /*localMatrix=*/nullptr); 78 SkPaint paint; 79 paint.setShader(shader); 80 canvas->drawRect(SkRect::MakeXYWH(0, kPadHeight, kWidth, kRectHeight), paint); 81 82 canvas->translate(0, kCellHeight); 83 } 84 } 85 86 private: 87 using INHERITED = skiagm::GM; 88 }; 89 90 DEF_GM(return new HardstopGradientsManyGM;) 91