| 1 |
/* |
| 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 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#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/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" |
| 19 |
#include "include/effects/SkGradient.h" |
| 20 |
|
| 21 |
class AlphaGradientsGM : public skiagm::GM { |
| 22 |
public: |
| 23 |
AlphaGradientsGM() {} |
| 24 |
|
| 25 |
protected: |
| 26 |
SkString getName() const override { return SkString("alphagradients"); } |
| 27 |
|
| 28 |
SkISize getISize() override { return SkISize::Make(640, 480); } |
| 29 |
|
| 30 |
static void draw_grad(SkCanvas* canvas, const SkRect& r, |
| 31 |
SkColor4f c0, SkColor4f c1, bool doPreMul) { |
| 32 |
SkColor4f colors[] = { c0, c1 }; |
| 33 |
SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } }; |
| 34 |
SkPaint paint; |
| 35 |
auto pm = doPreMul ? SkGradient::Interpolation::InPremul::kYes |
| 36 |
: SkGradient::Interpolation::InPremul::kNo; |
| 37 |
paint.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {pm}})); |
| 38 |
canvas->drawRect(r, paint); |
| 39 |
|
| 40 |
paint.setShader(nullptr); |
| 41 |
paint.setStyle(SkPaint::kStroke_Style); |
| 42 |
canvas->drawRect(r, paint); |
| 43 |
} |
| 44 |
|
| 45 |
void onDraw(SkCanvas* canvas) override { |
| 46 |
constexpr struct { |
| 47 |
SkColor4f fColor0; |
| 48 |
SkColor4f fColor1; |
| 49 |
} gRec[] = { |
| 50 |
{ SkColors::kWhite, {0, 0, 0, 0} }, |
| 51 |
{ SkColors::kWhite, {1, 0, 0, 0} }, |
| 52 |
{ SkColors::kWhite, {1, 1, 0, 0} }, |
| 53 |
{ SkColors::kWhite, {1, 1, 1, 0} }, |
| 54 |
|
| 55 |
{ SkColors::kRed, {0, 0, 0, 0} }, |
| 56 |
{ SkColors::kRed, {1, 0, 0, 0} }, |
| 57 |
{ SkColors::kRed, {1, 1, 0, 0} }, |
| 58 |
{ SkColors::kRed, {1, 1, 1, 0} }, |
| 59 |
|
| 60 |
{ SkColors::kBlue, {0, 0, 0, 0} }, |
| 61 |
{ SkColors::kBlue, {1, 0, 0, 0} }, |
| 62 |
{ SkColors::kBlue, {1, 1, 0, 0} }, |
| 63 |
{ SkColors::kBlue, {1, 1, 1, 0} }, |
| 64 |
}; |
| 65 |
|
| 66 |
SkRect r = SkRect::MakeWH(300, 30); |
| 67 |
|
| 68 |
canvas->translate(10, 10); |
| 69 |
|
| 70 |
for (int doPreMul = 0; doPreMul <= 1; ++doPreMul) { |
| 71 |
canvas->save(); |
| 72 |
for (size_t i = 0; i < std::size(gRec); ++i) { |
| 73 |
draw_grad(canvas, r, gRec[i].fColor0, gRec[i].fColor1, SkToBool(doPreMul)); |
| 74 |
canvas->translate(0, r.height() + 8); |
| 75 |
} |
| 76 |
canvas->restore(); |
| 77 |
canvas->translate(r.width() + 10, 0); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
private: |
| 82 |
using INHERITED = skiagm::GM; |
| 83 |
}; |
| 84 |
|
| 85 |
DEF_GM(return new AlphaGradientsGM;) |