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