blob: e549e12ed57be7904153a82857f06f094581d2cd [file] [log] [blame]
Kevin Lubick4765da42020-03-18 13:21:18 -04001// 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++) {
Kevin Lubick6f60a852020-03-23 09:20:38 -040022 SkScalar blockX = (i % 2) * 100;
23 SkScalar blockY = (i / 2) * 100;
Kevin Lubick4765da42020-03-18 13:21:18 -040024 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
Kevin Lubick6f60a852020-03-23 09:20:38 -040032 SkMatrix matr = SkMatrix::I();
Kevin Lubick4765da42020-03-18 13:21:18 -040033 if (i / 2 == 1) {
34 // bottom row will be rotated 45 degrees.
Kevin Lubick6f60a852020-03-23 09:20:38 -040035 matr.setRotate(45, blockX, blockY);
Kevin Lubick4765da42020-03-18 13:21:18 -040036 }
37
38 auto lgs = SkGradientShader::MakeLinear(
Kevin Lubick6f60a852020-03-23 09:20:38 -040039 pts, colors, positions, 3, SkTileMode::kMirror,
40 flags, &matr);
Kevin Lubick4765da42020-03-18 13:21:18 -040041
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