Debian Codegraph @

docs/examples/GradientShader_MakeLinear.cpp

raw
1 // Copyright 2020 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE(GradientShader_MakeLinear, 201, 201, false, 0) {
5 void draw(SkCanvas* canvas) {
6 // This fiddle draws 4 instances of a LinearGradient, demonstrating
7 // how the local matrix affects the gradient as well as the flag
8 // which controls unpremul vs premul color interpolation.
9
10 SkPaint strokePaint;
11 strokePaint.setStyle(SkPaint::kStroke_Style);
12 strokePaint.setColor(SK_ColorBLACK);
13
14 SkPaint p;
15 p.setStyle(SkPaint::kFill_Style);
16
17 SkColor4f transparentGreen = SkColors::kGreen.withAlpha(0);
18 SkColor4f colors[] = { transparentGreen, SkColors::kBlue, SkColors::kRed };
19 SkScalar positions[] = { 0.0, 0.65, 1.0 };
20
21 for (int i = 0; i < 4; i++) {
22 SkScalar blockX = (i % 2) * 100;
23 SkScalar blockY = (i / 2) * 100;
24 SkPoint pts[] = { {blockX, blockY}, {blockX + 50, blockY + 100} };
25
26 auto premul = SkGradient::Interpolation::InPremul::kNo;
27 if (i % 2 == 1) {
28 // right column will have premul
29 premul = SkGradient::Interpolation::InPremul::kYes;
30 }
31
32 SkMatrix matr = SkMatrix::I();
33 if (i / 2 == 1) {
34 // bottom row will be rotated 45 degrees.
35 matr.setRotate(45, blockX, blockY);
36 }
37
38 auto lgs = SkShaders::LinearGradient(
39 pts, {{colors, positions, SkTileMode::kMirror}, {premul}}, &matr);
40
41 p.setShader(lgs);
42 auto r = SkRect::MakeLTRB(blockX, blockY, blockX + 100, blockY + 100);
43 canvas->drawRect(r, p);
44 canvas->drawRect(r, strokePaint);
45 }
46 }
47 } // END FIDDLE