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"
4REG_FIDDLE(GradientShader_MakeLinear, 201, 201, false, 0) {
5void 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    SkColor transparentGreen = SkColorSetARGB(0, 0, 255, 255);
18    SkColor colors[] = { transparentGreen, SK_ColorBLUE, SK_ColorRED };
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        int flags = 0; // interpolate colors in unpremul
27        if (i % 2 == 1) {
28            // right column will have premul
29            flags = SkGradientShader::Flags::kInterpolateColorsInPremul_Flag;
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 = SkGradientShader::MakeLinear(
39            pts, colors, positions, 3, SkTileMode::kMirror,
40            flags, &matr);
41
42        p.setShader(lgs);
43        auto r = SkRect::MakeLTRB(blockX, blockY, blockX + 100, blockY + 100);
44        canvas->drawRect(r, p);
45        canvas->drawRect(r, strokePaint);
46    }
47}
48}  // END FIDDLE
49